How to strip the specific implementation of a specific action (event + =)

Source: Internet
Author: User

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 );
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.