今天看了一道.net的笔试题,其中就是要你实现这个连锁反应,当中可以看出观察者模式,这里使用c#语言的事件和代理机制实现起来是简单的,如果不用语言特性,是分别使用c#中event特性和不使用的demo
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Runtime.InteropServices;
5using System.Collections;
6using CppPlusObserver;
7namespace CSharpObserver
8{
9 public class Cat
10 {
11 public delegate void CryEventHandler(object o,EventArgs e);
12 public event CryEventHandler Cry;
13 private string m_name;
14 public Cat(string name)
15 {
16 m_name = name;
17 }
18 public void OnCry()
19 {
20 if (Cry != null)
21 {
22 Cry(this, EventArgs.Empty);
23 }
24 }
25 }
26 public class Master
27 {
28 private string m_name;
29 public Master(string name)
30 {
31 m_name = name;
32 }
33 public void Response(object o,EventArgs e)
34 {
35 Console.WriteLine(m_name+" wake up");
36 }
37 }
38 public class Mouse
39 {
40 private string m_name;
41 public Mouse(string name)
42 {
43 m_name = name;
44 }
45 public void Response(object o, EventArgs e)
46 {
47 Console.WriteLine(m_name + " flee away");
48 }
49 }
50
51}
52namespace CppPlusObserver
53{
54 public interface Observer
55 {
56 void Response();
57 }
58 public interface Subject
59 {
60 void AddEvent(Observer s);
61 }
62 public class Mouse : Observer
63 {
64 private string m_name;
65 public Mouse(string name, Subject sub)
66 {
67 m_name = name;
68 sub.AddEvent(this);
69 }
70 Observer 成员
78 }
79 public class Cat : Subject
80 {
81 string m_name;
82 List<Observer> observers;
83 public Cat(string name)
84 {
85 m_name = name;
86 observers = new List<Observer>();
87 }
88 Subject 成员
96 public void Cry()
97 {
98 foreach (Observer ob in observers)
99 {
100 ob.Response();
101 }
102 }
103 }
104 public class Master : Observer
105 {
106
107 private string m_name;
108 public Master(string name, Subject s)
109 {
110 m_name = name;
111 s.AddEvent(this);
112 }
113 Observer 成员
121 }
122}
123namespace PInvokeDemo
124{
125
126 class Program
127 {
128
129 static void Main(string[] args)
130 {
131 //Cat cat = new Cat("cat1");
132 //Master ma = new Master("Lilei", cat);
133 //Mouse ms = new Mouse("Jerry", cat);
134 CSharpObserver.Cat cat = new CSharpObserver.Cat("cat1");
135 CSharpObserver.Master ma = new CSharpObserver.Master("lilei");
136 CSharpObserver.Mouse ms = new CSharpObserver.Mouse("jerry");
137 cat.Cry += new CSharpObserver.Cat.CryEventHandler(ms.Response);
138 cat.Cry += new CSharpObserver.Cat.CryEventHandler(ma.Response);
139 cat.OnCry();
140
141 }
142 }
143}
144
2using System.Collections.Generic;
3using System.Text;
4using System.Runtime.InteropServices;
5using System.Collections;
6using CppPlusObserver;
7namespace CSharpObserver
8{
9 public class Cat
10 {
11 public delegate void CryEventHandler(object o,EventArgs e);
12 public event CryEventHandler Cry;
13 private string m_name;
14 public Cat(string name)
15 {
16 m_name = name;
17 }
18 public void OnCry()
19 {
20 if (Cry != null)
21 {
22 Cry(this, EventArgs.Empty);
23 }
24 }
25 }
26 public class Master
27 {
28 private string m_name;
29 public Master(string name)
30 {
31 m_name = name;
32 }
33 public void Response(object o,EventArgs e)
34 {
35 Console.WriteLine(m_name+" wake up");
36 }
37 }
38 public class Mouse
39 {
40 private string m_name;
41 public Mouse(string name)
42 {
43 m_name = name;
44 }
45 public void Response(object o, EventArgs e)
46 {
47 Console.WriteLine(m_name + " flee away");
48 }
49 }
50
51}
52namespace CppPlusObserver
53{
54 public interface Observer
55 {
56 void Response();
57 }
58 public interface Subject
59 {
60 void AddEvent(Observer s);
61 }
62 public class Mouse : Observer
63 {
64 private string m_name;
65 public Mouse(string name, Subject sub)
66 {
67 m_name = name;
68 sub.AddEvent(this);
69 }
70 Observer 成员
78 }
79 public class Cat : Subject
80 {
81 string m_name;
82 List<Observer> observers;
83 public Cat(string name)
84 {
85 m_name = name;
86 observers = new List<Observer>();
87 }
88 Subject 成员
96 public void Cry()
97 {
98 foreach (Observer ob in observers)
99 {
100 ob.Response();
101 }
102 }
103 }
104 public class Master : Observer
105 {
106
107 private string m_name;
108 public Master(string name, Subject s)
109 {
110 m_name = name;
111 s.AddEvent(this);
112 }
113 Observer 成员
121 }
122}
123namespace PInvokeDemo
124{
125
126 class Program
127 {
128
129 static void Main(string[] args)
130 {
131 //Cat cat = new Cat("cat1");
132 //Master ma = new Master("Lilei", cat);
133 //Mouse ms = new Mouse("Jerry", cat);
134 CSharpObserver.Cat cat = new CSharpObserver.Cat("cat1");
135 CSharpObserver.Master ma = new CSharpObserver.Master("lilei");
136 CSharpObserver.Mouse ms = new CSharpObserver.Mouse("jerry");
137 cat.Cry += new CSharpObserver.Cat.CryEventHandler(ms.Response);
138 cat.Cry += new CSharpObserver.Cat.CryEventHandler(ma.Response);
139 cat.OnCry();
140
141 }
142 }
143}
144