The cat shouted, and all the mice started to escape, and the host was awakened. (C # language) (Observer Mode)

Source: Internet
Author: User

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.