设计模式 - Observer 模式(观察者模式)
作用:定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新。
UML结构图:
解析:
Observer模式定义的是一种一对多的关系,这里的一就是图中的Subject类,而多则是Obesrver类,当Subject类的状态发生变化的时候通知与之对应的Obesrver类们也去相应的更新状态,同时支持动态的添加和删除Observer对象的功能.Obesrver模式的实现要点是,第一一般subject类都是采用链表等容器来存放Observer对象,第二抽取出Observer对象的一些公共的属性形成Observer基类,而Subject中保存的则是Observer类对象的指针,这样就使Subject和具体的Observer实现了解耦,也就是Subject不需要去关心到底是哪个Observer对放进了自己的容器中.生活中有很多例子可以看做是Observer模式的运用,比方说,一个班有一个班主任(Subject),他管理手下的一帮学生(Observer),当班里有一些事情发生需要通知学生的时候,班主任要做的不是逐个学生挨个的通知而是把学生召集起来一起通知,实现了班主任和具体学生的关系解耦。
代码实现:
Observer.h
1
2 #ifndef OBSERVER_H
3 #define OBSERVER_H
4
5 #include <list>
6
7 typedef int STATE;
8
9 class Observer;
10
11 // Subject抽象基类,只需要知道Observer基类的声明就可以了
12 class Subject
13 {
14 public:
15 Subject() : m_nSubjectState(-1){}
16 virtual ~Subject();
17
18 void Notify(); // 通知对象改变状态
19 void Attach(Observer *pObserver); // 新增对象
20 void Detach(Observer *pObserver); // 删除对象
21
22 // 虚函数,提供默认的实现,派生类可以自己实现来覆盖基类的实现
23 virtual void SetState(STATE nState); // 设置状态
24 virtual STATE GetState(); // 得到状态
25
26 protected:
27 STATE m_nSubjectState; // 模拟保存Subject状态的变量
28 std::list<Observer*> m_ListObserver; // 保存Observer指针的链表
29 };
30
31 // Observer抽象基类
32 class Observer
33 {
34 public:
35 Observer() : m_nObserverState(-1){}
36 virtual ~Observer(){}
37
38 // 纯虚函数,各个派生类可能有不同的实现
39 // 通知Observer状态发生了变化
40 virtual void Update(Subject* pSubject) = 0;
41
42 protected:
43 STATE m_nObserverState; // 模拟保存Observer状态的变量
44 };
45
46 // ConcreateSubject类,派生在Subject类
47 class ConcreateSubject
48 : public Subject
49 {
50 public:
51 ConcreateSubject() : Subject(){}
52 virtual ~ConcreateSubject(){}
53
54 // 派生类自己实现来覆盖基类的实现
55 virtual void SetState(STATE nState); // 设置状态
56 virtual STATE GetState(); // 得到状态
57
58 };
59
60 // ConcreateObserver类派生自Observer
61 class ConcreateObserver
62 : public Observer
63 {
64 public:
65 ConcreateObserver() : Observer(){}
66 virtual ~ConcreateObserver(){}
67
68 // 虚函数,实现基类提供的接口
69 virtual void Update(Subject* pSubject);
70 };
71
72 #endif
73
2 #ifndef OBSERVER_H
3 #define OBSERVER_H
4
5 #include <list>
6
7 typedef int STATE;
8
9 class Observer;
10
11 // Subject抽象基类,只需要知道Observer基类的声明就可以了
12 class Subject
13 {
14 public:
15 Subject() : m_nSubjectState(-1){}
16 virtual ~Subject();
17
18 void Notify(); // 通知对象改变状态
19 void Attach(Observer *pObserver); // 新增对象
20 void Detach(Observer *pObserver); // 删除对象
21
22 // 虚函数,提供默认的实现,派生类可以自己实现来覆盖基类的实现
23 virtual void SetState(STATE nState); // 设置状态
24 virtual STATE GetState(); // 得到状态
25
26 protected:
27 STATE m_nSubjectState; // 模拟保存Subject状态的变量
28 std::list<Observer*> m_ListObserver; // 保存Observer指针的链表
29 };
30
31 // Observer抽象基类
32 class Observer
33 {
34 public:
35 Observer() : m_nObserverState(-1){}
36 virtual ~Observer(){}
37
38 // 纯虚函数,各个派生类可能有不同的实现
39 // 通知Observer状态发生了变化
40 virtual void Update(Subject* pSubject) = 0;
41
42 protected:
43 STATE m_nObserverState; // 模拟保存Observer状态的变量
44 };
45
46 // ConcreateSubject类,派生在Subject类
47 class ConcreateSubject
48 : public Subject
49 {
50 public:
51 ConcreateSubject() : Subject(){}
52 virtual ~ConcreateSubject(){}
53
54 // 派生类自己实现来覆盖基类的实现
55 virtual void SetState(STATE nState); // 设置状态
56 virtual STATE GetState(); // 得到状态
57
58 };
59
60 // ConcreateObserver类派生自Observer
61 class ConcreateObserver
62 : public Observer
63 {
64 public:
65 ConcreateObserver() : Observer(){}
66 virtual ~ConcreateObserver(){}
67
68 // 虚函数,实现基类提供的接口
69 virtual void Update(Subject* pSubject);
70 };
71
72 #endif
73
Observer.cpp
1
2 #include "Observer.h"
3 #include <iostream>
4 #include <algorithm>
5
6 /* --------------------------------------------------------------------
7 | Subject类成员函数的实现
8 |
9 ----------------------------------------------------------------------*/
10
11 void Subject::Attach(Observer *pObserver)
12 {
13 std::cout << "Attach an Observer\n";
14
15 m_ListObserver.push_back(pObserver);
16 }
17
18 void Subject::Detach(Observer *pObserver)
19 {
20 std::list<Observer*>::iterator iter;
21 iter = std::find(m_ListObserver.begin(), m_ListObserver.end(), pObserver);
22
23 if (m_ListObserver.end() != iter)
24 {
25 m_ListObserver.erase(iter);
26 }
27
28 std::cout << "Detach an Observer\n";
29 }
30
31 void Subject::Notify()
32 {
33 std::cout << "Notify Observers's State\n";
34
35 std::list<Observer*>::iterator iter1, iter2;
36
37 for (iter1 = m_ListObserver.begin(), iter2 = m_ListObserver.end();
38 iter1 != iter2;
39 ++iter1)
40 {
41 (*iter1)->Update(this);
42 }
43 }
44
45 void Subject::SetState(STATE nState)
46 {
47 std::cout << "SetState By Subject\n";
48 m_nSubjectState = nState;
49 }
50
51 STATE Subject::GetState()
52 {
53 std::cout << "GetState By Subject\n";
54 return m_nSubjectState;
55 }
56
57 Subject::~Subject()
58 {
59 std::list<Observer*>::iterator iter1, iter2, temp;
60
61 for (iter1 = m_ListObserver.begin(), iter2 = m_ListObserver.end();
62 iter1 != iter2;
63 )
64 {
65 temp = iter1;
66 ++iter1;
67 delete (*temp);
68 }
69
70 m_ListObserver.clear();
71 }
72
73 /* --------------------------------------------------------------------
74 | ConcreateSubject类成员函数的实现
75 |
76 ----------------------------------------------------------------------*/
77 void ConcreateSubject::SetState(STATE nState)
78 {
79 std::cout << "SetState By ConcreateSubject\n";
80 m_nSubjectState = nState;
81 }
82
83 STATE ConcreateSubject::GetState()
84 {
85 std::cout << "GetState By ConcreateSubject\n";
86 return m_nSubjectState;
87 }
88
89 /* --------------------------------------------------------------------
90 | ConcreateObserver类成员函数的实现
91 |
92 ----------------------------------------------------------------------*/
93 void ConcreateObserver::Update(Subject* pSubject)
94 {
95 if (NULL == pSubject)
96 return;
97
98 m_nObserverState = pSubject->GetState();
99
100 std::cout << "The ObeserverState is " << m_nObserverState << std::endl;
101 }
102
2 #include "Observer.h"
3 #include <iostream>
4 #include <algorithm>
5
6 /* --------------------------------------------------------------------
7 | Subject类成员函数的实现
8 |
9 ----------------------------------------------------------------------*/
10
11 void Subject::Attach(Observer *pObserver)
12 {
13 std::cout << "Attach an Observer\n";
14
15 m_ListObserver.push_back(pObserver);
16 }
17
18 void Subject::Detach(Observer *pObserver)
19 {
20 std::list<Observer*>::iterator iter;
21 iter = std::find(m_ListObserver.begin(), m_ListObserver.end(), pObserver);
22
23 if (m_ListObserver.end() != iter)
24 {
25 m_ListObserver.erase(iter);
26 }
27
28 std::cout << "Detach an Observer\n";
29 }
30
31 void Subject::Notify()
32 {
33 std::cout << "Notify Observers's State\n";
34
35 std::list<Observer*>::iterator iter1, iter2;
36
37 for (iter1 = m_ListObserver.begin(), iter2 = m_ListObserver.end();
38 iter1 != iter2;
39 ++iter1)
40 {
41 (*iter1)->Update(this);
42 }
43 }
44
45 void Subject::SetState(STATE nState)
46 {
47 std::cout << "SetState By Subject\n";
48 m_nSubjectState = nState;
49 }
50
51 STATE Subject::GetState()
52 {
53 std::cout << "GetState By Subject\n";
54 return m_nSubjectState;
55 }
56
57 Subject::~Subject()
58 {
59 std::list<Observer*>::iterator iter1, iter2, temp;
60
61 for (iter1 = m_ListObserver.begin(), iter2 = m_ListObserver.end();
62 iter1 != iter2;
63 )
64 {
65 temp = iter1;
66 ++iter1;
67 delete (*temp);
68 }
69
70 m_ListObserver.clear();
71 }
72
73 /* --------------------------------------------------------------------
74 | ConcreateSubject类成员函数的实现
75 |
76 ----------------------------------------------------------------------*/
77 void ConcreateSubject::SetState(STATE nState)
78 {
79 std::cout << "SetState By ConcreateSubject\n";
80 m_nSubjectState = nState;
81 }
82
83 STATE ConcreateSubject::GetState()
84 {
85 std::cout << "GetState By ConcreateSubject\n";
86 return m_nSubjectState;
87 }
88
89 /* --------------------------------------------------------------------
90 | ConcreateObserver类成员函数的实现
91 |
92 ----------------------------------------------------------------------*/
93 void ConcreateObserver::Update(Subject* pSubject)
94 {
95 if (NULL == pSubject)
96 return;
97
98 m_nObserverState = pSubject->GetState();
99
100 std::cout << "The ObeserverState is " << m_nObserverState << std::endl;
101 }
102
Main.cpp
1
2 #include "Observer.h"
3 #include <iostream>
4
5 int main()
6 {
7 Observer *p1 = new ConcreateObserver;
8 Observer *p2 = new ConcreateObserver;
9
10 Subject* p = new ConcreateSubject;
11 p->Attach(p1);
12 p->Attach(p2);
13 p->SetState(4);
14 p->Notify();
15
16 p->Detach(p1);
17 p->SetState(10);
18 p->Notify();
19
20 delete p;
21
22 system("pause");
23
24 return 0;
25 }
2 #include "Observer.h"
3 #include <iostream>
4
5 int main()
6 {
7 Observer *p1 = new ConcreateObserver;
8 Observer *p2 = new ConcreateObserver;
9
10 Subject* p = new ConcreateSubject;
11 p->Attach(p1);
12 p->Attach(p2);
13 p->SetState(4);
14 p->Notify();
15
16 p->Detach(p1);
17 p->SetState(10);
18 p->Notify();
19
20 delete p;
21
22 system("pause");
23
24 return 0;
25 }