A program designed to make a cat bark, a mouse escape, a person being awakened

Source: Internet
Author: User

The problem is this: the cat screamed, all the mice began to flee, the owner was awakened.

Requirements:

1, must have the linkage sex, the mouse and the human behavior is passive

2, consider the expandable line, cat calls may cause other linkage effects

See this program design topic, my first reaction is to use the event to solve, the cat called triggered the event, causing the mouse to escape, the mouse fled and triggered the incident caused the owner to be awakened. So according to this idea, I made the following answer.

First of all, the cat, mouse, and people were abstracted out into three categories, respectively: Cat, Mouse, people. In the Cat class we do the following:

public class Cat
{public
    delegate void crying (Object Sender,eventargs e);//define a cat call delegate public
    event crying cry ;//define Cat call event public
    void Oncry (EventArgs e)
    {
       if cry. =null)
       {
           cry (this,e);
       }
    } public
   void startcrying ()//Cat call, triggering cry event
   {
      MessageBox.Show ("The cat began to shout ...");
      EventArgs e=new EventArgs ();
      Oncry (e);
   }

public class Mouse
{public
   delegate void Runing (Object Sender,eventargs e), public
   evnet delegate run;
   public void OnRun (EventArgs e)
   {
        if (run!=null)
          {
             run (this,e); 
          }
   }
   public void startruning (cat c)
   {
       c.cry+=new cat.crying (c_cry);/registered cat called event, the mouse heard the cat and began to escape
   }
   Public void C_cry (Object Sender,evnetargs e)/////////////////////////////////////////////////
       EventArgs e=new EventArgs ();
       OnRun (e);
   }
public class
man {public
     void waking (Mouse m)
     {
         m.run+=new mouse.runing (m_run);//people registered for the Rat escape, When the rat ran away
     , he was awakened.} public
     void M_run (Object Sender,eventargs e)
         {
         MessageBox.Show ("People awake, What ' wrong?");
         }
}

btnTest_Click (object sender, EventArgs e)
{
    cat c=new cat ();
    Mouse m=new Mouse ();
    Person P=new person ();
    M.startrunning (c);
    P.waking (m);
    C.startcrying ();
}

The experimental results are as follows:

Here's another way to solve the problem.

OBSERVER (Observer mode)

First we need to create two new interfaces:

Public interface Observer
{
     void Response ()//reaction to the behavior of the observed object, this refers to the cat called
} public
Interface Subject
{
     void addobserver (Observer obj);//Add all observers, notify them when action occurs
}

public class Cat:subject
{
    ArrayList arrlylist, public
    Cat ()
    {
       arrlylist=new ArrayList ();
    }
    The void Addobserver (Observer obj)//implementation adds a method to observe the object
    {
       arrlylist.add (obj);
    }
    void Cry ()//cat called, and notify all observers, make corresponding response
    {
        MessageBox.Show ("cat called ...");
        foreach (Observer obj in arrlylist)
         {
              obj. Response ();}}

The public class Mouse:observer
{public
    Mouse (Cat C)//Adds the current observed object to the Observer collection
    {
        c.addobserver (this);
    } Public
    void Response ()
    {
        MessageBox.Show ("The rat starts running away ...");
    }

The public class People:observer
{public
   people (Cat C)//Adds the current observed object to the Observer collection
   {
      c.addoberver (this);
   }  Public
   void Respone ()
{
       MessageBox.Show ("People awake, What ' wrong?");
   }

Btn_click (Object Sender,eventargs e)
 {
cat c=new cat ();
Mouse m=new Mouse (c);
People P=new people (c);
C.cry ();
 }

The results are the same as those shown above.

Well, here's the solution to this program design problem, and through these two different solutions we can see that when it comes to multiple different observers, we use the observation to realize the linkage, the comparison, and the scalability is relatively strong, In fact, the observer pattern is primarily designed to react differently to multiple different observer objects. In this mode, a target object manages all the observer objects that are dependent on it, and actively notifies it when its own state changes. This is usually done by calling the methods provided by the various observers. This pattern is often used to implement an event-handling system.

We use the first method of event subscription processing, can achieve multi-level linkage, cat called Trigger rat Run, mouse run aroused people awake, the existence of the event is to make up for the lack of delegates, the event is the sender of the event (the object that triggers the event) and the event recipient (the method of handling the event) is associated with a proxy class, That is, the event mechanism is implemented through proxy classes. When an event is triggered, the agent of the event notifies (calls) the appropriate method to handle the event. Events are used to pass values for forms in WinForm, click events for buttons, and more.

This section is here, I hope to give you some help, but also please a lot of 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.