码迷,mamicode.com
首页 > 编程语言 > 详细

c++ 行为型模式_观察者(Observer)

时间:2020-01-28 19:37:10      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:cond   其他   div   pre   依赖   begin   span   end   public   

1) 意图

定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新

2) 结构

 技术图片

 

 Subject提供注册和删除观测者的接口

  • Observer 定义一个抽象的更新接口
  • ConcreteSubject 是实际被观测的对象,当状态改变时,通知各个观测者
  • ConcreteObserver 实际观测者

3) 适用性

  1. 当一个抽象模型有两个方面,其中一个方面依赖于另一个方面,将这两者封装在独立的对象中以使它们可以各自独立地改变和复用
  2. 当对一个对象的改变需要同时改变其他对象,而不知道具体有多少对象有待改变时
  3. 当一个对象必须通知其他对象,而它又不能假定其他对象是谁,即不希望这些对象是紧耦合的

4) 举例

  1 #include <iostream>
  2 #include <list>
  3 typedef std::string STATE;
  4 class Observer
  5 {
  6 public:
  7     Observer() {}
  8     virtual ~Observer() {}
  9     virtual void Update(STATE& state) = 0;
 10 };
 11 class ConcreateObserverA : public Observer
 12 {
 13 public:
 14     ConcreateObserverA():m_state("begin"){}
 15     virtual ~ConcreateObserverA() {}
 16     virtual void Update(STATE& state)
 17     {
 18         std::cout << "ConcreateObserverA Update state from " << m_state.c_str();
 19         m_state = state;
 20         std::cout << " to " <<m_state.c_str()<< std::endl;
 21     }
 22 private:
 23     STATE m_state;
 24 };
 25 class ConcreateObserverB : public Observer
 26 {
 27 public:
 28     ConcreateObserverB() :m_state("begin") {}
 29     virtual ~ConcreateObserverB() {}
 30     virtual void Update(STATE& state)
 31     {
 32         std::cout << "ConcreateObserverB Update state from " << m_state.c_str();
 33         m_state = state;
 34         std::cout << " to " << m_state.c_str() << std::endl;
 35     }
 36 private:
 37     STATE m_state;
 38 };
 39 class Subject
 40 {
 41 public:
 42     Subject() {}
 43     virtual ~Subject() {}
 44     virtual void notify() = 0;
 45     virtual void attach(Observer *pObserver) = 0;
 46     virtual void detach(Observer *pObserver) = 0;
 47     virtual void setState(STATE& state) = 0;
 48 };
 49 class ConcreateSubject:public Subject
 50 {
 51     typedef std::list<Observer*> LIST;
 52 public:
 53     ConcreateSubject(STATE& state):m_state(state) {}
 54     virtual ~ConcreateSubject() 
 55     {
 56         for (LIST::iterator it = m_list.begin();
 57             it != m_list.end(); )
 58         {
 59             delete(*(it++));
 60         }
 61     }
 62     virtual void notify()
 63     {
 64         for (LIST::iterator it = m_list.begin();
 65             it != m_list.end(); ++it)
 66         {
 67             (*it)->Update(m_state);
 68         }
 69     }
 70     virtual void attach(Observer *pObserver)
 71     {
 72         m_list.push_back(pObserver);
 73         std::cout << "attach an Observer" << std::endl;
 74     }
 75     virtual void detach(Observer *pObserver)
 76     {
 77         LIST::iterator it = std::find(m_list.begin(), m_list.end(), pObserver);
 78         if (m_list.end() != it)
 79         {
 80             m_list.erase(it);
 81         }
 82         std::cout << "detach an Observer"<<std::endl;
 83     }
 84     void setState(STATE& state)
 85     {
 86         m_state = state;
 87     }
 88 private:
 89     LIST m_list;
 90     STATE m_state;
 91 };
 92 
 93 int main() 
 94 {
 95     Observer* pObserverA = new ConcreateObserverA();
 96     Observer* pObserverB = new ConcreateObserverB();
 97 
 98     Subject* pSubject = new ConcreateSubject(std::string("state first"));
 99     pSubject->attach(pObserverA);
100     pSubject->attach(pObserverB);
101     pSubject->notify();
102     pSubject->setState(std::string("state second"));
103     pSubject->notify();
104     delete pSubject;
105     system("pause");
106 }

 

c++ 行为型模式_观察者(Observer)

标签:cond   其他   div   pre   依赖   begin   span   end   public   

原文地址:https://www.cnblogs.com/ho966/p/12238469.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!