If there is no event, can we delegate it to implement the event function? Actually, it is okay. Next we will analyze it step by step! Here is an example:
In such a scenario, there is a balloon (equivalent to the sender of an event or message) and a balloon Explosion (equivalent to triggering an event ), when a child hears a balloon explosion, it will cry (the child is like a subscriber of an event ). We do not apply to events and use delegation to complete this program.
Step 1: define a global delegate type, Public Delegate void bombeventhandler (); // define a delegate. It is actually a class, which is equivalent to a delegate class.
Step 2: Define the balloonpublisher. It is very simple. This class only uses an explosive method from friends, and then sends a message. I blew it up, that is, calling the bomb delegate.
Public class balloonpublisher
{Public bombeventhandler bomb; // delegate Type Variable
Public void balloonbomb ()
{
If (bomb! = NULL)
{
Bomb (); // call the delegate
}
}
}
Step 3: We implement subscriber and Child class. To make it simple, we just need to define a method for getting scared.
Public class childrensubscribe
{
// Actually subscribe to Delegation
Public void onhearbomb ()
{
Console. writeline ("I was shocked and cried ");
}
}
After everything is defined, the client is associated:
Static void main (string [] ARGs)
{
// The client triggers the event and subscribes to the event
Balloonpublisher Pb = new balloonpublisher ();
Childrensubscribe sb = new childrensubscribe ();
PB. Bomb + = sb. onhearbomb; // are you familiar with this? How the key balloon explosion delegate and children hear the explosion
PB. balloonbomb (); // trigger the explosion event, which is actually a delegate
}
The execution results clearly show that when the explosion is triggered, the method of crying also runs on the delegate chain.
Review the above process. It is the same as the event function, indicating that the delegate can fully implement the event function! But why is there an event? In the above example, we can think that the balloon explosion belongs to the balloon itself, and it has nothing to do with the client. Only the balloon can trigger the explosion method, however, the above example can directly trigger the balloon explosion through the client, such as Pb. bomb (); directly triggers the explosion commission, and can also scare the children into crying. This destroys encapsulation. This is the first reason for the event.
If the balloon explodes, it will not only cause children to cry, but also cause adults to find the balloon to blow up, and the birds around will fly away and wait for a series of events. In this case, you need to add these methods to the delegated Pb. Bomb.
If the client accidentally disconnects or changes the delegate, it will cause a lot of trouble and all subscribed classes will fail.
In order to solve the above problems, the incident came into being. In fact, the incident is the encapsulation of the Delegate. I will not talk about the specific internal implementation. There are many online websites.
The following shows the program that uses events to complete this function: in fact, it is very simple to change the delegate in the ball to the event definition. In this way, you can directly call PB on the client. the bombevent (); Syntax is incorrect, so that encapsulation and events can only be triggered by balloons. It has nothing to do with the client.
Public class balloonpublisher
{
Public event bombeventhandler bombevent; // I added the suffix of the event.
Public void balloonbomb ()
{
If (bombevent! = NULL)
{
Bombevent (); // call the delegate
}
}
}