一道猫和老鼠吵醒主人的笔试题(C#)
CSDN帖子:http://community.csdn.net/Expert/topic/3839/3839240.xml?temp=.6079377.
程序设计:猫大叫一声,所有的老鼠都开始逃跑,主人被惊醒。(C#语言)
要求:
1.要有联动性,老鼠和主人的行为是被动的。
2.考虑可扩展性,猫的叫声可能引起其它联动效应
大部分答案都是使用的事件编程,我这里换了一下思路,使用观察着模式,用接口也实现了,因为考虑到第二个要求,即猫大叫也可能直接导致主人惊醒,所以Man也继承了ICatCatcher接口
1using System;
2using System.Collections;
3
4namespace test
5{
6
7 public interface ICatCatcher
8 {
9 void DoSth();
10 }
11
12 public interface ICatSubject
13 {
14 void RegesiterCatCatcher(ICatCatcher catCatcher);
15 void Miao();
16 }
17
18 public interface IRatSubject
19 {
20 void RegesiterRatCatcher(IRatCatcher ratCatcher);
21 void Run();
22 }
23
24 public interface IRatCatcher
25 {
26 void Wake();
27 }
28
29 public class Cat:ICatSubject
30 {
31 public Cat()
32 {
33 }
34
35 private ArrayList catcherList = new ArrayList();
36 public void RegesiterCatCatcher(ICatCatcher catcher)
37 {
38 catcherList.Add( catcher );
39 }
40
41 public void Miao()
42 {
43 Console.WriteLine( "Miao" );
44
45 for(int i=0;i<catcherList.Count;i++)
46 {
47 ICatCatcher catCatcher = (ICatCatcher)catcherList[i];
48 catCatcher.DoSth();
49 }
50 }
51
52 [STAThread]
53 public static void Main()
54 {
55 Cat cat = new Cat();
56
57 Rat[] rat = new Rat[10];
58 for( int i=0;i<10;i++)
59 {
60 rat[i] = new Rat(cat);
61 }
62
63 Man man = new Man(rat,cat);
64
65 cat.Miao();
66 }
67 }
68
69 public class Rat:ICatCatcher,IRatSubject
70 {
71 public Rat(ICatSubject catSub)
72 {
73 catSub.RegesiterCatCatcher(this);
74 }
75
76 public void DoSth()
77 {
78 Run();
79 }
80
81 private ArrayList ratcherList = new ArrayList();
82 public void RegesiterRatCatcher(IRatCatcher catcher)
83 {
84 ratcherList.Add( catcher );
85 }
86
87 public void Run()
88 {
89 Console.WriteLine("Rat Run");
90 for(int i=0;i<ratcherList.Count;i++)
91 {
92 IRatCatcher ratCatcher = (IRatCatcher)ratcherList[i];
93 ratCatcher.Wake();
94 }
95 }
96 }
97
98 public class Man:ICatCatcher,IRatCatcher
99 {
100 public Man(IRatSubject[] ratSub,ICatSubject catSub)
101 {
102 for( int i=0 ;i<ratSub.Length;i++)
103 {
104 ratSub[i].RegesiterRatCatcher(this);
105 }
106 catSub.RegesiterCatCatcher(this);
107 }
108
109 public void DoSth()
110 {
111 Console.WriteLine( "Cat bring on Wake" );
112 }
113
114 public void Wake()
115 {
116 Console.WriteLine( "Rats bring on Wake" );
117 }
118 }
119}
120
2using System.Collections;
3
4namespace test
5{
6
7 public interface ICatCatcher
8 {
9 void DoSth();
10 }
11
12 public interface ICatSubject
13 {
14 void RegesiterCatCatcher(ICatCatcher catCatcher);
15 void Miao();
16 }
17
18 public interface IRatSubject
19 {
20 void RegesiterRatCatcher(IRatCatcher ratCatcher);
21 void Run();
22 }
23
24 public interface IRatCatcher
25 {
26 void Wake();
27 }
28
29 public class Cat:ICatSubject
30 {
31 public Cat()
32 {
33 }
34
35 private ArrayList catcherList = new ArrayList();
36 public void RegesiterCatCatcher(ICatCatcher catcher)
37 {
38 catcherList.Add( catcher );
39 }
40
41 public void Miao()
42 {
43 Console.WriteLine( "Miao" );
44
45 for(int i=0;i<catcherList.Count;i++)
46 {
47 ICatCatcher catCatcher = (ICatCatcher)catcherList[i];
48 catCatcher.DoSth();
49 }
50 }
51
52 [STAThread]
53 public static void Main()
54 {
55 Cat cat = new Cat();
56
57 Rat[] rat = new Rat[10];
58 for( int i=0;i<10;i++)
59 {
60 rat[i] = new Rat(cat);
61 }
62
63 Man man = new Man(rat,cat);
64
65 cat.Miao();
66 }
67 }
68
69 public class Rat:ICatCatcher,IRatSubject
70 {
71 public Rat(ICatSubject catSub)
72 {
73 catSub.RegesiterCatCatcher(this);
74 }
75
76 public void DoSth()
77 {
78 Run();
79 }
80
81 private ArrayList ratcherList = new ArrayList();
82 public void RegesiterRatCatcher(IRatCatcher catcher)
83 {
84 ratcherList.Add( catcher );
85 }
86
87 public void Run()
88 {
89 Console.WriteLine("Rat Run");
90 for(int i=0;i<ratcherList.Count;i++)
91 {
92 IRatCatcher ratCatcher = (IRatCatcher)ratcherList[i];
93 ratCatcher.Wake();
94 }
95 }
96 }
97
98 public class Man:ICatCatcher,IRatCatcher
99 {
100 public Man(IRatSubject[] ratSub,ICatSubject catSub)
101 {
102 for( int i=0 ;i<ratSub.Length;i++)
103 {
104 ratSub[i].RegesiterRatCatcher(this);
105 }
106 catSub.RegesiterCatCatcher(this);
107 }
108
109 public void DoSth()
110 {
111 Console.WriteLine( "Cat bring on Wake" );
112 }
113
114 public void Wake()
115 {
116 Console.WriteLine( "Rats bring on Wake" );
117 }
118 }
119}
120
这里如果调试会出现一点点小问题,就是老鼠有很多,每个老鼠的Run都会Wake一下Man,所以感觉是主人被多次惊醒,其实这只是因为计算机总是按照顺序来执行程序的,能够模拟到这种效果应该已经算符合题意了