I conducted a training session yesterday to explain the application of delegation and events. To be honest, it sounds confused. Although I learned how to use it, I haven't figured out when to use it, later, our manager gave me an example of a typical delegate and event, that is, the cat wakes up and runs the mouse.
First of all, it is easy to define three classes: Cat, People, and Mouse. They are respectively named Cat, People, and Mouse. Here I also wrote an interface, it writes a void Action () to implement polymorphism. The People and Mouse classes inherit this interface and implement it, and then define a delegate public delegate void Scream (), the return type is void, and an event public event Scream scream is defined in the Cat class to bind the following events. Next, we need to write a virtual class to serve as an observer, while the Cat class is an observer. When a method in the Cat class is called, The method bound to the event is triggered, here I wrote a virtual class to assume the role of the observer. The Cat class is used as the observer. When the Cat class is called, The Observer tells the other two classes that the Cat class has been called, then the two classes implement their respective methods. That is to say, the observer will wake up and the mouse will start to run after watching the cat scream, this is the different actions of different roles triggered by the same thing, and the constructor method of the virtual class is bound to the virtual method Action (). The code for the virtual class is as follows:
Public abstract class Observer: IScream
{
Protected Cat {get; set ;}
Public Observer (Cat cat)
{
This. cat = cat;
This. cat. scream + = Action;
}
Public abstract void Action ();
}
The human and mouse classes inherit from this virtual class. By Rewriting the virtual method Action to achieve the effect of polymorphism, the base is used to call the constructor of the parent class, the human and mouse code is as follows:
Class Person: Observer
{
Public override void Action ()
{
Console. WriteLine ("the master woke up ");
}
Public Person (Cat cat): base (cat)
{
}
}
Class Mouse: Observer
{
Public override void Action ()
{
Console. WriteLine ("the mouse ran ");
}
Public Mouse (Cat cat): base (cat)
{
}
}
After the definition is completed, you only need to call the Cat class method to achieve the expected effect. The overall code is as follows:
Public delegate void Scream ();
Public class Cat
{
Public event Scream scream;
Public void Action ()
{
Console. WriteLine ("A cat call ");
Scream ();
}
}
Public abstract class Observer: IScream
{
Protected Cat {get; set ;}
Public Observer (Cat cat)
{
This. cat = cat;
This. cat. scream + = Action;
}
Public abstract void Action ();
}
Class Mouse: Observer
{
Public override void Action ()
{
Console. WriteLine ("the mouse ran ");
}
Public Mouse (Cat cat): base (cat)
{
}
}
Class Person: Observer
{
Public override void Action ()
{
Console. WriteLine ("the master woke up ");
}
Public Person (Cat cat): base (cat)
{
}
}
Class Program
{
Static void Main (string [] args)
{
Cat cat = new Cat ();
Person p = new Person (cat );
Mouse m = new Mouse (cat );
Cat. Action ();
Console. ReadLine ();
}
}