要求: 1.要有联动性,老鼠和主人的行为是被动的。
2.考虑可扩展性,猫的叫声可能引起其他联动效应。
要点:1. 联动效果,运行代码只要执行Cat.Cryed()方法。2. 对老鼠和主人进行抽象
评分标准: <1>.构造出Cat、Mouse、Master三个类,并能使程序运行(2分)
<2>从Mouse和Master中提取抽象(5分)
<3>联动效应,只要执行Cat.Cryed()就可以使老鼠逃跑,主人惊醒。(3分)
代码一
1 public interface Observer
2 {
3 void Response(); //观察者的响应,如是老鼠见到猫的反映
4 }
5 public interface Subject
6 {
7 void AimAt(Observer obs); //针对哪些观察者,这里指猫的要扑捉的对象---老鼠
8 }
9 public class Mouse : Observer
10 {
11 private string name;
12 public Mouse(string name, Subject subj)
13 {
14 this.name = name;
15 subj.AimAt(this);
16 }
17
18 public void Response()
19 {
20 Console.WriteLine(name + " attempt to escape!");
21 }
22 }
23 public class Master : Observer
24 {
25 public Master(Subject subj)
26 {
27 subj.AimAt(this);
28 }
29
30 public void Response()
31 {
32 Console.WriteLine("Host waken!");
33 }
34 }
35
36 public class Cat : Subject
37 {
38 private ArrayList observers;
39 public Cat()
40 {
41 this.observers = new ArrayList();
42 }
43 public void AimAt(Observer obs)
44 {
45 this.observers.Add(obs);
46 }
47 public void Cry()
48 {
49 Console.WriteLine("Cat cryed!");
50 foreach (Observer obs in this.observers)
51 {
52 obs.Response();
53 }
54 }
55 }
56 class MainClass
57 {
58 static void Main(string[] args)
59 {
60 Cat cat = new Cat();
61 Mouse mouse1 = new Mouse("mouse1", cat);
62 Mouse mouse2 = new Mouse("mouse2", cat);
63 Master master = new Master(cat);
64 cat.Cry();
65 }
66 }
67
2 {
3 void Response(); //观察者的响应,如是老鼠见到猫的反映
4 }
5 public interface Subject
6 {
7 void AimAt(Observer obs); //针对哪些观察者,这里指猫的要扑捉的对象---老鼠
8 }
9 public class Mouse : Observer
10 {
11 private string name;
12 public Mouse(string name, Subject subj)
13 {
14 this.name = name;
15 subj.AimAt(this);
16 }
17
18 public void Response()
19 {
20 Console.WriteLine(name + " attempt to escape!");
21 }
22 }
23 public class Master : Observer
24 {
25 public Master(Subject subj)
26 {
27 subj.AimAt(this);
28 }
29
30 public void Response()
31 {
32 Console.WriteLine("Host waken!");
33 }
34 }
35
36 public class Cat : Subject
37 {
38 private ArrayList observers;
39 public Cat()
40 {
41 this.observers = new ArrayList();
42 }
43 public void AimAt(Observer obs)
44 {
45 this.observers.Add(obs);
46 }
47 public void Cry()
48 {
49 Console.WriteLine("Cat cryed!");
50 foreach (Observer obs in this.observers)
51 {
52 obs.Response();
53 }
54 }
55 }
56 class MainClass
57 {
58 static void Main(string[] args)
59 {
60 Cat cat = new Cat();
61 Mouse mouse1 = new Mouse("mouse1", cat);
62 Mouse mouse2 = new Mouse("mouse2", cat);
63 Master master = new Master(cat);
64 cat.Cry();
65 }
66 }
67
//---------------------------------------------------------------------------------------------
设计方法二: 使用event -- delegate设计..
代码二
1 public delegate void SubEventHandler();
2 public abstract class Subject
3 {
4 public event SubEventHandler SubEvent;
5 protected void FireAway()
6 {
7 if (this.SubEvent != null)
8 this.SubEvent();
9 }
10 }
11 public class Cat : Subject
12 {
13 public void Cry()
14 {
15 Console.WriteLine("cat cryed.");
16 this.FireAway();
17 }
18 }
19 public abstract class Observer
20 {
21 public Observer(Subject sub)
22 {
23 sub.SubEvent += new SubEventHandler(Response);
24 }
25 public abstract void Response();
26 }
27 public class Mouse : Observer
28 {
29 private string name;
30 public Mouse(string name, Subject sub)
31 : base(sub)
32 {
33 this.name = name;
34 }
35 public override void Response()
36 {
37 Console.WriteLine(name + " attempt to escape!");
38 }
39 }
40 public class Master : Observer
41 {
42 public Master(Subject sub) : base(sub) { }
43 public override void Response()
44 {
45 Console.WriteLine("host waken");
46 }
47 }
48 class Class1
49 {
50 static void Main(string[] args)
51 {
52 Cat cat = new Cat();
53 Mouse mouse1 = new Mouse("mouse1", cat);
54 Mouse mouse2 = new Mouse("mouse2", cat);
55 Master master = new Master(cat);
56 cat.Cry();
57 }
58
59 }
60
2 public abstract class Subject
3 {
4 public event SubEventHandler SubEvent;
5 protected void FireAway()
6 {
7 if (this.SubEvent != null)
8 this.SubEvent();
9 }
10 }
11 public class Cat : Subject
12 {
13 public void Cry()
14 {
15 Console.WriteLine("cat cryed.");
16 this.FireAway();
17 }
18 }
19 public abstract class Observer
20 {
21 public Observer(Subject sub)
22 {
23 sub.SubEvent += new SubEventHandler(Response);
24 }
25 public abstract void Response();
26 }
27 public class Mouse : Observer
28 {
29 private string name;
30 public Mouse(string name, Subject sub)
31 : base(sub)
32 {
33 this.name = name;
34 }
35 public override void Response()
36 {
37 Console.WriteLine(name + " attempt to escape!");
38 }
39 }
40 public class Master : Observer
41 {
42 public Master(Subject sub) : base(sub) { }
43 public override void Response()
44 {
45 Console.WriteLine("host waken");
46 }
47 }
48 class Class1
49 {
50 static void Main(string[] args)
51 {
52 Cat cat = new Cat();
53 Mouse mouse1 = new Mouse("mouse1", cat);
54 Mouse mouse2 = new Mouse("mouse2", cat);
55 Master master = new Master(cat);
56 cat.Cry();
57 }
58
59 }
60