Delegation, events, and Observer Model problems caused by "Cats, mice, and Masters"

Source: Internet
Author: User

This is a very classic interview question. there are many posts discussed on the Internet, and some netizens have provided wonderful answers. here is just a simple answer. though simplifiedCodeIt reflects that many people have never been very clear about delegation, especially the definition and registration of events, but also the observer model.

Using system;

Namespace catratandhost // cat, mouse and host, interesting delegate, event and Observer Model Problems
{
Class Program
{
Static void main (string [] ARGs)
{
Catclass cat = new catclass ();
Manclass man = new manclass ();
For (INT I = 0; I <5; I ++)
{
Ratclass rat = new ratclass (); // The mouse is an observer and is interested in the movements of cats. note: In this loop, five new rats with the same name are added. the variable rat only applies {...} valid.
Rat. Subject = cat; // set the cat to be the observed object and register the event handling method for the cat call event with the cat-The mouse escaped.
Man. Subject = rat; // The mouse is the object of observation here. The host is interested in its running, so he registers the event handling method for the running event with the mouse.
}
Cat. catevent + = new catclass. cateventhandler (man. wake1); // register the event processing method directly with the cat.
Man. iswake = false; // The host goes to bed.
Cat. catmiao (); // a cat call that causes a series of actions.
Console. readkey ();
}
}
Public interface iobserver // as the observer, there is a property set for the observer object.
{
Isubject subject
{
Set;
}
}
Public interface isubject
{}
Public class catclass: isubject // cat object, inheriting the observation object interface
{
Public Delegate void cateventhandler (Object source, eventargs E); // declare the event Delegate
Public event cateventhandler catevent; // defines the event

Public Virtual void catmiao () // call and trigger the event
{< br> console. writeline ("The Cat: Miao... ");
If (catevent! = NULL)
{< br> catevent (this, eventargs. empty);
}< BR >}< br> public class ratclass: iobserver, isubject // mouse object, inherit the observer interface, and also observe the object.
{< br> public event eventhandler ratevent; // exploitation.. Net pre-defined eventhandler delegate to directly define events.

Catclass cat = NULL; // The observed object used when registering the Event Processing Method
Public isubject subject // observe Object Attributes
{
Set
{
Cat = (catclass) value;
Cat. catevent + = new catclass. cateventhandler (run); // registers the event processing method.
}
}
Public Virtual void run (Object source, eventargs e) // defines the Event Processing Method
{
Console. writeline ("the rat: Run... run ...");
If (ratevent! = NULL)
{
Ratevent (this, eventargs. Empty );
}
}
}
Public class manclass: iobserver // the host is the same as the mouse. In fact, the host may also be awakened by the mouse.
{
Private bool _ iswake = true;
Public bool iswake
{
Get
{
Return _ iswake;
}
Set
{
_ Iswake = value;
}
}

Ratclass rat = NULL;
Public isubject subject
{
Set
{
Rat = (ratclass) value;
Rat. ratevent + = new eventhandler (wake2 );
}
}
Public Virtual void wake1 (Object source, eventargs E)
{
If (! _ Iswake) // ensure that the host is not awakened again
{
Console. writeline ("the Host: waked by cat ...");
_ Iswake = true;
}
}
Public Virtual void wake2 (Object source, eventargs E)
{
If (! _ Iswake) // ensure that the host is not awakened again
{
Console. writeline ("the Host: waked by rat ...");
_ Iswake = true;
}
}
}
}

The Code uses two methods for defining events:

Public event eventhandler ratevent; // exploitation.. Net pre-defined eventhandler delegate to directly define events. this method is simple, but the parameters and return values of the event can only be performed according to the predefined method. however, in general, this method is enough.
Public Delegate void cateventhandler (Object source, eventargs E); // declare the event Delegate
Public event cateventhandler catevent; // defines an event. In this way, you must first declare a related delegate. then, the new delegate is used to register the event. this method is more difficult than the previous method, but we can define the parameter list and return value by ourselves, which is more flexible and more powerful.

The Code uses two event registration methods:

In fact, it can only be regarded as one method, but it can only be done through an impossible way. In the end, it is used to "observe the object. event name + = new event corresponds to the Delegate (Event Processing Method of the observer) "formula to complete.
One method is to use the observer attribute -- The Observer object to register an event for the observer.
for example, Rat. subject = cat;
register directly after declaring the observer and the observer.
for example, Cat. catevent + = new catclass. cateventhandler (man. wake1);

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.