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