看完作者写的代码感觉非常烦琐,同时就如同作者所说还存在不足.利用C#的事件技术可以很好的解决上述问题.以下是实现的UML截图:
下面给出解决代码:
Code
1using System;
2
3namespace test
4{
5
6 public interface IObserver
7 {
8 ISubject Subject
9 {
10 set;
11 }
12 }
13
14 public interface ISubject
15 {
16 }
17
18 class Class1
19 {
20 [STAThread]
21 static void Main(string[] args)
22 {
23 int i;
24 CatClass catsubject=new CatClass ();
25
26 for (i=0;i<10;i++)
27 {
28 RatClass rat=new RatClass();
29 rat.Subject =catsubject;
30 }
31
32 ManClass man=new ManClass();
33 man.Subject=catsubject;
34
35 catsubject.Miao();
36 Console.ReadLine ();
37
38 }
39 }
40
41 public class CatClass:ISubject
42 {
43 ICat 成员ICat 成员#region ICat 成员
44 public delegate void CatEventHandler(object source,EventArgs e);
45 public event CatEventHandler CatEvent;
46
47 public void Miao()
48 {
49 Console.WriteLine ("喵");
50 if (CatEvent!=null)
51 {
52 CatEvent(this,EventArgs.Empty);
53 }
54 }
55
56 #endregion
57 }
58
59 public class RatClass:IObserver
60 {
61 IObserver 成员IObserver 成员#region IObserver 成员
62
63 CatClass cat=null;
64 public ISubject Subject
65 {
66 set
67 {
68 cat=(CatClass)value;
69 cat.CatEvent+=new test.CatClass.CatEventHandler(Run);
70 }
71 }
72
73 #endregion
74
75
76 public void Run(object source, EventArgs e)
77 {
78 Console.WriteLine ("猫来了,赶紧跑!!!");
79 }
80
81 }
82
83 public class ManClass:IObserver
84 {
85 CatClass cat=null;
86 IObserver 成员IObserver 成员#region IObserver 成员
87
88 public ISubject Subject
89 {
90 set
91 {
92 cat=(CatClass)value;
93 cat.CatEvent+=new test.CatClass.CatEventHandler(Wake);
94 }
95 }
96
97 #endregion
98
99 private void Wake(object source, EventArgs e)
100 {
101 Console.WriteLine ("主人被吵醒");
102 }
103 }
104
105}
106
1using System;
2
3namespace test
4{
5
6 public interface IObserver
7 {
8 ISubject Subject
9 {
10 set;
11 }
12 }
13
14 public interface ISubject
15 {
16 }
17
18 class Class1
19 {
20 [STAThread]
21 static void Main(string[] args)
22 {
23 int i;
24 CatClass catsubject=new CatClass ();
25
26 for (i=0;i<10;i++)
27 {
28 RatClass rat=new RatClass();
29 rat.Subject =catsubject;
30 }
31
32 ManClass man=new ManClass();
33 man.Subject=catsubject;
34
35 catsubject.Miao();
36 Console.ReadLine ();
37
38 }
39 }
40
41 public class CatClass:ISubject
42 {
43 ICat 成员ICat 成员#region ICat 成员
44 public delegate void CatEventHandler(object source,EventArgs e);
45 public event CatEventHandler CatEvent;
46
47 public void Miao()
48 {
49 Console.WriteLine ("喵");
50 if (CatEvent!=null)
51 {
52 CatEvent(this,EventArgs.Empty);
53 }
54 }
55
56 #endregion
57 }
58
59 public class RatClass:IObserver
60 {
61 IObserver 成员IObserver 成员#region IObserver 成员
62
63 CatClass cat=null;
64 public ISubject Subject
65 {
66 set
67 {
68 cat=(CatClass)value;
69 cat.CatEvent+=new test.CatClass.CatEventHandler(Run);
70 }
71 }
72
73 #endregion
74
75
76 public void Run(object source, EventArgs e)
77 {
78 Console.WriteLine ("猫来了,赶紧跑!!!");
79 }
80
81 }
82
83 public class ManClass:IObserver
84 {
85 CatClass cat=null;
86 IObserver 成员IObserver 成员#region IObserver 成员
87
88 public ISubject Subject
89 {
90 set
91 {
92 cat=(CatClass)value;
93 cat.CatEvent+=new test.CatClass.CatEventHandler(Wake);
94 }
95 }
96
97 #endregion
98
99 private void Wake(object source, EventArgs e)
100 {
101 Console.WriteLine ("主人被吵醒");
102 }
103 }
104
105}
106