多个主题对象,每个主题对象都有三个观察者,感觉有点问题,说不好。有问题请提出来。
-
- #include <iostream>
- #include <list>
- #include <string>
- using namespace std;
-
- class Subject;
-
- class Observer
- {
- protected:
- string name;
- Subject *subject;
- public:
- Observer(Subject *subject, string name)
- {
- this->subject = subject;
- this->name = name;
- }
- virtual void Update()
- {}
- };
-
- class Subject
- {
- list<Observer*> observers;
-
- public:
-
- void AddObserver(Observer *observer)
- {
- observers.push_back(observer);
- }
-
- void RemoveObserver(Observer *observer)
- {
- observers.remove(observer);
- }
-
- virtual void Notify()
- {
- list<Observer*>::iterator iter = observers.begin();
- for(; iter != observers.end(); iter++)
- {
- (*iter)->Update();
- }
- }
- virtual string GetState()
- {
- return " ";
- }
-
- int GetObserverSize()
- {
- return observers.size();
- }
- };
-
- class ConcreteSubjectOne:public Subject
- {
- string state;
- public:
-
- ConcreteSubjectOne(string state)
- {
- this->state ="One " + state;
- }
- string GetState()
- {
- return this->state;
- }
- };
-
- class ConcreteSubjectTwo:public Subject
- {
- string state;
- public:
-
- ConcreteSubjectTwo(string state)
- {
- this->state ="Two " + state;
- }
- string GetState()
- {
- return this->state;
- }
- };
-
-
- class ConcreteObserverOne:public Observer
- {
- public:
- ConcreteObserverOne(Subject *subject, string name)
- :Observer(subject, name)
- {
- }
- void Update()
- {
- cout<<"Observer Type One: "<< name << " subject state is "<< subject->GetState()<<endl;
- }
-
- };
-
- class ConcreteObserverTwo:public Observer
- {
- public:
- ConcreteObserverTwo(Subject *subject, string name)
- :Observer(subject, name)
- {
- }
- void Update()
- {
- cout<<"Observer Type Two: "<< name << " subject state is "<< subject->GetState()<<endl;
- }
-
- };
-
- int main()
- {
- ConcreteSubjectOne *subject_1 = new ConcreteSubjectOne("subject__1");
- ConcreteSubjectTwo *subject_2 = new ConcreteSubjectTwo("subject__2");
-
- ConcreteObserverOne *observer1 = new ConcreteObserverOne(subject_1, "observer 1");
- ConcreteObserverOne *observer2 = new ConcreteObserverOne(subject_1, "observer 2");
- ConcreteObserverOne *observer3 = new ConcreteObserverOne(subject_1, "observer 3");
-
- ConcreteObserverTwo *observer4 = new ConcreteObserverTwo(subject_2, "observer 4");
- ConcreteObserverTwo *observer5 = new ConcreteObserverTwo(subject_2, "observer 5");
- ConcreteObserverTwo *observer6 = new ConcreteObserverTwo(subject_2, "observer 6");
-
- subject_1->AddObserver(observer1);
- subject_1->AddObserver(observer2);
- subject_1->AddObserver(observer3);
-
- subject_2->AddObserver(observer4);
- subject_2->AddObserver(observer5);
- subject_2->AddObserver(observer6);
-
-
- subject_1->Notify();
- subject_2->Notify();
-
- cout<<subject_1->GetObserverSize()<<endl;
- cout<<subject_2->GetObserverSize()<<endl;
-
- delete subject_1;
- delete subject_2;
-
- delete observer6;
- delete observer5;
- delete observer4;
- delete observer3;
- delete observer2;
- delete observer1;
-
- getchar();
- return 0;
- }