当对象间存在一对多关系时,则使用观察者模式(Observer Pattern)。比如,当一个对象被修改时,则会自动通知依赖它的对象。观察者模式属于行为型模式。
1.观察者模式解决一对多的问题,例如一个模块发生改变时,要通知其他的模块;
2.被观察者(observable)/主题(subject)中包含所有的观察者(observer);
3.观察者通过被观察者提供的接口(addObserver)添加自己到被观察者的观察者列表中;
4.观察者提供一个 update 接口供被观察者调用,已便在发生改变时通知观察者。
5.避免观察者和被观察者循环应用导致系统崩溃。
observer.h
1 #ifndef __OBSERVER_H
2 #define __OBSERVER_H
3
4 typedef struct subject Subject;
5 typedef struct observer Observer;
6
7 struct subject {
8 Observer *observer_list;
9 void (*add_observer)(Subject *sub,Observer *obs);
10 void (*delete_observer)(Subject *sub,Observer *obs);
11 int (*count_observers)(Subject *sub);
12 void (*notify_observer)(Subject *sub,Observer *obs,void *arg);
13 void (*notify_all_observers)(Subject *sub,void *arg);
14 };
15
16 struct observer {
17 void (*update)(Observer *obs,Subject *sub,void *arg);
18 Observer *next;
19 };
20
21 void subjectInit(Subject *sub);
22
23 void observerInit(Observer *obs);
24
25 #endif
observer.c
1 #include
2 #include "observer.h"
3
4 static void
5 _subjectAddObserver(Subject *sub,Observer *obs)
6 {
7 Observer *_obs = sub->observer_list;
8 obs->next = NULL;
9
10 if(_obs == NULL) {
11 sub->observer_list = obs;
12 return;
13 }
14
15 while(_obs->next != NULL)
16 _obs = _obs->next;
17
18 _obs->next = obs;
19 }
20
21 static void
22 _subjectDeleteObserver(Subject *sub,Observer *obs)
23 {
24 Observer *_obs = sub->observer_list;
25
26 if(_obs == NULL) return;
27
28 if(_obs == obs) {
29 sub->observer_list = _obs->next;
30 return;
31 }
32
33 while(_obs->next != NULL) {
34 if(_obs->next == obs) {
35 _obs->next = obs->next;
36 return;
37 }
38
39 _obs = _obs->next;
40 }
41 }
42
43 static int
44 _subjectCountObservers(Subject *sub)
45 {
46 int cnt = 0;
47 Observer *_obs = sub->observer_list;
48
49 while(_obs != NULL) {
50 cnt ++;
51 _obs = _obs->next;
52 }
53
54 return cnt;
55 }
56
57 static void
58 _subjectNotifyObserver(Subject *sub,Observer *obs,void *arg)
59 {
60 obs->update(obs,sub,arg);
61 }
62
63 static void
64 _subjectNotifyAllObservers(Subject *sub,void *arg)
65 {
66 Observer *_obs = sub->observer_list;
67
68 while(_obs != NULL) {
69 _obs->update(_obs,sub,arg);
70 _obs = _obs->next;
71 }
72 }
73
74 void
75 subjectInit(Subject *sub)
76 {
77 sub->observer_list = NULL;
78 sub->add_observer = _subjectAddObserver;
79 sub->delete_observer = _subjectDeleteObserver;
80 sub->count_observers = _subjectCountObservers;
81 sub->notify_observer = _subjectNotifyObserver;
82 sub->notify_all_observers = _subjectNotifyAllObservers;
83 }
84
85 static void
86 _observerUpdate(Observer *obs,Subject *sub,void *arg)
87 {
88 (void)obs;
89 (void)sub;
90 (void)arg;
91
92 /** You should override the update function **/
93 while(1) {
94 ;
95 }
96 }
97
98 void
99 observerInit(Observer *obs)
100 {
101 obs->update = _observerUpdate;
102 obs->next = NULL;
103 }
实体观察者类
1 struct observerExtend {
2 Observer obs;
3 Subject *sub;
4 int id;
5 };
6
7 typedef struct observerExtend ObserverExtend;
8
9 void obExtdinit(ObserverExtend *extd);
1 #include
2 #include "observer_extend.h"
3
4 static void
5 update(Observer *obs,Subject *sub,void *arg)
6 {
7 ObserverExtend *extd = (ObserverExtend *)obs;
8 printf("observer %d, arg %d\r\n",extd->id, arg);
9 }
10
11 void
12 obExtdinit(ObserverExtend *extd)
13 {
14 observerInit(&extd->obs);
15 extd->obs.update = update;
16 extd->sub->add_observer(extd->sub, &extd->obs);
17 }
手机扫一扫
移动阅读更方便
你可能感兴趣的文章