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 !!!!!