Program design: The cat shouted, all the mice began to flee, the master was awakened. (C # language)

Source: Internet
Author: User

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)

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.