Program Design: The Cat shouted, and all the mice started to escape, and the host was awakened. (C # language)
Requirements:
1. The behavior of rats and Masters Should Be passive.
2. Considering scalability, the call of a cat may cause other association effects.
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)
Public interface Observer
{
Void Response (); // the Response of the observer, if the mouse sees the reflection of the cat
}
Public interface Subject
{
Void AimAt (Observer obs); // for which observers, this refers to the object to be captured by a cat-Mouse
}
Public class Mouse: Observer
{
Private string name;
Public Mouse (string name, Subject subj)
{
This. name = name;
Subj. AimAt (this );
}
Public void Response ()
{
Console. WriteLine (name + "attempt to escape! ");
}
}
Public class Master: Observer
{
Public Master (Subject subj)
{
Subj. AimAt (this );
}
Public void Response ()
{
Console. WriteLine ("Host waken! ");
}
}
Public class Cat: Subject
{
Private ArrayList observers;
Public Cat ()
{
This. observers = new ArrayList ();
}
Public void AimAt (Observer obs)
{
This. observers. Add (obs );
}
Public void Cry ()
{
Console. WriteLine ("Cat cryed! ");
Foreach (Observer obs in this. observers)
{
Obs. Response ();
}
}
}
Class MainClass
{
Static void Main (string [] args)
{
Cat cat = new Cat ();
Mouse mouse1 = new Mouse ("mouse1", cat );
Mouse mouse2 = new Mouse ("mouse2", cat );
Master master = new Master (cat );
Cat. Cry ();
}
}
// Configure //---------------------------------------------------------------------------------------------
Design Method 2: Design with event -- delegate ..
Public delegate void SubEventHandler ();
Public abstract class Subject
{
Public event SubEventHandler SubEvent;
Protected void FireAway ()
{
If (this. SubEvent! = Null)
This. SubEvent ();
}
}
Public class Cat: Subject
{
Public void Cry ()
{
Console. WriteLine ("cat cryed .");
This. FireAway ();
}
}
Public abstract class Observer
{
Public Observer (Subject sub)
{
Sub. SubEvent + = new SubEventHandler (Response );
}
Public abstract void Response ();
}
Public class Mouse: Observer
{
Private string name;
Public Mouse (string name, Subject sub): base (sub)
{
This. name = name;
}
Public override void Response ()
{
Console. WriteLine (name + "attempt to escape! ");
}
}
Public class Master: Observer
{
Public Master (Subject sub): base (sub ){}
Public override void Response ()
{
Console. WriteLine ("host waken ");
}
}
Class Class1
{
Static void Main (string [] args)
{
Cat cat = new Cat ();
Mouse mouse1 = new Mouse ("mouse1", cat );
Mouse mouse2 = new Mouse ("mouse2", cat );
Master master = new Master (cat );
Cat. Cry ();
}
}
Original article: http://hi.baidu.com/leihen/blog/item/6facb80e389eace737d12262.html