Requirements: 1. To have linkage, the behavior of the mouse and the master is passive.
2. Considering extensibility, cat calls can cause other linkage effects.
Point: 1. Linkage effect, run code as long as the cat.cryed () method is executed. 2. Abstract the mouse and the master
Rating Standard: <1> constructs a cat, Mouse, master three classes, and enables the program to run (2 points)
<2> extracting abstraction from Mouse and master (5 points)
<3> linkage effect, as long as the implementation of cat.cryed () can make the mouse escape, the host awakened. (3 points)
Public InterfaceObserver {voidResponse ();//The response of the Observer as if the mouse had seen the cat's reaction } Public InterfaceSubject {voidAimat (Observer obs);//for which observers, this refers to the cat's object to catch-the mouse } Public classMouse:observer {Private stringname; PublicMouse (stringname, Subject subj) { This. Name =name; Subj. Aimat ( This); } Public voidResponse () {Console.WriteLine (name+"attempt to escape!"); } } Public classMaster:observer { PublicMaster (Subject subj) {subj. Aimat ( This); } Public voidResponse () {Console.WriteLine ("Host waken!"); } } Public classCat:subject {PrivateArrayList observers; PublicCat () { This. Observers =NewArrayList (); } Public voidAimat (Observer obs) { This. Observers. ADD (OBS); } Public voidCry () {Console.WriteLine ("Cat cryed!"); foreach(Observer Obsinch This. Observers) {Obs. Response (); } } } classMainClass {Static voidMain (string[] args) {Cat Cat=NewCat (); Mouse mouse1=NewMouse ("mouse1", cat); Mouse Mouse2=NewMouse ("Mouse2", cat); Master Master=NewMaster (CAT); Cat. Cry (); } }
Program design: The cat shouted, all the mice began to flee, the master was awakened. (C # language)