比较典型的java回调案例-员工老板
2015-04-06 19:18
232 查看
下面是一个典型的回调案例:公司员工工作,工作完成后主管要求员工汇报工作完成情况。
事件接口:
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对象的方法,实现回调功能。设计模式中,观察者模式也是一个典型的应用回调机制的例子。
事件接口:
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对象的方法,实现回调功能。设计模式中,观察者模式也是一个典型的应用回调机制的例子。
相关文章推荐
- 使用JDBC连接MySQL数据库--典型案例分析(一)---员工数据读取与操作
- 使用JDBC连接MySQL数据库--典型案例分析(八)----实现员工数据的分页查询
- java基本对象Integer,String比较相等及equal案例说明
- 使用JDBC连接MySQL数据库--典型案例分析(四)----更新和插入员工数据
- Java TCP\UDP的典型案例
- java实现接口的典型案例
- java常见面试题之Integer,String比较相等及equal案例
- java-抽象类员工案例
- [数据库]关于三个比较典型的数据库试题(1.找到员工表中工资最高的前三名;2.找到员工表中薪水大于本部门平均薪水的员工;3.统计每年入职的员工个数)
- java 接口回调经典案例--网络请求回调
- 使用JDBC连接MySQL数据库--典型案例分析(七)----批量插入员工信息
- Java Collections---HashMap深度分析与比较
- C# 与 C 、 C++ 、 D 、 Java 的性能比较(三)
- 挺有意思的比较,JAVA与C#。
- Java Collections---HashMap深度分析与比较(zt)
- 每年12次考虑离职 员工心中的老板靠不住
- java做的比较完善的FTP连接上传下载文件
- Java中初学者比较爱出错的运算问题
- Java Collections---HashMap深度分析与比较
- 关于JAVA匿名内部类,回调,事件模式的一点讨论 VS dotNET事件模式