How can we strip the specific implementation of specific actions? For example, when a person walks on the road and meets a friend, you will say hello to him. If a person encounters a vehicle, you will give it up in time to avoid injury .. Technically speaking, the client is a scenario action, that is, the failedeventargs of the specific execution action, that is, the Custom Event class customereventprovider, that is, an event provider, and another benefit of this Code: the scalability is good. To process the desired action, you only need to change "specific implementation. In fact, if there are many actions, use the command mode or the mode you think is acceptable. Please refer to the following link for details: Program : I use the method of defining events to strip the specific implementation of specific actions. Come on beat!
By caviare, brianlei
The procedure is as follows:
/// Call
New client ("car ");
///
Public class client
{
Customereventprovider CEP;
Public client (string fall1_ss ){
CEP = new customereventprovider ();
/// Before taking a walk, you can define several events (Actions) in advance and automatically execute specific actions in case of any situation to give a response.
CEP. onfailed + = new customereventprovider. failedhandler (action. Failed );
CEP. onsuccess + = new customereventprovider. successhandler (action. Success );
Walk (fall1_ss );
}
Public void walk (string fall1_ss) {
// The rough points written here. The specific mode can be used here. If, else can be discarded.
// I will not write it here. It provides an idea
If (fall1_ss. equals ("people") {
CEP. showsuccess (this, new eventargs ();
}< br> else {
failedeventargs FEA = new failedeventargs ();
CEP. showfailed (this, FEA);
}< BR >}
/// <Summary>
/// Class of the specific action to be executed. The specific implementation can be specified here.
/// </Summary>
Public class action
{
Public static void success (Object sender, eventargs e ){
Console. writeline ("success ");
}
Public static void failed (Object sender, failedeventargs e ){
E. eventmessage = "failed ";
Console. writeline (E. eventmessage );
}
}
/// <Summary>
/// Custom Event class
/// Used to: Define some eventcode, message etc.
/// </Summary>
Public class failedeventargs: eventargs
{
Private string eventcode;
Public String eventcode {
Get {return eventcode ;}
Set {eventcode = value ;}
}
Private string eventmessage;
Public String eventmessage {
Get {return eventmessage ;}
Set {eventmessage = value ;}
}
}
/// <Summary>
/// Event provider, which defines various actions
/// </Summary>
Public class customereventprovider
{
Public Delegate void failedhandler (Object sender, failedeventargs E );
Public event failedhandler onfailed;
Public void showfailed (Object sender, failedeventargs e ){
If (onfailed! = NULL ){
Onfailed (sender, e );
}
}
Public Delegate void successhandler (Object sender, eventargs E );
Public event successhandler onsuccess;
Public void showsuccess (Object sender, eventargs e ){
If (onsuccess! = NULL ){
Onsuccess (sender, e );
}
}
}