例如,假设我们希望在某个时间发生时得到通知。我们可以定义一个接口:
Java代码
package com.wxy.callback;
public interface InterestingEvent {
/**
*这是一个常规方法,如果需要,可以有返回值,也可以接受参数
*/
public void interestingEvent();
}
这使得我们可以控制实现该接口的类得任何对象。因此,我们不必关心任何外部类型信息。发出事件信号的类必须等待实现了InterestingEvent接口的对象,并在适当时候调用interestingEvent()方法。
Java代码
package com.wxy.callback;
public class EventNotifier {
/**事件对象*/
private InterestingEvent ie;
/**事件发生状态*/
private boolean somethingHappend;
/**
* 构造函数,传入事件对象以备后用
* @param event 事件对象
*/
public EventNotifier(InterestingEvent event) {
this.ie = event;
//事件未发生状态
somethingHappend = false;
}
/**
* 事件发生,调用InterestingEvent事件发生对象的方法发出事件信号
*/
public void doWork() {
//检查在别处设置的谓词
if (somethingHappend) {
//通过调用接口的这个方法发出事件信号
ie.interestingEvent();
}
}
}
在EventNotifier中,使用somethingHappend谓词来跟踪是否哦应触发事件。在许多情况下,调用此方法足以保证向interesthingEvent()发出信号。
希望接受事件通知的代码必须实现Interestingevent接口,并将自身引用传递给事件通知程序。
Java代码
public class CallMe implements InterestingEvent {
private EventNotifier en;
/**
* 创建事件通知程序,并将自身引用传递给它
*/
public CallMe() {
this.en = new EventNotifier(this);
}
/**
* 为事件定义实际的处理程序
*/
@Override
public void interestingEvent() {
System.out.println("oh,something interesting has happended!");
System.out.println("do some action!");
}
public EventNotifier getEn() {
return en;
}
public void setEn(EventNotifier en) {
this.en = en;
}
}
下面是一个典型的回调案例:公司员工工作,工作完成后主管要求员工汇报工作完成情况。
事件接口:
Java代码
package com.wxy.callback;
public interface Event {
/**
* 返回发生事件信息
* @return 事件信息
*/
public String happendEvent();
}
事件具体实现类:
Java代码
package com.wxy.callback;
public class EventA implements Event {
@Override
public String happendEvent() {
return "job has been finished!";
}
}
Java代码
package com.wxy.callback;
public class EventB implements Event {
@Override
public String happendEvent() {
return "job has been finished!";
}
}
主管类:
Java代码
package com.wxy.callback;
public class Boss {
private String name;
public Boss(String name) {
this.name = name;
}
public void getStaffEvent(Staff staff, Event event) {
System.out.println("the msg what the boss received is--" + staff.getName() + ":"
+ event.happendEvent());
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
员工类:
Java代码
package com.wxy.callback;
public class Staff {
private Event event; //事件
private String name; //员工姓名
private Boss boss; //员工所属主管
/**
* 员工构造器
* @param name 员工姓名
* @param boss 传入Boss对象,便于回调反馈工作状况
*/
public Staff(String name, Boss boss) {
this.name = name;
this.boss = boss;
}
public void doWork() {
System.out.println(name + " is doing working...");
//do somtething.....
for (int i = 0; i < 10; i++) {
System.out.println("sheep" + i);
}
System.out.println(name + " was finished work!");
//tell the boss what has happend,这里就是boss的回调方法
boss.getStaffEvent(this, event);
}
public Event getEvent() {
return event;
}
public void setEvent(Event event) {
this.event = event;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Boss getBoss() {
return boss;
}
public void setBoss(Boss boss) {
this.boss = boss;
}
}
测试类:
Java代码
package com.wxy.callback;
public class StaffBossTest {
public static void main(String args[]) {
//初始化员工和主管
Boss boss = new Boss("boss");
Staff staffA = new Staff("staffA", boss);
Staff staffB = new Staff("staffB", boss);
//主管发放了两个新任务
Event event1 = new EventA();
Event event2 = new EventB();
//员工接受任务开始干活
staffA.setEvent(event1);
staffB.setEvent(event2);
//员工干晚活,及时向主管反馈工作情况
staffA.doWork();
staffB.doWork();
}
}
测试结果:
Java代码
staffA was finished work!
the msg what the boss received is--staffA:job has been finished!
staffB is doing working...
sheep0
sheep1
sheep2
sheep3
sheep4
sheep5
sheep6
sheep7
sheep8
sheep9
staffB was finished work!
the msg what the boss received is--staffB:job has been finished!
可以看到,当员工完成工作时(即触发某事件时),staff对象调用boss对象的方法,实现回调功能。设计模式中,观察者模式也是一个典型的应用回调机制的例子。