Code
1/**//*
2 * 题目:
3 * 猫叫了,所有老鼠开始逃跑,主人被惊醒,请用OO的思想描绘此过程
4 * 1,老鼠跟主人是被动的
5 * 2,要考虑联动性与扩展性
6 */
7using System;
8using System.Collections.Generic;
9using System.Text;
10
11namespace CatCry {
12
13 /**//*委托,当猫叫时将信息传给所有观察者*/
14 public delegate void CatCryEventHandler(object sender, EventArgs e);
15
16 /**//*被观察对象*/
17 abstract class Subject {
18 public event CatCryEventHandler CatCryEvent;//猫叫时的事件
19 public void Cry(EventArgs e) {
20 this.CatCryEvent(this, e);
21 }
22 public void Cry() {
23 //不接收猫叫时发出的信息
24 this.CatCryEvent(this, new EventArgs());
25 }
26 }
27
28 /**//*观察者*/
29 abstract class Observer {
30 public Observer() { }
31 public Observer(Subject subject) {
32 subject.CatCryEvent += new CatCryEventHandler(this.Notified);
33 }
34 //当猫叫时做出的反应(当收到通知时)
35 abstract protected void Notified(object sender, EventArgs e);
36 }
37
38 /**//*定义猫作为被观察对象*/
39 class Cat : Subject {
40 public Cat() { Console.WriteLine("猫叫了"); }
41 }
42
43 /**//*定义老鼠作为观察者*/
44 class Mouse : Observer {
45 string mouseName;
46 public Mouse() { }
47 public Mouse(string mouseName, Subject subject)
48 : base(subject) {
49 this.mouseName = mouseName;
50 }
51 override protected void Notified(object sender, EventArgs e) {
52 Console.WriteLine(this.mouseName + "开始逃跑");
53 }
54 }
55
56 /**//*定义主人作为观察者*/
57 class Marster : Observer {
58 public Marster() { }
59 public Marster(Subject subject) : base(subject) { }
60 override protected void Notified(object sender, EventArgs e) {
61 Console.WriteLine("主人被惊醒了");
62 }
63 }
64
65 /**//// <summary>
66 /// 功能:用观察者模式实现
67 /// 作者:allnen
68 /// 时间:2008-6-5
69 /// </summary>
70 class Program {
71 static void Main(string[] args) {
72 Cat cat = new Cat();
73 Mouse m1 = new Mouse("老鼠1", cat);
74 Mouse m2 = new Mouse("老鼠2", cat);
75
76 Marster marster = new Marster(cat);
77 cat.Cry();//猫叫了
78
79 }
80 }
81}
82
1/**//*
2 * 题目:
3 * 猫叫了,所有老鼠开始逃跑,主人被惊醒,请用OO的思想描绘此过程
4 * 1,老鼠跟主人是被动的
5 * 2,要考虑联动性与扩展性
6 */
7using System;
8using System.Collections.Generic;
9using System.Text;
10
11namespace CatCry {
12
13 /**//*委托,当猫叫时将信息传给所有观察者*/
14 public delegate void CatCryEventHandler(object sender, EventArgs e);
15
16 /**//*被观察对象*/
17 abstract class Subject {
18 public event CatCryEventHandler CatCryEvent;//猫叫时的事件
19 public void Cry(EventArgs e) {
20 this.CatCryEvent(this, e);
21 }
22 public void Cry() {
23 //不接收猫叫时发出的信息
24 this.CatCryEvent(this, new EventArgs());
25 }
26 }
27
28 /**//*观察者*/
29 abstract class Observer {
30 public Observer() { }
31 public Observer(Subject subject) {
32 subject.CatCryEvent += new CatCryEventHandler(this.Notified);
33 }
34 //当猫叫时做出的反应(当收到通知时)
35 abstract protected void Notified(object sender, EventArgs e);
36 }
37
38 /**//*定义猫作为被观察对象*/
39 class Cat : Subject {
40 public Cat() { Console.WriteLine("猫叫了"); }
41 }
42
43 /**//*定义老鼠作为观察者*/
44 class Mouse : Observer {
45 string mouseName;
46 public Mouse() { }
47 public Mouse(string mouseName, Subject subject)
48 : base(subject) {
49 this.mouseName = mouseName;
50 }
51 override protected void Notified(object sender, EventArgs e) {
52 Console.WriteLine(this.mouseName + "开始逃跑");
53 }
54 }
55
56 /**//*定义主人作为观察者*/
57 class Marster : Observer {
58 public Marster() { }
59 public Marster(Subject subject) : base(subject) { }
60 override protected void Notified(object sender, EventArgs e) {
61 Console.WriteLine("主人被惊醒了");
62 }
63 }
64
65 /**//// <summary>
66 /// 功能:用观察者模式实现
67 /// 作者:allnen
68 /// 时间:2008-6-5
69 /// </summary>
70 class Program {
71 static void Main(string[] args) {
72 Cat cat = new Cat();
73 Mouse m1 = new Mouse("老鼠1", cat);
74 Mouse m2 = new Mouse("老鼠2", cat);
75
76 Marster marster = new Marster(cat);
77 cat.Cry();//猫叫了
78
79 }
80 }
81}
82