Hello, today I want to share something with you. For example, this is used a lot in JavaScript. I'm going to talk about callbacks. Do you know when and how to use this? Do you really understand its usage in the java environment? When I asked myself these questions, that's why I started researching this. The idea behind this is Inversion of Control (PS: Wikipedia’s explanation is Inversion of Control, abbreviated as IoC), which is a design principle in object-oriented programming that can be used to reduce the coupling between computer codes. Degree.) This example describes how the framework works, also based on the "Hollywood principle - Don't call me, we will call you" well known.
Use the simple callback pattern in Java to understand it. The specific examples are as follows:
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 may want to ask me, when to use this or what is the difference between direct call and callback mechanism?
The answer is: Well, this example just shows you how to construct such a callback function in the java environment. Of course there's no point in using it that way. Let us now examine it in more detail.
The idea within it is inversion of control. Let's use a timer as a real-life example. Suppose you know that there is a special timer that supports hourly callbacks. To be precise, it means that every hour, the timer will call the calling method you registered.
Specific examples:
We want to update the website every hour. Here is the UML model for the example:
Callback interface:
Let's first define the callback interface:
import java.util.ArrayList;import java.util.List;// For example: Let's assume that this interface is offered from your OS to be implementedinterface TimeUpdaterCallBack { void updateTime(long time);}// this is your implementation./ / for example: You want to update your website time every hourclass WebSiteTimeUpdaterCallBack implements TimeUpdaterCallBack { @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 callback methods:
// This is the SystemTimer implemented by your Operating System (OS)// You don't know how this timer was implemented. This example just// show to you how it could looks like. How you could implement a// callback by yourself if you want to.class SystemTimer { List<TimeUpdaterCallBack> callbacks = new ArrayList<TimeUpdaterCallBack>(); public void registerCallBackForUpdatesEveryHour(TimeUpdaterCallBack timerCallBack) { callbacks.add(timerCallBack); } // ... This SystemTimer may have more logic here we don't know ... // At some point of the implementaion of this SystemTimer (you don't know) / / this method will be called and every registered timerCallBack // will be called. Every registered timerCallBack may have a totally // different implementation of the method updateTime() and my be // used in different ways by different clients. public void oneHourHasBeenExprired() { for (TimeUpdaterCallBack timerCallBack : callbacks) { timerCallBack.updateTime(System.currentTimeMillis()); } }}
Finally, here’s the website time updater in our dummy simple example:
// This is our client. It will be 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(); TimeUpdaterCallBack webSiteCallBackUpdater = new WebSiteTimeUpdaterCallBack(); SystemTimer.registerCallBackForUpdatesEveryHour(webSiteCallBackUpdater); }}
Original text: http://cleancodedevelopment-qualityseal.blogspot.com/2012/10/understanding-callbacks-with-java.html