zoukankan      html  css  js  c++  java
  • 3_observer

    #Observer
    
    成就系统
    achievements system
    玩家完成某种成就后,通知监督者,监督者做出相应出来
    
    
    ```
    //简单来说就是事件触发的时候, 通知监督者
    
    class Observer
    {
    public:
      virtual ~Observer() {}
      virtual void onNotify(const Entity& entity, Event event) = 0;
    };
    
    class Achievements : public Observer
    {
    public:
      virtual void onNotify(const Entity& entity, Event event)
      {
        switch (event)
        {
        case EVENT_ENTITY_FELL:
          if (entity.isHero() && heroIsOnBridge_)
          {
            unlock(ACHIEVEMENT_FELL_OFF_BRIDGE);
          }
          break;
    
          // Handle other events, and update heroIsOnBridge_...
        }
      }
    
    private:
      void unlock(Achievement achievement)
      {
        // Unlock if not already unlocked...
      }
    
      bool heroIsOnBridge_;
    };
    
    class Subject
    {
    private:
      Observer* observers_[MAX_OBSERVERS];
      int numObservers_;
      
     public:
      void addObserver(Observer* observer)
      {
        // Add to array...
      }
    
      void removeObserver(Observer* observer)
      {
        // Remove from array...
      }
    
      
      protected:
      void notify(const Entity& entity, Event event)
      {
        for (int i = 0; i < numObservers_; i++)
        {
          observers_[i]->onNotify(entity, event);
        }
      }
      
      
    };
    ```
    
    语言绑定:
        
        java mvc
        c# event
    
    
    其他例子:
    
    ```
    1 ui得到数据改变的通知
    2 数据改变,ui即时表现出来
    ```
  • 相关阅读:
    spring和mybatis的结合
    SpringMVC
    springdata
    springboot的总结
    SpringAop代理模式笔记
    springcloud
    完全二叉树和满二叉树
    C# 读取EXCEL文件的三种经典方法
    C#加密app.config中连接字符串的代码
    c#winform 程序 App.config文件加密(SDK命令)
  • 原文地址:https://www.cnblogs.com/lightlfyan/p/4229200.html
Copyright © 2011-2022 走看看