Understand the callback mechanism in Java (translation) _java

Source: Internet
Author: User

Hello, today I want to share something with you, for example, this is used in JavaScript a lot. I want to talk about callbacks (callbacks). Do you know when to use, how to use this? Do you really understand its usage in the Java environment? When I ask myself these questions, that's why I started to study them. The idea behind this is control reversal (PS: Wikipedia's explanation is that control reversal (inversion of controls, abbreviated as IOC) is a design principle in object-oriented programming that can be used to reduce the coupling between computer code. This example describes how the framework works, as well as the "Hollywood principles: Don't call us, we'll call you (" Hollywood Principle-don ' t calls me, we will called you) "is known to everyone.

Simple Java callback pattern to understand it, the specific example below:

Interface CallBack {
 void Methodtocallback ();
}

Class Callbackimpl implements CallBack {public
 void Methodtocallback () {
  System.out.println ("I ' ve been called Back ");
 }

Class Caller {public

 void Register (CallBack CallBack) {
  callback.methodtocallback ();
 }

 public static void Main (string[] args) {
  Caller Caller = new Caller ();
  CallBack CallBack = new Callbackimpl ();
  Caller.register (CallBack);
 }

You might ask me, when do I use this or would I ask what is the difference between a direct call and a callback mechanism?

The answer is: Well, this example just shows you how to construct such a callback function in a Java environment. Of course it doesn't make sense to use it that way. Let's look at it in more detail now.

The idea in it is to control the reversal. Let's use the timer as an example in reality. Suppose you know that there is a special timer that supports the callback function every hour. To be exact, every hour, the timer calls your registered calling method.

Specific examples:

We want to update the time of the Web site hourly, and here is the example UML model:

Callback Interface:

Let's first define the callback interface:

Import java.util.ArrayList;
Import java.util.List;

For Example:let ' s assume so this interface is offered from your OS to be implemented interface
ck {
 void UpdateTime (long time);
}

This is your implementation.
For example:you want to update your website time every hour
class Websitetimeupdatercallback implements Rcallback {

 @Override public
 void UpdateTime (long time) {
  //print the updated time anywhere in your website ' s example
  System.out.println (time);
 }

In our example, the system timer supports the callback method:

This is the systemtimer implemented by your operating System (OS)//You don ' t know to how this timer was implemented. This is example just//show to your how it could looks like.
How do you could implement a//callback by yourself if your want to.

 Class Systemtimer {list<timeupdatercallback> callbacks = new arraylist<timeupdatercallback> ();
 public void Registercallbackforupdateseveryhour (Timeupdatercallback timercallback) {callbacks.add (timerCallBack); }

 // ... This systemtimer may have to logic here we don ' t know ...//At some point of the implementaion of this systemtimer (y ou don ' t know)//This method would be called and every registered TimerCallback/would be called. Every registered TimerCallback may have a totally//different implementation of the The Method UpdateTime () and I am//US
 Ed in different ways by different clients. public void onehourhasbeenexprired () {for (Timeupdatercallback timercallback:callbacks) {Timercallback.updatetim E (SyStem.currenttimemillis ()); }
 }
}

Finally, we have a virtual simple example of the site time update device:

This is our client. It is used in our WebSite example. It shall update
//The website ' s time every hour.
Class Websitetimeupdater {public

 static void Main (string[] args) {
  Systemtimer systemtimer = new Systemtimer (); C5/>timeupdatercallback websitecallbackupdater = new Websitetimeupdatercallback ();
  Systemtimer.registercallbackforupdateseveryhour (Websitecallbackupdater);
 }

Original: http://cleancodedevelopment-qualityseal.blogspot.com/2012/10/understanding-callbacks-with-java.html

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.