编程语言
首页 > 编程语言> > Java异步编程第2篇

Java异步编程第2篇

作者:互联网

假如如今有一个Buttonbutton,Buttonbutton上有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/















标签:异步,Java,编程,public,test02,import,com,event,Event
来源: https://www.cnblogs.com/ldxsuanfa/p/10801644.html