Title: The mouse escaped and the host woke up. (10 points)
Key points: 1. linkage effect. To run the code, you only need to execute the cat. cryed () method. 2. abstract the mouse and the host
Scoring standard: <1>. constructs cat, mouse, and master classes, and enables the program to run (2 points)
<2> extract abstraction from mouse and master (5 points)
<3> As long as Cat. cryed () is executed, the mouse can escape and the host will be awakened. (3 points)
Program code
1 using system;
2 using system. collections;
3
4 namespace leleapplication1
5 {
6 public interface observer
7 {
8 void response (); // the response of the observer, if the mouse sees the reflection of the cat
9}
10 public interface subject
11 {
12 Void aimat (Observer obs); // for which observers, this refers to the object to be captured by a cat-Mouse
13}
14
15 public class mouse: Observer
16 {
17 private string name;
18 public mouse (string name, subject subj)
19 {
20 This. Name = Name;
21 subj. aimat (this );
22}
23
24 public void response ()
25 {
26 console. writeline (name + "attempt to escape! ");
27}
28}
29
30 public class master: Observer
31 {
32 public master (subject subj)
33 {
34 subj. aimat (this );
35}
36
37 public void response ()
38 {
39 console. writeline ("host waken! ");
40}
41}
42
43
44 public class Cat: Subject
45 {
46 private arraylist observers;
47 Public CAT ()
48 {
49 This. Observers = new arraylist ();
50}
51 public void aimat (Observer obs)
52 {
53 This. Observers. Add (OBS );
54}
55 public void cry ()
56 {
57 console. writeline ("cat cryed! ");
58 foreach (Observer OBS in this. Observers)
59 {
60 obs. Response ();
61}
62}
63}
64
65 class mainclass
66 {
67/** // <summary>
68 // main entry point of the application.
69 /// </Summary>
70 [stathread]
71 static void main (string [] ARGs)
72 {
73 Cat cat = new CAT ();
74 mouse mouse1 = new mouse ("mouse1", CAT );
75 mouse mouse2 = new mouse ("mouse2", CAT );
76 master = new master (CAT );
77 cat. Cry ();
78}
79}
80}
--------------------------------------------------------------------------------
Design Method 2: Design with event -- delegate ..
Program code
1 using system;
2 using system. collections;
3
4
5 namespace leleapplication1
6 {
7 public delegate void subeventhandler ();
8 public abstract class subject
9 {
10 public event subeventhandler subevent;
11 protected void fireaway ()
12 {
13 if (this. subevent! = NULL)
14 This. subevent ();
15}
16}
17 public class Cat: Subject
18 {
19 public void cry ()
20 {
21 console. writeline ("cat cryed .");
22 This. fireaway ();
23}
24}
25
26 public abstract class observer
27 {
28 public observer (subject sub)
29 {
30 sub. subevent + = new subeventhandler (response );
31}
32 public abstract void response ();
33}
34 public class mouse: Observer
35 {
36 private string name;
37 public mouse (string name, subject sub): Base (sub)
38 {
39 This. Name = Name;
40}
41 public override void response ()
42 {
43 console. writeline (name + "attempt to escape! ");
44}
45}
46 public class master: Observer
47 {
48 Public master (subject sub): Base (sub ){}
49 public override void response ()
50 {
51 console. writeline ("host waken ");
52}
53}
54
55 class mainclass
56 {
57/** // <summary>
58 // main entry point of the application.
59 /// </Summary>
60 [stathread]
61 static void main (string [] ARGs)
62 {
63 Cat cat = new CAT ();
64 mouse mouse1 = new mouse ("mouse1", CAT );
65 mouse mouse2 = new mouse ("mouse2", CAT );
66 master = new master (CAT );
67 cat. Cry ();
68}
69}
70}