Reprint: C # Implements interface callbacks

Source: Internet
Author: User

Typically, we create an object and immediately go directly to the method that uses it. In some cases, however, you want to be able to invoke a method of this object after a scene has occurred or when the condition is met. The callback will resolve the problem of "delaying the invocation of object methods". The object of the called method is called the callback object.
The principle of implementing callbacks is as follows:
You first create a callback object, and then you create a controller object that tells the Controller object which method the callback object needs to be called. The Controller object is responsible for checking whether a scene is present or whether a condition is satisfied. The method of the callback object is called automatically when this scenario occurs or when this condition is met.
The following is a small example of a C # implementation callback.

usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceConsoleApplication1 {classProgram {Static voidMain (string[] args) {              //creates a controller object that passes in the callback object supplied to itController obj =NewController (NewCallBack ()); //Startobj.          Star (); }      }       Public InterfaceIback {voidrun (); }       Public classCallback:iback { Public voidrun () {//for brevity here just shows the next timeSystem.Console.WriteLine (DateTime.Now); }      }       Public classController { PublicIback callbackobj =NULL;//The callback object is referenced here          PublicController (Iback obj) { This. Callbackobj =obj; }           Public voidStar () {Console.WriteLine ("Tap the keyboard any key to display the current time until you press ESC to exit ....");  while(Console.readkey (true). Key! =consolekey.escape) {callbackobj.run (); }          }      } } 

As you can see, when the sample program runs, when the run () method that calls the callback object is determined by the user, the Controller object invokes a callback run () method every time the user taps a key. The key to implementing the loopback in this example is the introduction of the Iback interface.
The same effect can be achieved if you use the callback object directly without the Iback interface, as follows:

    Public classController { PublicCallBack callbackobj =NULL;//a reference to the callback object method          PublicController (CallBack obj) { This. Callbackobj =obj; }           Public voidStar () {Console.WriteLine ("Tap the keyboard any key to display the current time until you press ESC to exit ....");  while(Console.readkey (true). Key! =consolekey.escape) {callbackobj.run (); }          }      } 

But thinking carefully, the result of this is to bind the Controller class to the callback object, in case you have to modify the controller class's code if you need to invoke other types of objects.
If the controller class receives an abstract interface variable iback, any object that implements the interface can be called back by the Controller class object, and the controller class code is no longer modified to ensure that the code is adaptable to the environment. is undoubtedly a good solution.

Reprint: C # Implements interface callbacks

Related Article

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.