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);
}
}
import java.util.ArrayList;
import java.util.List;
// For example: Let's assume that this interface is offered from your OS to be implemented
interface TimeUpdaterCallBack {
void updateTime(long time);
}
// this is your implementation.
// for example: You want to update your website time every hour
class WebSiteTimeUpdaterCallBack implements TimeUpdaterCallBack {
@Override
public void updateTime(long time) {
// print the updated time anywhere in your website's example
System.out.println(time);
}
}
// 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());
}
}
}
// 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);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有