博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java callback
阅读量:1970 次
发布时间:2019-04-27

本文共 2559 字,大约阅读时间需要 8 分钟。

CALLBACK PATTERN IN JAVA ENVIRONMENT


Hi there! today i wanna share something with you, that it is very common and widely used in javascript for example. I'm speaking of callbacks. Do you know how and when this "pattern" is used? Do you really understand it in a java context (environment)? Well i was asking me also some of those questions and that's the reason i started to learn more about it. The ideia behind it is the inversion of control (abbreviated IoC). This paradigma describes the way frameworks work. It is also known as the "Hollywood principle - Don't call me, we will call you"

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);    }}
// 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
callbacks = new ArrayList
(); 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);    }}

你可能感兴趣的文章
golang实现大数据量文件的排序
查看>>
golang中的time包
查看>>
2019NOIP D4题 加工领奖
查看>>
2021.5.19 JS高级第二天
查看>>
SpringBoot内置Tomcat配置参数
查看>>
ubuntu 快捷键
查看>>
linux 根目录下文件夹分析
查看>>
linux 查看分区和文件大小
查看>>
Not using PCAP_FRAMES 解释(snort中)
查看>>
技术转管理?这些“坑”你要绕道走
查看>>
领域驱动设计(DDD)前夜:面向对象思想
查看>>
Camera驱动调试小记
查看>>
四线触摸屏原理
查看>>
C/C++如何返回一个数组/指针
查看>>
腾讯AI语音识别API踩坑记录
查看>>
YbtOJ——递推算法【例题4】传球游戏
查看>>
安装openrave 0.9的各种依赖包
查看>>
kpm代码使用细节
查看>>
@FeignClient注解的重复名称解决
查看>>
java.net.BindException: 无法指定被请求的地址
查看>>