设计模式之观察者模式_C++

  1 // ADBHelper.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2 //
  3 
  4 //#include "pch.h"
  5 #include <stdafx.h>
  6 #include <iostream>
  7 #include <list>
  8 using namespace std;
  9 
 10 class Observer
 11 {
 12 public:
 13     virtual void Update(int) = 0;
 14 };
 15 
 16 class Subject
 17 {
 18 public:
 19     virtual void Attach(Observer *) = 0;
 20     virtual void Detach(Observer *) = 0;
 21     virtual void Notify() = 0;
 22 };
 23 
 24 class ConcreteObserver : public Observer
 25 {
 26 public:
 27     ConcreteObserver(Subject *pSubject) : m_pSubject(pSubject) {}
 28 
 29     void Update(int value)
 30     {
 31         cout << "ConcreteObserver get the update. New State:" << value << endl;
 32     }
 33 
 34 private:
 35     Subject *m_pSubject;
 36 };
 37 
 38 class ConcreteObserver2 : public Observer
 39 {
 40 public:
 41     ConcreteObserver2(Subject *pSubject) : m_pSubject(pSubject) {}
 42 
 43     void Update(int value)
 44     {
 45         cout << "ConcreteObserver2 get the update. New State:" << value << endl;
 46     }
 47 
 48 private:
 49     Subject *m_pSubject;
 50 };
 51 
 52 class ConcreteSubject : public Subject
 53 {
 54 public:
 55     void Attach(Observer *pObserver);
 56     void Detach(Observer *pObserver);
 57     void Notify();
 58 
 59     void SetState(int state)
 60     {
 61         m_iState = state;
 62     }
 63 
 64 private:
 65     std::list<Observer *> m_ObserverList;
 66     int m_iState;
 67 };
 68 
 69 void ConcreteSubject::Attach(Observer *pObserver)
 70 {
 71     m_ObserverList.push_back(pObserver);
 72 }
 73 
 74 void ConcreteSubject::Detach(Observer *pObserver)
 75 {
 76     m_ObserverList.remove(pObserver);
 77 }
 78 
 79 void ConcreteSubject::Notify()
 80 {
 81     std::list<Observer *>::iterator it = m_ObserverList.begin();
 82     while (it != m_ObserverList.end())
 83     {
 84         (*it)->Update(m_iState);
 85         ++it;
 86     }
 87 }
 88 
 89 int main()
 90 {
 91     // Create Subject
 92     ConcreteSubject *pSubject = new ConcreteSubject();
 93 
 94     // Create Observer
 95     Observer *pObserver = new ConcreteObserver(pSubject);
 96     Observer *pObserver2 = new ConcreteObserver2(pSubject);
 97 
 98     // Change the state
 99     pSubject->SetState(2);
100 
101     // Register the observer
102     pSubject->Attach(pObserver);
103     pSubject->Attach(pObserver2);
104 
105     pSubject->Notify();
106 
107     // Unregister the observer
108     pSubject->Detach(pObserver);
109 
110     pSubject->SetState(3);
111     pSubject->Notify();
112 
113     delete pObserver;
114     delete pObserver2;
115     delete pSubject;
116 }
117 
118 // Run program: Ctrl + F5 or Debug > Start Without Debugging menu
119 // Debug program: F5 or Debug > Start Debugging menu
120 
121 // Tips for Getting Started: 
122 //   1. Use the Solution Explorer window to add/manage files
123 //   2. Use the Team Explorer window to connect to source control
124 //   3. Use the Output window to see build output and other messages
125 //   4. Use the Error List window to view errors
126 //   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
127 //   6. In the future, to open this project again, go to File > Open > Project and select the .sln file

转载:https://www.cnblogs.com/carsonzhu/p/5770253.html

未完,待描述……

posted @ 2021-01-12 15:24  YangARTuan  阅读(111)  评论(0编辑  收藏  举报