观察者模式是设计模式中行为模型的一种,是定义对象间的一种一对多的依赖关系,以便当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并自动刷新。
典型的应用情形为:
夜里有一只猫大叫了一声,同处一屋的老鼠接受到了猫大叫的信息,于是开始逃跑。同样,主人听到了,被吵醒了;小孩子听到了,被吓哭了。
实现代码如下:
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
using System;
2![](/Images/OutliningIndicators/None.gif)
3
namespace DelegateEvent
4![](/Images/OutliningIndicators/None.gif)
5![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
6![](/Images/OutliningIndicators/InBlock.gif)
7![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
8![](/Images/OutliningIndicators/InBlock.gif)
9
/// Subject 的摘要说明。
10![](/Images/OutliningIndicators/InBlock.gif)
11
/// 被观察者抽象基类
12![](/Images/OutliningIndicators/InBlock.gif)
13
/// 所有被观察者对象都继承此类
14![](/Images/OutliningIndicators/InBlock.gif)
15
/// 做为抽象基类,此类不允许直接进行实例化
16![](/Images/OutliningIndicators/InBlock.gif)
17
/// 此类中首先定义委托
18![](/Images/OutliningIndicators/InBlock.gif)
19
/// 再有委托定义事件
20![](/Images/OutliningIndicators/InBlock.gif)
21
/// 然后创立与委托想关联的方法Notify
22![](/Images/OutliningIndicators/InBlock.gif)
23
/// </summary>
24![](/Images/OutliningIndicators/InBlock.gif)
25
public abstract class Subject
26![](/Images/OutliningIndicators/InBlock.gif)
27![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
28![](/Images/OutliningIndicators/InBlock.gif)
29
public Subject()
30![](/Images/OutliningIndicators/InBlock.gif)
31![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
32![](/Images/OutliningIndicators/InBlock.gif)
33
//
34![](/Images/OutliningIndicators/InBlock.gif)
35
// TODO: 在此处添加构造函数逻辑
36![](/Images/OutliningIndicators/InBlock.gif)
37
//
38![](/Images/OutliningIndicators/InBlock.gif)
39
}
40![](/Images/OutliningIndicators/InBlock.gif)
41![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
42![](/Images/OutliningIndicators/InBlock.gif)
43
/// 创建一个委托
44![](/Images/OutliningIndicators/InBlock.gif)
45
/// </summary>
46![](/Images/OutliningIndicators/InBlock.gif)
47
public delegate void SubEventHandler();
48![](/Images/OutliningIndicators/InBlock.gif)
49![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
50![](/Images/OutliningIndicators/InBlock.gif)
51
/// 根据SubEventHandler创建一个事件
52![](/Images/OutliningIndicators/InBlock.gif)
53
/// </summary>
54![](/Images/OutliningIndicators/InBlock.gif)
55
public event SubEventHandler SubEvent;
56![](/Images/OutliningIndicators/InBlock.gif)
57![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
58![](/Images/OutliningIndicators/InBlock.gif)
59
/// 将于委托相连接的方法
60![](/Images/OutliningIndicators/InBlock.gif)
61
/// </summary>
62![](/Images/OutliningIndicators/InBlock.gif)
63
protected void Notify()
64![](/Images/OutliningIndicators/InBlock.gif)
65![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
66![](/Images/OutliningIndicators/InBlock.gif)
67
if(this.SubEvent!=null)
68![](/Images/OutliningIndicators/InBlock.gif)
69
this.SubEvent();
70![](/Images/OutliningIndicators/InBlock.gif)
71
}
72![](/Images/OutliningIndicators/InBlock.gif)
73
}
74![](/Images/OutliningIndicators/InBlock.gif)
75
}
76![](/Images/OutliningIndicators/None.gif)
77
//------------------------------------------------------------------------------------------
78![](/Images/OutliningIndicators/None.gif)
79
被观察者基类完成后我们再来创建观察者基类Observer.
80![](/Images/OutliningIndicators/None.gif)
81
namespace DelegateEvent
82![](/Images/OutliningIndicators/None.gif)
83![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
84![](/Images/OutliningIndicators/InBlock.gif)
85![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
86![](/Images/OutliningIndicators/InBlock.gif)
87
/// Observer 的摘要说明。
88![](/Images/OutliningIndicators/InBlock.gif)
89
/// 观察者抽象基类
90![](/Images/OutliningIndicators/InBlock.gif)
91
/// 所有观察者对象都由此类派生
92![](/Images/OutliningIndicators/InBlock.gif)
93
/// 使用此类进行对事件的注册
94![](/Images/OutliningIndicators/InBlock.gif)
95
/// 并和事件的方法关联
96![](/Images/OutliningIndicators/InBlock.gif)
97
/// 另外定义了一个抽象方法Response
98![](/Images/OutliningIndicators/InBlock.gif)
99
/// 可以由子类来进行覆盖
100![](/Images/OutliningIndicators/InBlock.gif)
101
/// </summary>
102![](/Images/OutliningIndicators/InBlock.gif)
103
public abstract class Observer
104![](/Images/OutliningIndicators/InBlock.gif)
105![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
106![](/Images/OutliningIndicators/InBlock.gif)
107
public Observer(Subject childModel)
108![](/Images/OutliningIndicators/InBlock.gif)
109![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
110![](/Images/OutliningIndicators/InBlock.gif)
111
//注册SubEvent事件通过SubEventHandler委托和Response方法关联
112![](/Images/OutliningIndicators/InBlock.gif)
113
//子类通过调用此构造函数完成事件的注册
114![](/Images/OutliningIndicators/InBlock.gif)
115
childModel.SubEvent+=new Subject.SubEventHandler(Response);
116![](/Images/OutliningIndicators/InBlock.gif)
117
}
118![](/Images/OutliningIndicators/InBlock.gif)
119
120![](/Images/OutliningIndicators/InBlock.gif)
121![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
122![](/Images/OutliningIndicators/InBlock.gif)
123
/// 抽象方法,用于引发事件
124![](/Images/OutliningIndicators/InBlock.gif)
125
/// </summary>
126![](/Images/OutliningIndicators/InBlock.gif)
127
public abstract void Response();
128![](/Images/OutliningIndicators/InBlock.gif)
129
}
130![](/Images/OutliningIndicators/InBlock.gif)
131
}
132![](/Images/OutliningIndicators/None.gif)
133
同理,我们还可以继续创建另一个观察者基类,用来响应不同的事件的方法。
134![](/Images/OutliningIndicators/None.gif)
135
namespace DelegateEvent
136![](/Images/OutliningIndicators/None.gif)
137![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
138![](/Images/OutliningIndicators/InBlock.gif)
139![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
140![](/Images/OutliningIndicators/InBlock.gif)
141
/// Observer2 的摘要说明。
142![](/Images/OutliningIndicators/InBlock.gif)
143
/// 注册了两个事件的方法。
144![](/Images/OutliningIndicators/InBlock.gif)
145
/// </summary>
146![](/Images/OutliningIndicators/InBlock.gif)
147
public abstract class Observer2
148![](/Images/OutliningIndicators/InBlock.gif)
149![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
150![](/Images/OutliningIndicators/InBlock.gif)
151
public Observer2(Subject childModel)
152![](/Images/OutliningIndicators/InBlock.gif)
153![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
154![](/Images/OutliningIndicators/InBlock.gif)
155
childModel.SubEvent+=new Subject.SubEventHandler(Response);
156![](/Images/OutliningIndicators/InBlock.gif)
157
childModel.SubEvent+=new Subject.SubEventHandler(Response2);
158![](/Images/OutliningIndicators/InBlock.gif)
159
}
160![](/Images/OutliningIndicators/InBlock.gif)
161
public abstract void Response();
162![](/Images/OutliningIndicators/InBlock.gif)
163
public abstract void Response2();
164![](/Images/OutliningIndicators/InBlock.gif)
165
}
166![](/Images/OutliningIndicators/InBlock.gif)
167
}
168![](/Images/OutliningIndicators/None.gif)
169
//-------------------------------------------------------------------------------------------------------
170![](/Images/OutliningIndicators/None.gif)
171
现在,我们来针对这个实例中的猫大叫这个引发事件进行解析。
172![](/Images/OutliningIndicators/None.gif)
173
namespace DelegateEvent
174![](/Images/OutliningIndicators/None.gif)
175![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
176![](/Images/OutliningIndicators/InBlock.gif)
177![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
178![](/Images/OutliningIndicators/InBlock.gif)
179
/// Cat 的摘要说明。
180![](/Images/OutliningIndicators/InBlock.gif)
181
/// 此类作为被观察者对象
182![](/Images/OutliningIndicators/InBlock.gif)
183
/// 直接继承Subject类
184![](/Images/OutliningIndicators/InBlock.gif)
185
/// 使用一个Cry方法,调用Notify方法起用先前定义的SubEvent事件
186![](/Images/OutliningIndicators/InBlock.gif)
187
/// </summary>
188![](/Images/OutliningIndicators/InBlock.gif)
189
public class Cat:Subject
190![](/Images/OutliningIndicators/InBlock.gif)
191![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
192![](/Images/OutliningIndicators/InBlock.gif)
193
public Cat()
194![](/Images/OutliningIndicators/InBlock.gif)
195![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
196![](/Images/OutliningIndicators/InBlock.gif)
197
//
198![](/Images/OutliningIndicators/InBlock.gif)
199
// TODO: 在此处添加构造函数逻辑
200![](/Images/OutliningIndicators/InBlock.gif)
201
//
202![](/Images/OutliningIndicators/InBlock.gif)
203
}
204![](/Images/OutliningIndicators/InBlock.gif)
205
public void Cry()
206![](/Images/OutliningIndicators/InBlock.gif)
207![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
208![](/Images/OutliningIndicators/InBlock.gif)
209
System.Console.WriteLine("Cat Cry
..");
210![](/Images/OutliningIndicators/InBlock.gif)
211
//调用从ModelBase继承过来的Notify()
212![](/Images/OutliningIndicators/InBlock.gif)
213
this.Notify();
214![](/Images/OutliningIndicators/InBlock.gif)
215
}
216![](/Images/OutliningIndicators/InBlock.gif)
217
}
218![](/Images/OutliningIndicators/InBlock.gif)
219
}
220![](/Images/OutliningIndicators/None.gif)
221
// 这样一个被观察者对象就完成了。
222![](/Images/OutliningIndicators/None.gif)
223
//--------------------------------------------------------------------------------------------------------------------
224![](/Images/OutliningIndicators/None.gif)
225
被观察者对象有了,我们再来创建具体的观察者对象。此例中,有主人,小孩和老鼠对猫的叫声做出了反应,因此我们可以创建三个类,分别对主人、小孩和老鼠进行响应。其中,主人和老鼠对应Observer类,响应其中的一个事件;而小孩则继承Observer2类,响应其中的两个事件。
226![](/Images/OutliningIndicators/None.gif)
227
//------------------------------------观察者--主人类---------------------------------------------------
228![](/Images/OutliningIndicators/None.gif)
229
namespace DelegateEvent
230![](/Images/OutliningIndicators/None.gif)
231![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
232![](/Images/OutliningIndicators/InBlock.gif)
233![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
234![](/Images/OutliningIndicators/InBlock.gif)
235
/// Master 的摘要说明。
236![](/Images/OutliningIndicators/InBlock.gif)
237
/// </summary>
238![](/Images/OutliningIndicators/InBlock.gif)
239
public class Master:Observer
240![](/Images/OutliningIndicators/InBlock.gif)
241![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
242![](/Images/OutliningIndicators/InBlock.gif)
243![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
244![](/Images/OutliningIndicators/InBlock.gif)
245
/// 构造函数,接受一个Cat类型的对象childModel并强制转换为基类ModelBase变量
246![](/Images/OutliningIndicators/InBlock.gif)
247
/// 再将childModel传入到父类Observer的构造函数当中,实现注册。
248![](/Images/OutliningIndicators/InBlock.gif)
249
/// </summary>
250![](/Images/OutliningIndicators/InBlock.gif)
251
/// <param name="childModel"></param>
252![](/Images/OutliningIndicators/InBlock.gif)
253
public Master(Subject childModel):base(childModel)
254![](/Images/OutliningIndicators/InBlock.gif)
255![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
256![](/Images/OutliningIndicators/InBlock.gif)
257
258![](/Images/OutliningIndicators/InBlock.gif)
259
}
260![](/Images/OutliningIndicators/InBlock.gif)
261
public override void Response()
262![](/Images/OutliningIndicators/InBlock.gif)
263![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
264![](/Images/OutliningIndicators/InBlock.gif)
265
System.Console.WriteLine("主人醒来");
266![](/Images/OutliningIndicators/InBlock.gif)
267
}
268![](/Images/OutliningIndicators/InBlock.gif)
269
}
270![](/Images/OutliningIndicators/InBlock.gif)
271
}
272![](/Images/OutliningIndicators/None.gif)
273
//------------------------------------观察者--老鼠类-----------------------------------------------------
274![](/Images/OutliningIndicators/None.gif)
275
namespace DelegateEvent
276![](/Images/OutliningIndicators/None.gif)
277![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
278![](/Images/OutliningIndicators/InBlock.gif)
279![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
280![](/Images/OutliningIndicators/InBlock.gif)
281
/// Mouse 的摘要说明。
282![](/Images/OutliningIndicators/InBlock.gif)
283
/// </summary>
284![](/Images/OutliningIndicators/InBlock.gif)
285
public class Mouse:Observer
286![](/Images/OutliningIndicators/InBlock.gif)
287![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
288![](/Images/OutliningIndicators/InBlock.gif)
289
private string name;
290![](/Images/OutliningIndicators/InBlock.gif)
291
public Mouse(string name, Subject childModel):base(childModel)
292![](/Images/OutliningIndicators/InBlock.gif)
293![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
294![](/Images/OutliningIndicators/InBlock.gif)
295
this.name=name;
296![](/Images/OutliningIndicators/InBlock.gif)
297
}
298![](/Images/OutliningIndicators/InBlock.gif)
299
300![](/Images/OutliningIndicators/InBlock.gif)
301
//覆盖Observer类Response方法
302![](/Images/OutliningIndicators/InBlock.gif)
303
public override void Response()
304![](/Images/OutliningIndicators/InBlock.gif)
305![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
306![](/Images/OutliningIndicators/InBlock.gif)
307
System.Console.WriteLine(this.name+"开始逃跑");
308![](/Images/OutliningIndicators/InBlock.gif)
309
}
310![](/Images/OutliningIndicators/InBlock.gif)
311
}
312![](/Images/OutliningIndicators/InBlock.gif)
313
}
314![](/Images/OutliningIndicators/None.gif)
315
//于主人类不同的是,老鼠类的构造函数可以接受一个字符串参数,这样可以变的更多样化
316![](/Images/OutliningIndicators/None.gif)
317
//------------------------------------观察者--小孩------------------------------------------------------------
318![](/Images/OutliningIndicators/None.gif)
319
namespace DelegateEvent
320![](/Images/OutliningIndicators/None.gif)
321![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
322![](/Images/OutliningIndicators/InBlock.gif)
323![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
324![](/Images/OutliningIndicators/InBlock.gif)
325
/// Child 的摘要说明。
326![](/Images/OutliningIndicators/InBlock.gif)
327
/// </summary>
328![](/Images/OutliningIndicators/InBlock.gif)
329
public class Child:Observer2
330![](/Images/OutliningIndicators/InBlock.gif)
331![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
332![](/Images/OutliningIndicators/InBlock.gif)
333
public Child(Subject childBase):base(childBase)
334![](/Images/OutliningIndicators/InBlock.gif)
335![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
336![](/Images/OutliningIndicators/InBlock.gif)
337
338![](/Images/OutliningIndicators/InBlock.gif)
339
}
340![](/Images/OutliningIndicators/InBlock.gif)
341
public override void Response()
342![](/Images/OutliningIndicators/InBlock.gif)
343![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
344![](/Images/OutliningIndicators/InBlock.gif)
345
Console.WriteLine("baby醒来。。。。");
346![](/Images/OutliningIndicators/InBlock.gif)
347
}
348![](/Images/OutliningIndicators/InBlock.gif)
349
public override void Response2()
350![](/Images/OutliningIndicators/InBlock.gif)
351![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
352![](/Images/OutliningIndicators/InBlock.gif)
353
Console.WriteLine("开始哭闹。。。。。");
354![](/Images/OutliningIndicators/InBlock.gif)
355
}
356![](/Images/OutliningIndicators/InBlock.gif)
357
}
358![](/Images/OutliningIndicators/InBlock.gif)
359
}
360![](/Images/OutliningIndicators/None.gif)
361
//小孩类里,由于继承的是Observer2类,因此可以响应两种不同的方法。
362![](/Images/OutliningIndicators/None.gif)
363
在主函数里,定义每个观察者子类的对象,由其构造函数接受被观察者对象进行事件的响应。
364![](/Images/OutliningIndicators/None.gif)
365
namespace DelegateEvent
366![](/Images/OutliningIndicators/None.gif)
367![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
368![](/Images/OutliningIndicators/InBlock.gif)
369![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
370![](/Images/OutliningIndicators/InBlock.gif)
371
/// SubMain 的摘要说明。
372![](/Images/OutliningIndicators/InBlock.gif)
373
/// </summary>
374![](/Images/OutliningIndicators/InBlock.gif)
375
public class SubMain
376![](/Images/OutliningIndicators/InBlock.gif)
377![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
378![](/Images/OutliningIndicators/InBlock.gif)
379
public SubMain()
380![](/Images/OutliningIndicators/InBlock.gif)
381![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
382![](/Images/OutliningIndicators/InBlock.gif)
383
384![](/Images/OutliningIndicators/InBlock.gif)
385
}
386![](/Images/OutliningIndicators/InBlock.gif)
387![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
388![](/Images/OutliningIndicators/InBlock.gif)
389
/// 主函数,定义子类对象
390![](/Images/OutliningIndicators/InBlock.gif)
391
/// </summary>
392![](/Images/OutliningIndicators/InBlock.gif)
393
public static void Main()
394![](/Images/OutliningIndicators/InBlock.gif)
395![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
396![](/Images/OutliningIndicators/InBlock.gif)
397
Cat myCat=new Cat();
398![](/Images/OutliningIndicators/InBlock.gif)
399
Mouse myMouse1=new Mouse("mouse1",myCat);
400![](/Images/OutliningIndicators/InBlock.gif)
401
Mouse myMouse2=new Mouse("mouse2",myCat);
402![](/Images/OutliningIndicators/InBlock.gif)
403
Master myMaster=new Master(myCat);
404![](/Images/OutliningIndicators/InBlock.gif)
405
406![](/Images/OutliningIndicators/InBlock.gif)
407
Child myLittleMaster=new Child(myCat);
408![](/Images/OutliningIndicators/InBlock.gif)
409
myCat.Cry();
410![](/Images/OutliningIndicators/InBlock.gif)
411
}
412![](/Images/OutliningIndicators/InBlock.gif)
413
}
414![](/Images/OutliningIndicators/InBlock.gif)
415
}
416![](/Images/OutliningIndicators/None.gif)
417![](/Images/OutliningIndicators/None.gif)
改变一下应用环境:
老鼠偷油,猫来捉老鼠,老鼠吓跑,打碎了油瓶,人被吵醒。
这样的一个环境下,如何使用观察者模式呢?
首先区分,谁是一整串事件的引发者,可以分析是老鼠,老鼠偷油这件事情引发了一系列事件。
猫作为老鼠的观察者,当老鼠偷油这件事情发生以后,触发猫捉老鼠的事件,而下面思维开始混乱,因为老鼠逃跑,打翻油瓶这件事按理论来说应该不是直接由老鼠偷油这件事情引发的,而是由猫捉老鼠这件事情。因此在猫捉老鼠,老鼠逃跑的事件中,又似乎是猫是被观察者,而老鼠是观察者。但这样理解的话,是不是就形成一种循环了亚。
但细细想来,上面这个问题又是自欺欺人,因为首先观察者模式是希望实现一个对象的状态发生改变时,所有依赖于它的对象都得到通知并自动刷新。
从上面情形来看,老鼠逃跑只是这个链上的一个节点,而这个节点的事件发起人正是被观察者,因此又出现下列问题,被观察者能否依赖于其本身呢?只要是能依赖,这样一个情形,使用观察者模性就是合理的。但是假使这样可行的话,那么观察者和被观察者之间是否又没有了清晰的界限,加大了对象之间的耦合关系,这是不是又违背OO思想?希望大家给点意见。
上述情形实现代码如下:
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4![](/Images/OutliningIndicators/None.gif)
5
namespace ConsoleApplication3
6![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
7![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
8
/// 被观察者的抽象类
9
/// </summary>
10
public class Model
11![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
12![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
13
/// 定义一个返回为空的委托
14
/// </summary>
15
public delegate void ModelDelegate();
16![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
17
/// 通知事件
18
/// </summary>
19
event ModelDelegate _notifyEvent;
20![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
21
/// 通知事件
22
/// </summary>
23
public event ModelDelegate NotifyEvent
24![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
25
add
26![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
27
this._notifyEvent += value;
28
}
29
remove
30![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
31
this._notifyEvent -= value;
32
}
33
}
34![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
35
/// 通知方法
36
/// </summary>
37
public void Notify()
38![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
39
this._notifyEvent();
40
}
41
List<Observer> _observers = new List<Observer>();
42![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
43
/// 构造
44
/// </summary>
45
public Model()
46![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
47![](/Images/OutliningIndicators/InBlock.gif)
48
}
49![](/Images/OutliningIndicators/InBlock.gif)
50![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
51
/// 观察者集合
52
/// </summary>
53
public List<Observer> ObserversCollection
54![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
55
get
56![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
57
return _observers;
58
}
59
set
60![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
61
_observers = value;
62
}
63
}
64![](/Images/OutliningIndicators/InBlock.gif)
65![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
66
/// 注册观察者
67
/// </summary>
68
/// <param name="observer"></param>
69
public void RegisterObserver(Observer observer)
70![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
71
if (observer != null)
72![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
73
this.ObserversCollection.Add(observer);
74
}
75
}
76![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
77
/// 注销观察者
78
/// </summary>
79
/// <param name="observer"></param>
80
public void DetachObserver(Observer observer)
81![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
82
if (observer != null)
83![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
84
this.ObserversCollection.Remove(observer);
85
}
86
}
87
88
}
89
}
90![](/Images/OutliningIndicators/None.gif)
91![](/Images/OutliningIndicators/None.gif)
92![](/Images/OutliningIndicators/None.gif)
93
using System;
94
using System.Collections.Generic;
95
using System.Text;
96![](/Images/OutliningIndicators/None.gif)
97
namespace ConsoleApplication3
98![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
99
public abstract class Observer
100![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
101
private Model _model;
102![](/Images/OutliningIndicators/InBlock.gif)
103![](/Images/OutliningIndicators/ContractedSubBlock.gif)
Contructor#region Contructor
104![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
105
/// 构造函数
106
/// </summary>
107
/// <param name="model"></param>
108
public Observer(Model model)
109![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
110
this._model = model;
111
this._model.NotifyEvent += new Model.ModelDelegate(DoSomeThing);
112
}
113
#endregion
114![](/Images/OutliningIndicators/InBlock.gif)
115![](/Images/OutliningIndicators/InBlock.gif)
116![](/Images/OutliningIndicators/ContractedSubBlock.gif)
Public Field#region Public Field
117![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
118
/// 被观察者
119
/// </summary>
120
public Model ObserverdModel
121![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
122
get
123![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
124
return _model;
125
}
126
set
127![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
128
_model = value;
129
}
130
}
131
#endregion
132![](/Images/OutliningIndicators/InBlock.gif)
133![](/Images/OutliningIndicators/ContractedSubBlock.gif)
Method#region Method
134![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
135
/// 观察者得到通知后的操作
136
/// </summary>
137
public abstract void DoSomeThing();
138
#endregion
139
}
140
}
141![](/Images/OutliningIndicators/None.gif)
142![](/Images/OutliningIndicators/None.gif)
143
using System;
144
using System.Collections.Generic;
145
using System.Text;
146![](/Images/OutliningIndicators/None.gif)
147
namespace ConsoleApplication3
148![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
149
public enum RatStateEnmu
150![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
151
Stealing,
152
Running,
153
StrickDownBottom,
154
Tackled
155
}
156
public class Rat:Model
157![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
158
bool _seeCat;
159
RatStateEnmu _state;
160
public RatStateEnmu State
161![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
162
get
163![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
164
return _state;
165
}
166
set
167![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
168
_state = value;
169
}
170
}
171
EventHandler _seeCatHandler;
172
public event EventHandler SeeCatComming
173![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
174
add
175![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
176
_seeCatHandler += value;
177
}
178
remove
179![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
180
_seeCatHandler -= value;
181
}
182
}
183
void Run(Object sender,EventArgs e)
184![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
185
Console.WriteLine("Rat:不好,大傻猫过来了,赶紧跑!\n");
186
_state = RatStateEnmu.Running;
187
if (_seeCatHandler != null)
188![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
189
_seeCatHandler(sender,e);
190
}
191
}
192![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
193
/// 是否看见了猫
194
/// </summary>
195
public bool SeeCat
196![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
197
get
198![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
199
return _seeCat;
200
}
201
set
202![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
203
_seeCat = value;
204
Run(this, new EventArgs());
205
}
206
}
207
public void StealOil()
208![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
209
Console.WriteLine("Rat:噔噔噔噔,我是一只小老鼠,自由的小老鼠,我赛,好大一桶油,够我吃1年的了。我要把它搬回家!\n");
210
Console.WriteLine("Rat:等等,听说最近死猫经常出现,小心点为妙!\n");
211
this._state = RatStateEnmu.Stealing;
212
this.Notify();
213
}
214![](/Images/OutliningIndicators/InBlock.gif)
215
public void StrickDowmBottle()
216![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
217
Console.WriteLine("system:哗啦啦,油瓶子被打翻了!\n");
218
this._state = RatStateEnmu.StrickDownBottom;
219
this.Notify();
220
}
221
}
222
}
223![](/Images/OutliningIndicators/None.gif)
224![](/Images/OutliningIndicators/None.gif)
225
using System;
226
using System.Collections.Generic;
227
using System.Text;
228![](/Images/OutliningIndicators/None.gif)
229
namespace ConsoleApplication3
230![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
231![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
232
/// 观察者-猫
233
/// </summary>
234
public class Cat:Observer
235![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
236
private Rat _rat;
237![](/Images/OutliningIndicators/InBlock.gif)
238
public Cat(Rat rat) : base(rat)
239![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
240
this._rat = rat;
241
this._rat.SeeCatComming += new EventHandler(_rat_SeeCatComming);
242
}
243![](/Images/OutliningIndicators/InBlock.gif)
244
void _rat_SeeCatComming(object sender, EventArgs e)
245![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
246
this._rat.StrickDowmBottle();
247
}
248![](/Images/OutliningIndicators/InBlock.gif)
249
public override void DoSomeThing()
250![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
251
if (_rat.State== RatStateEnmu.Stealing)
252![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
253
Console.WriteLine("cat:谁打扰了我的美梦,原来是你这个臭老鼠,耽误你猫大爷的休息,我要吃了你!\n");
254
this._rat.SeeCat = true;
255
}
256
}
257
}
258
}
259![](/Images/OutliningIndicators/None.gif)
260![](/Images/OutliningIndicators/None.gif)
261
using System;
262
using System.Collections.Generic;
263
using System.Text;
264![](/Images/OutliningIndicators/None.gif)
265
namespace ConsoleApplication3
266![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
267
public class Person:Observer
268![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
269
Rat rat;
270
public Person(Rat rat)
271
: base(rat)
272![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
273
this.rat = rat;
274
Console.WriteLine("Person:呼噜呼噜,昨天加班到3点,今天睡得真香!\n");
275
}
276
public override void DoSomeThing()
277![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
278
if (rat.State == RatStateEnmu.StrickDownBottom)
279![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
280
Console.WriteLine("Person:这是干什么呢亚,噼哩啪啦的,把我都给吵醒了\n");
281
}
282
}
283
}
284
}
285![](/Images/OutliningIndicators/None.gif)
286
--程序入口点
287
using System;
288
using System.Collections.Generic;
289
using System.Text;
290![](/Images/OutliningIndicators/None.gif)
291
namespace ConsoleApplication3
292![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
293
class Program
294![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
295
static void Main(string[] args)
296![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
297
Rat rat = new Rat();
298
Person p = new Person(rat);
299
Cat cat = new Cat(rat);
300
rat.StealOil();
301
Console.Read();
302
}
303
}
304
}
305![](/Images/OutliningIndicators/None.gif)
306![](/Images/OutliningIndicators/None.gif)
result:
UML关系图: