* Created by IntelliJ IDEA.
* User: leizhimin
* Date: 2008-7-31 13:18:33
* 事件接口
*/
public interface Event {
/**
* 返回发生事件信息
*
* @return 事件信息
*/
public String happendEvent();
}
* Created by IntelliJ IDEA.
* User: leizhimin
* Date: 2008-7-31 13:18:14
* 事件A
*/
public class EventA implements Event {
/**
* 返回发生事件信息
*
* @return 事件信息
*/
public String happendEvent() {
return "发生了事件 EventA!";
}
}
* Created by IntelliJ IDEA.
* User: leizhimin
* Date: 2008-7-31 13:18:21
* 事件B
*/
public class EventB implements Event{
/**
* 返回发生事件信息
* @return 事件信息
*/
public String happendEvent() {
return "发生了事件 EventB!";
}
}
* Created by IntelliJ IDEA.
* User: leizhimin
* Date: 2008-7-31 13:20:48
* 工人
*/
public class Worker {
private Event event; //事件
private String name; //工人姓名
private Boss boss; //工人所属的老板
public Worker(String name, Boss boss) {
this.name = name;
this.boss = boss;
}
/**
* 干活
*/
public void doWork() {
System.out.println(name + " is doing working...");
//工人挺辛苦,干着枯燥乏味的重复工作,哈哈
for (int i = 0; i < 2000000; i++) {
int x = i / 234 + 234;
}
System.out.println(name + " was finished work.");
//向老板说明发生的情况
boss.getWorkerEvent(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;
}
}
* Created by IntelliJ IDEA.
* User: leizhimin
* Date: 2008-7-31 13:26:22
* 老板
*/
public class Boss {
private String name;
public Boss(String name) {
this.name = name;
}
/**
* 老板接收工人的事件
* @param worker 工人
* @param event 事件
*/
public void getWorkerEvent(Worker worker, Event event) {
System.out.println("老板接收到事件信息: "+worker.getName() + ": " + event.happendEvent());
}
}
* Created by IntelliJ IDEA.
* User: leizhimin
* Date: 2008-7-31 13:35:38
* 测试类
*/
public class Test {
public static void main(String args[]){
//初始化老板和工人
Boss boss = new Boss("老板");
Worker worker1= new Worker("张三",boss);
Worker worker2= new Worker("李四",boss);
//捏造两个事件
Event event1 = new EventA();
Event event2 = new EventB();
//事件是工人发出的
worker1.setEvent(event1);
worker2.setEvent(event2);
//工人干活,干完了通知老板干的情况如何
worker1.doWork();
worker2.doWork();
}
}
张三 was finished work.
老板接收到事件信息: 张三: 发生了事件 EventA!
李四 is doing working...
李四 was finished work.
老板接收到事件信息: 李四: 发生了事件 EventB!
Process finished with exit code 0