Observer mode (the story of cats and mice !)

Source: Internet
Author: User

The observer mode is often seen in forums, mainly taking cats, mice, and masters as examples:
My personal understanding is as follows:
CodeAs follows:

Namespace Test
{
/// <Summary>
/// Define a delegate
/// </Summary>
Public Delegate void medelegate ();
/// <Summary>
/// Define a server abstract class
/// </Summary>
Abstract class master
{
Private string name;
Public string name
{
Get {return name ;}
Set {name = value ;}
}
/// <Summary>
/// Define an event
/// </Summary>
Public event medelegate Eve;
Public void notifly ()
{
If (Eve! = NULL)
{
Eve ();
}
}
Public abstract void is called ();
}
/// <Summary>
/// Define a cat class, inherited from the master abstract class
/// </Summary>
Class Cat: Master
{
Public override void ()
{
Console. writeline ("Hello Tom! ");
}
}
/// <Summary>
/// Define a client abstract class
/// </Summary>
Abstract class client
{
Private string name;
Public string name
{
Get {return name ;}
Set {name = value ;}
}
Public abstract void response ();

}
/// <Summary>
/// Rat inheritance and client abstract class
/// </Summary>
Class mouse: Client
{
Public override void response ()
{
Console. writeline ("the mouse escaped ");
}
}
/// <Summary>
/// Inherit the abstract class of the Client
/// </Summary>
Class person: Client
{
Public override void response ()
{
Console. writeline ("the master wakes up! ");
}
}
Class Program
{
Static void main (string [] ARGs)
{
Cat c = new CAT ();
Mouse M = new mouse ();
Person P = new person ();
// Registration time
C. Eve + = new medelegate (M. Response );
C. Eve + = new medelegate (P. Response );
C. Call ();
// Trigger the event
C. notifly ();
}
}
}

Through this example, we can deeply understand that an event is a way to provide notifications when a managed event occurs !!!!!

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.