Java异步编程第2篇

来源:互联网 发布:语音包软件 编辑:程序博客网 时间:2024/04/28 05:26

假如现在有一个Button按钮,Button按钮上有click和doubleclick事件。两个不同的事件需要进行不同的处理,这时候就需要为相应的事件注册Listener了。修改后的目录组织结构如下:


1、事件基本类的编写如下:

package com.event.test02;public class Event {public String action; // 事件名称public String message; // 附加说明/** * @param action * @param message */public Event(String action, String message) {this.action = action;this.message = message;}}
package com.event.test02;public class EventNames {/** * 单击事件 */public static final String Click = "Click";/** * 双击事件 */public static final String DoubleClick = "DoubleClick";}
package com.event.test02;import com.event.test02.exception.EventAlreadyRegisteredException;import com.event.test02.handler.ClickHandler;import com.event.test02.handler.DoubleClickHandler;public class EventBinder {public static void bindEvents(){try {EventDispatcher eventDispatcher = EventDispatcher.getInstance();eventDispatcher.addEventListener(EventNames.Click, new ClickHandler());eventDispatcher.addEventListener(EventNames.DoubleClick, new DoubleClickHandler());} catch (EventAlreadyRegisteredException e) {e.printStackTrace();}}}
package com.event.test02;import java.util.HashMap;import com.event.test02.exception.EventAlreadyRegisteredException;import com.event.test02.exception.UnknowEventException;import com.event.test02.handler.EventListener;public class EventDispatcher {private static EventDispatcher eventDispatcher;private EventDispatcher(){}/** * 获取实例 * @return EventDispatcher */public static EventDispatcher getInstance(){if(eventDispatcher == null){eventDispatcher = new EventDispatcher();}return eventDispatcher;}    HashMap<String, EventListener> map = new HashMap<String, EventListener>();        /**     * 添加监听器     * @param event     * @param listener     * @throws EventAlreadyRegisteredException     */    public void addEventListener(String event, EventListener listener)     throws EventAlreadyRegisteredException {    if(map.get(event) != null){throw new EventAlreadyRegisteredException();    }    map.put(event, listener);    }    /**     * 移除某一个监听器     * @param event     */    public void removeEventListener(String event) {    map.remove(event);    }    /**     * @param e     * @throws UnknowEventException     */    public void dispatchEvent(Event e) throws UnknowEventException {    EventListener listener = map.get(e.action);    if(listener == null){    throw new UnknowEventException();    }else{    listener.handleEvent(e);    }    }    /**     * 移除所有监听器     */    public void removeAllListeners() {    map.clear();    }}


2、监听器类的编写如下

package com.event.test02.handler;import com.event.test02.Event;public interface EventListener {public void handleEvent(Event e);}
package com.event.test02.handler;import com.event.test02.Event;public class DoubleClickHandler implements EventListener{public void handleEvent(Event e) {System.out.println("hand  doubleclick event ....");}}
package com.event.test02.handler;import com.event.test02.Event;public class ClickHandler implements EventListener{public void handleEvent(Event e) {System.out.println("hand  click event ....");}}

3、自定义异常处理类

package com.event.test02.exception;public class EventAlreadyRegisteredException extends Exception {private static final long serialVersionUID = 741821449383395827L;}
package com.event.test02.exception;public class UnknowEventException extends Exception {private static final long serialVersionUID = 4829929946904208467L;}
最后运行的结果如下:

hand  click event ....
hand  doubleclick event ....


如上的程序灵活了不少,其组织也非常清晰。在使用的时候不要拘泥于某一种形式。在实际项目开发的过程中,可以将许多大的操作封装为一个专门的任务Task,也可以启动若干个承担不同任务的线程。只要给这些任务定义一个事件名称,当需要执行某个任务时,利用事件触发即可。


有兴趣的读者可以去看一个框架disruptor,推荐的地址如下:

http://ifeve.com/disruptor/















0 0
原创粉丝点击