Class Person
{
Public Person (string name)
{
This. name = name;
}
// Some attributes and Methods
String name;
Public void Eat (Food food );
}
The Person class has an Eat method. For its instances, Zhang San, Li Si, and Wang Wu both have their own Eat methods.
Person zhanSan, liSi, wangWu;
ZhanSan = new Person ("James ");
LiSi = new Person ("Li Si ");
WangWu = new Person ("Wang Wu ");
We can call the Eat method by defining a delegate.
Public void delegate Eat (Food food );
If you want to call zhanSan. Eat (food)
Eat zhanSanEat = new Eat (zhanSan. Eat );
Others are similar:
Eat liSiEat = new Eat (liSi. Eat );
Eat wangWu = new Eat (wangWu. Eat );
Therefore, calling zhanSanEat (food) is equivalent to calling zhanSan. Eat (food)
The most useful thing about delegation is the delegation chain. If Zhang San, Li Si, and Wang Wu eat together
You can define the delegate as follows:
// Define a composite delegate
Eat togetherEat;
// Connect the method to the delegate through "+" in c #, that is, to add the delegate to the delegate chain
// Use "-" to delete the method from the delegate chain
TogetherEat = zhanSanEat + liSiEat + wangwuEat;
// Zhang San, Li Si, and Wang Wu eat watermelon together
TogetherEat (watermelon );
// No. It's only Li Si and Wang Wu.
TogetherEat = liSiEat + wangWuEat;
TogetherEat (watermelon );
The event mechanism in. net is implemented through delegation.
Below is a piece of source code on C # technology unveiling. I added some comments to explain the implementation mechanism of the event.
// The InventoryManager class is used to update inventory. It also defines the event that should be triggered when inventory is updated, that is, it publishes a delegate called by the subscriber.
// The InventoryWatcher class defines the subscriber. You can select whether to add the subscriber to the publisher list to receive a notification when updating the inventory.
Using System;
Namespace DelegateEvents
{
// Define the EventArgs derived class to carry event information
Class InventoryChangeEventArgs: System. EventArgs
{
Public InventoryChangeEventArgs (string sku, int change)
{
This. sku = sku;
This. change = change;
}
String sku;
Public String SKU
{
Get {return SKU ;}
}
Int change;
Public int change
{
Get {return change ;}
}
}
// Publisher
Class inventorymanager
{
// Declare a delegate. Two parameters are required. The first parameter is the publisher object, and the second parameter must be the eventargs class or its derived class.
Public Delegate void inventorychangeeventhandler (Object source, inventorychangeeventargs E );
// Defines the event instance of the Delegate (that is, the delegate chain, used by the subscriber to add himself to the delegate chain
Public event inventorychangeeventhandler oninventorychangehandler;
// Inventorymanager method, used to update inventory
Public void updateinventory (string SKU, int change)
{
If (0 = change)
Return;
// Define the event parameter instance and pass SKU and change information
Inventorychangeeventargs E = new inventorychangeeventargs (SKU, change );
// Determines whether the delegated linked list is empty. If it is not empty, a subscriber has subscribed.
If (this. OnInventoryChangeHandler! = Null)
{
Console. WriteLine ("[InventoryManager. UpdateInventory] Raising event to all subscribers... \ n ");
// Call the methods on the delegate linked list in sequence
This. OnInventoryChangeHandler (this, e );
}
}
}
// Subscriber
Class InventoryWatcher
{
// Define the publisher
InventoryManager invnetoryManager;
Public InventoryWatcher (InventoryManager inventoryManager)
{
Console. WriteLine ("[InventoryWatcher. InventoryWatcher] Subscribing to InventoryChange event \ n ");
This. invnetoryManager = inventoryManager;
// Connect yourself to InventoryManager. InventoryChangeEventHandler to delegate
InventoryManager. OnInventoryChangeHandler + = new InventoryManager. InventoryChangeEventHandler (OnInventoryChange );
}
// Subscriber method, called when the publisher updates inventory
Void oninventorychange (Object source, inventorychangeeventargs E)
{
Int change = E. Change;
Console. writeline ("[inventorymanager. oninventorychange] \ n \ tpart {0} was {1} By {2} units \ n", E. SKU, change> 0? "Increased": "decreased", math. Abs (E. Change ));
}
}
/// <Summary>
/// Use a delegate to implement an event mechanism instance
/// </Summary>
Class delegateevents
{
/// <Summary>
/// Main entry point of the application.
/// </Summary>
[Stathread]
Static void main (string [] ARGs)
{
// Define the delegate Publisher Object
Inventorymanager = new inventorymanager ();
Console. writeline ("[delegateevents. Main instantiating subscriber object \ n ");
// Define the subscriber object
InventoryWatcher inventWatcher = new InventoryWatcher (inventoryManager );
InventoryManager. UpdateInventory ("111 006 116", 2 );
InventoryManager. UpdateInventory ("111 005 383", 5 );
Console. ReadLine ();
}
}
}