1
using
System;
2 using System.Collections;
3 using System.Collections.Generic;
4
5 public class MyClass
6 {
7 public static void Main()
8 {
9 Mouse m = new Mouse();
10 People p = new People();
11
12 HandleDelegate m_h = new HandleDelegate(m.Run);
13 HandleDelegate p_h = new HandleDelegate(p.Wakeup);
14
15 Observer ob = new Observer();
16 ob.AddObserver(m_h);
17 ob.AddObserver(p_h);
18 Cat cat = new Cat(ob);
19 cat.Cry();
20 ob.RemoveObserver(p_h);
21 cat.Cry();
22 RL();
23 }
24
25 Helper methods#region Helper methods
26
27 private static void WL(object text, params object[] args)
28 {
29 Console.WriteLine(text.ToString(), args);
30 }
31
32 private static void RL()
33 {
34 Console.ReadLine();
35 }
36
37 private static void Break()
38 {
39 System.Diagnostics.Debugger.Break();
40 }
41
42 #endregion
43}
44
45
46 public delegate void HandleDelegate();
47
48 public class Cat
49 {
50 private Observer ob;
51
52 public Cat(Observer _ob)
53 {
54 ob = _ob;
55 }
56
57 public void Cry()
58 {
59 Console.WriteLine("Cat cry .");
60 ob.Do();
61 }
62}
63
64 public class Mouse
65 {
66 public void Run()
67 {
68 Console.WriteLine("Runing.");
69 }
70}
71
72 public class People
73 {
74 public void Wakeup()
75 {
76 Console.WriteLine("Wakeup");
77 }
78}
79
80 public class Observer
81 {
82 private IList<HandleDelegate> list = null;
83
84 public Observer()
85 {
86 list = new List<HandleDelegate>();
87 }
88
89 public void AddObserver(HandleDelegate h)
90 {
91 list.Add(h);
92 }
93 public void RemoveObserver(HandleDelegate h)
94 {
95 if(list.Contains(h))
96 {
97 list.Remove(h);
98 }
99 else
100 return;
101 }
102
103 public void Do()
104 {
105 foreach(HandleDelegate h in list)
106 {
107 h();
108 }
109 }
110
111}
2 using System.Collections;
3 using System.Collections.Generic;
4
5 public class MyClass
6 {
7 public static void Main()
8 {
9 Mouse m = new Mouse();
10 People p = new People();
11
12 HandleDelegate m_h = new HandleDelegate(m.Run);
13 HandleDelegate p_h = new HandleDelegate(p.Wakeup);
14
15 Observer ob = new Observer();
16 ob.AddObserver(m_h);
17 ob.AddObserver(p_h);
18 Cat cat = new Cat(ob);
19 cat.Cry();
20 ob.RemoveObserver(p_h);
21 cat.Cry();
22 RL();
23 }
24
25 Helper methods#region Helper methods
26
27 private static void WL(object text, params object[] args)
28 {
29 Console.WriteLine(text.ToString(), args);
30 }
31
32 private static void RL()
33 {
34 Console.ReadLine();
35 }
36
37 private static void Break()
38 {
39 System.Diagnostics.Debugger.Break();
40 }
41
42 #endregion
43}
44
45
46 public delegate void HandleDelegate();
47
48 public class Cat
49 {
50 private Observer ob;
51
52 public Cat(Observer _ob)
53 {
54 ob = _ob;
55 }
56
57 public void Cry()
58 {
59 Console.WriteLine("Cat cry .");
60 ob.Do();
61 }
62}
63
64 public class Mouse
65 {
66 public void Run()
67 {
68 Console.WriteLine("Runing.");
69 }
70}
71
72 public class People
73 {
74 public void Wakeup()
75 {
76 Console.WriteLine("Wakeup");
77 }
78}
79
80 public class Observer
81 {
82 private IList<HandleDelegate> list = null;
83
84 public Observer()
85 {
86 list = new List<HandleDelegate>();
87 }
88
89 public void AddObserver(HandleDelegate h)
90 {
91 list.Add(h);
92 }
93 public void RemoveObserver(HandleDelegate h)
94 {
95 if(list.Contains(h))
96 {
97 list.Remove(h);
98 }
99 else
100 return;
101 }
102
103 public void Do()
104 {
105 foreach(HandleDelegate h in list)
106 {
107 h();
108 }
109 }
110
111}
面试的时候遇到这样一个问题,整理下