java flash tcp字节流通信(五)-java 伪事件处理器(新)
来源:互联网 发布:js json push 编辑:程序博客网 时间:2024/06/05 03:09
package com.event;
/**
* 事件
*/
public class Event {
/**发起事件的对象*/
private Object source;
/**事件类型*/
private int type;
/**参数*/
private Object[] args;
public Event(int type, Object ...args){
this.type = type;
this.args = args;
}
public Object getSource() {
return source;
}
void setSource(Object source){
this.source = source;
}
public int getType() {
return type;
}
public Object[] getArgs() {
return args;
}
}
------------------------------------------------
package com.event;
/**
*
* 事件监听器
*
*/
public interface EventListener {
/**
*
* @param event
* @throws Exception
*/
public void eventPerformed(Event event) throws Exception;
}
------------------------------------------------
package com.event;
import java.util.HashMap;
import java.util.Map;
/**
*
* 事件派发器
*
*/
public abstract class EventDispatcher {
/**{key=EventType,value={key=被监听对象,value=EventListeners}}*/
private Map<Integer, EventListener> listeners;
public EventDispatcher(){
listeners = new HashMap<Integer, EventListener>();
}
/**
*
* @param eventType 事件类型
* @param target 被监听对象
* @param listener 监听器
* @throws Exception
*/
public void addListener(int eventType, EventListener listener) throws Exception{
/**添加监听器,事件类型必须是注册过的*/
listeners.put(eventType, listener);
}
/**
* 移除事件监听器
* @param evenType 事件类型
* @param target 被监听对象
*/
public void removeListener(int eventType){
listeners.remove(eventType);
}
/**
* 触发事件
* @param event
* @return
*/
public boolean dispatchEvent(Event event) throws Exception{
EventListener listener = listeners.get(event.getType());
if(listener != null){
event.setSource(this);
listener.eventPerformed(event);
return true;
}
return false;
}
}
------------------------------------------------
测试类:
package com.event.test;
import com.event.Event;
import com.event.EventDispatcher;
import com.event.EventListener;
public class EventTest extends EventDispatcher{
public final static int LOGIN = 1;
public final static int LOGOUT = 2;
public EventTest() throws Exception{
EventListener listener = null;
listener = new EventListener(){
public void eventPerformed(Event event){
login(event);
}
};
this.addListener(LOGIN, listener);
listener = new EventListener(){
public void eventPerformed(Event event){
logout(event);
}
};
this.addListener(LOGOUT, listener);
}
public void login(Event event){
System.out.println((String)event.getArgs()[0] + " login...");
}
public void logout(Event event){
System.out.println((String)event.getArgs()[0] + " logout...");
}
public static void main(String args[]) throws Exception{
EventTest test = new EventTest();
test.dispatchEvent(new Event(EventTest.LOGIN, "admin"));
test.dispatchEvent(new Event(EventTest.LOGOUT, "admin"));
test.removeListener(EventTest.LOGIN);
test.dispatchEvent(new Event(EventTest.LOGIN, "admin"));
test.dispatchEvent(new Event(EventTest.LOGOUT, "admin"));
}
}
测试结果:
admin login...
admin logout...
admin logout...
- java flash tcp字节流通信(五)-java 伪事件处理器(新)
- java flash tcp字节流通信(五)-java 伪事件处理器
- java flash tcp字节流通信(四)-flash as3 粘包/半包处理器(数据缓存)
- java flash tcp字节流通信(四)-java 粘包/半包处理器(数据缓存)
- java flash tcp字节流通信(二)-flash as3客户端
- java flash tcp字节流通信(一)-java 协议(1)
- java flash tcp字节流通信(一)-java 协议(3)
- java flash tcp字节流通信(一)-java 协议(4)
- java flash tcp字节流通信(一)-java 协议(5)
- java flash tcp字节流通信(一)-java 协议(2)
- java flash tcp字节流通信(一)-java 协议(6)
- java flash tcp字节流通信(二)-java 服务端
- java flash tcp字节流通信(一)-flash as3 协议(1)
- java flash tcp字节流通信(一)-flash as3 协议(2)
- java flash tcp字节流通信(一)-flash as3 协议(3)
- java flash tcp字节流通信(一)-flash as3 协议(4)
- java flash tcp字节流通信(一)-flash as3 协议(5)
- java flash tcp字节流通信(四)-flash as3 客户端(粘包/半包处理)
- TRIZ--揭示了创造发明的内在规律和原理
- 将字节流byte[]转换成成文件
- chapter5 使用Hibernate完成对象持久化
- 在Java7中如何操作文件
- JVM BUG: Internal Error (classFileParser.cpp:3161), pid=1116, tid=3912 Error: ShouldNotReachHere()
- java flash tcp字节流通信(五)-java 伪事件处理器(新)
- 编程珠玑题目集锦
- jQuery 语法
- Android数据存储方式之SharedPreferences
- delphi 调用系统内存整理函数
- java拼接sql语句例子
- 简单的按钮样式
- TStringList的Find(慎用),IndexOf和Sort
- 我最喜欢的10条编程语录