Program Design for the call of a cat, escape of a mouse, and awakening of a person

Source: Internet
Author: User

The question is: The Cat shouted, all the mice started to escape, and the host was awakened.

Requirements:

1. interactions are required. The behavior of rats and people is passive.

2. Considering the scalability, cat call may cause other association effects.

When I saw this program design question, my first reaction was to solve it with an event. The cat called the trigger event, which caused the mouse to escape. The mouse escaped and triggered the event, causing the host to be awakened, or a cat call causes a mouse to escape and wake up. So according to this idea, I made the following answers.

The first solution is to solve the problem with events: multi-level interaction: that is, the cat calls-"The mouse runs-" the person wakes up

The second solution adopts the observer mode: the cat calls the mouse to run, and the cat calls the mouse to wake up.

First, the cat, mouse, and people are abstracted into three classes: CAT, mouse, and people. In the cat class, we do the following:

 

Public class cat {Public Delegate void crying (Object sender, eventargs E); // defines a project called deleGATE public event crying cry; // defines the project name event public void oncry (eventargs E) {If (cry! = NULL) {cry (this, e) ;}} public void startcrying () // call a cat and trigger the cry event {MessageBox. show ("The Cat started calling ...... "); eventargs E = new eventargs (); oncry (e );}}

 

Public class mouse {Public Delegate void runing (Object sender, eventargs E); Public evnet running run; Public void onrun (eventargs e) {If (RUN! = NULL) {run (this, e) ;}} public mouse (cat c) {C. cry + = new cat. crying (c_cry); // registered a cat call event. When the mouse hears the call, it starts to escape} void c_cry (Object sender, evnetargs E) // The mouse triggered another wake-up event {MessageBox. show ("the mouse has escaped ........ "); eventargs E = new eventargs (); onrun (e) ;}} public class person {public person (mouse m) {M. run + = new mouse. runing (m_run); // a person registers for a rat escape event and is awakened when the mouse escapes} void m_run (Object sender, eventargs e) {MessageBox. show ("Wake up, W Hat's wrong? ");}}

 

BtnTest_Click(object sender, EventArgs e){    Cat c=new Cat();    Mouse m=new Mouse(c);    Person p=new Person(m);    c.StartCrying();
}

 

The experiment results are as follows:

 

Next we can solve this problem in another way.

Observer (Observer Mode)

First, we need to create two interfaces:

 

Public interface observer {void response (); // responds to the behavior of the observed object. This refers to the cat name} public interface subject {void addobserver (Observer OBJ ); // Add all the observers and notify them when an action occurs}

 

Public class Cat: Subject {arraylist arrlylist; Public CAT () {arrlylist = new arraylist ();} void addobserver (Observer OBJ) // Add and observe the object method {arrlylist. add (OBJ);} void cry () // The cat calls and notifies all observers to respond accordingly {MessageBox. show ("the cat is called ...... "); foreach (Observer OBJ in arrlylist) {obj. response ();}}}

 

Public class mouse: Observer {public mouse (cat c) // Add the current observed object to the observer set {C. addobserver (this);} public void response () {MessageBox. show ("the mouse has escaped ..... ");}}

 

Public class people: Observer {public people (cat c) // Add the current observed object to the observer set {C. addoberver (this);} public void respone () {MessageBox. show ("Wake up, what's wrong? ");}}

 

Btn_Click(Object sender,EventArgs e) {Cat c=new Cat();Mouse m=new Mouse(c);People p=new People(c);c.Cry(); }

 

The result is the same as shown above.

Now, the solution to this program design question is just like this. Through these two different solutions, we can find that when we are targeting multiple different observers, observation is used to achieve linkage, comparison, and scalability. In fact, the observer mode is mainly used to design different responses to multiple observer objects. In this mode, a target object manages all observer objects that depend on it and actively sends notifications when its status changes. This is usually done by calling the methods provided by various observers. This mode is usually used to implement an event processing system.

On the other hand, we use the first method to subscribe to events to achieve multi-level interaction. The cat calls the mouse to run and the mouse runs the mouse to wake up. The event exists to make up for the lack of delegation, an event is a proxy class that associates the event sender (the object that triggers the event) with the event receiver (the method used to process the event). That is, the event mechanism is implemented through the proxy class. When an event is triggered, the event proxy notifies (CALLS) the corresponding method to process the event. Events are mostly used for passing values of forms in winform, click events of buttons, and so on.

This section is here. I hope it will help you and provide more guidance!

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.