java回调函数实现

时间:2022-07-21 16:14:41

 

Java回调实现 有个这样的问题:老板(Boss)让工人(Worker)干完活告诉他干的情况如何。 实际上是一个典型的回调问题,Java实现如下: /**
* 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 EventAimplements Event {
    /**
     * 返回发生事件信息
     *
     * @return 事件信息
     */

    public String happendEvent() {
        return "发生了事件 EventA!";
    }
}
 /**
* Created by IntelliJ IDEA.
* User: leizhimin
* Date: 2008-7-31 13:18:21
* 事件B
*/

public class EventBimplements 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 staticvoid 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();

    }
}
 运行结果:张三 is doing working...
张三 was finished work.
老板接收到事件信息: 张三: 发生了事件 EventA!
李四 is doing working...
李四 was finished work.
老板接收到事件信息: 李四: 发生了事件 EventB!

Process finished with exit code 0
 呵呵,工人干活完成后,自动将发生的事件汇报给老板。 当然,如果工人很多,每个人都汇报的话,老板肯定会烦,那么老板可能会下达,发生了严重问题的员工才需要汇报问题。这个时候,需要做个事件过滤。  

本文出自 “熔 岩” 博客,转载请与作者联系!