ActionScript中的事件派发器Java版实现
ActionScript中的事件派发器设计的非常灵活,虽然Java中也有事件,但终归没有这个用的顺手,于是就顺手写了一个
调用方式:
1 2 3 4 5 6 7 8 9 10 11 |
public static void main(String[] args) throws InterruptedException { EventManager.instance.addEventListener("myEvent", new EventHandler<Object, DataEvent<Object>>(null) { @Override protected void handler(DataEvent<Object> event) { System.out.println(event.getData()); } }); EventManager.instance.dispatchEvent(new DataEvent<Object>("myEvent", "haha")); } |
实现类:
函数类:
IFunction.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package net.code2048.common.function; /** * * @author 破晓(www.code2048.net) * * @param <T> * @param <P> */ public interface IFunction<T, P> { T apply(Object[] argArray); T call(Object... args); P getParent(); } |
Function.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
package net.code2048.common.function; /** * * @author 破晓(www.code2048.net) * * @param <T> * @param <P> */ public abstract class Function<T, P> implements IFunction<T, P> { public Function() { this(null); } protected P _parent; public Function(P parent) { _parent = parent; } @Override public abstract T apply(Object[] argArray); @Override public T call(Object... args) { return apply(args); } @Override public P getParent() { return _parent; } } |
Handler.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package net.code2048.common.function; /** * * @author 破晓(www.code2048.net) * * @param <P> * @param <D> */ public abstract class Handler<P,D> extends Function<Object,P> { public Handler(P parent) { super(parent); } public Handler() { super(null); } @SuppressWarnings("unchecked") @Override public Object apply(Object[] argArray) { handler((D) argArray[0]); return null; } public abstract void handler(D event); } |
事件类:
Event.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
package net.code2048.common.event; /** * * @author 破晓(www.code2048.net) * */ public class Event { private String _type; private boolean _cancelable; public Event(String type) { this(type, false); } public Event(String type, boolean cancelable) { _type = type; _cancelable = cancelable; } public String getType() { return _type; } public boolean getCancelable() { return _cancelable; } } |
DataEvent.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package net.code2048.common.event; /** * * @author 破晓(www.code2048.net) * * @param <T> */ public class DataEvent<T> extends Event { private T _data; public DataEvent(String type, T data) { super(type); _data = data; } public DataEvent(String type, T data, boolean cancelable) { super(type, cancelable); _data = data; } public T getData() { return _data; } public void setData(T data) { _data = data; } } |
EventHandler.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package net.code2048.common.event; import net.code2048.common.function.Function; /** * * @author 破晓(www.code2048.net) * * @param <P> * @param <E> */ public abstract class EventHandler<P,E extends Event> extends Function<Object,P> { public EventHandler(P parent) { super(parent); } @SuppressWarnings("unchecked") @Override public Object apply(Object[] argArray) { handler((E) argArray[0]); return null; } protected abstract void handler(E event); } |
IEventDispatcher.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package net.code2048.common.event; /** * * @author 破晓(www.code2048.net) * */ public interface IEventDispatcher { void addEventListener(String type, EventHandler<?,?> listener); void removeEventListener(String type, EventHandler<?,?> listener); boolean dispatchEvent(Event event); boolean hasEventListener(String type); } |
EventDispatcher.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
package net.code2048.common.event; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * * @author 破晓(www.code2048.net) * */ public class EventDispatcher implements IEventDispatcher { private Map<String, List<EventHandler<?,?>>> listenerList = new HashMap<String, List<EventHandler<?,?>>>(); /** * 注册事件监听 * @param type * @param listener * */ @Override public void addEventListener(String type, EventHandler<?,?> listener) { List<EventHandler<?,?>> typeList; if(listenerList.containsKey(type)) typeList = listenerList.get(type); else { typeList = new ArrayList<EventHandler<?,?>>(); listenerList.put(type, typeList); } typeList.add(listener); } /** * 移除指定类型和指定callback的单个事件监听 * @param type * @param listener * */ @Override public void removeEventListener(String type, EventHandler<?,?> listener) { if(listenerList.containsKey(type)) { List<EventHandler<?,?>> typeList = listenerList.get(type); if(typeList.contains(listener)) typeList.remove(listener); if(typeList.size() == 0) listenerList.remove(type); } } /** * 派发事件 * @param event * @return * */ @Override public boolean dispatchEvent(Event event) { if(event == null) return false; if(!listenerList.containsKey(event.getType())) return false; List<EventHandler<?,?>> typeList = listenerList.get(event.getType()); for(EventHandler<?,?> fun: typeList) { fun.call(event); } return true; } /** * 检查是否存在指定类型的监听 * @param type * @return * */ @Override public boolean hasEventListener(String type) { return listenerList.containsKey(type); } } |
EventManager.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
package net.code2048.common.event; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * * @author 破晓(www.code2048.net) * */ public class EventManager implements IEventDispatcher { private Map<String, List<EventHandler<?,?>>> listenerList = new HashMap<String, List<EventHandler<?,?>>>(); public static EventManager instance = new EventManager(); private EventManager(){} /** * 注册事件监听 * @param type * @param listener * */ public void addEventListener(String type, EventHandler<?,?> listener) { List<EventHandler<?,?>> typeList; if(listenerList.containsKey(type)) typeList = listenerList.get(type); else { typeList = new ArrayList<EventHandler<?,?>>(); listenerList.put(type, typeList); } typeList.add(listener); } /** * 移除指定类型和指定callback的单个事件监听 * @param type * @param listener * */ public void removeEventListener(String type, EventHandler<?,?> listener) { if(listenerList.containsKey(type)) { List<EventHandler<?,?>> typeList = listenerList.get(type); if(typeList.contains(listener)) typeList.remove(listener); if(typeList.size() == 0) listenerList.remove(type); } } /** * 移除指定类型的所有监听 * @param type * */ public void removeEventListeners(String type) { if(listenerList.containsKey(type)) listenerList.remove(type); } /** * 派发事件 * @param event * @return * */ public boolean dispatchEvent(Event event) { if(event == null) return false; if(!listenerList.containsKey(event.getType())) return false; List<EventHandler<?,?>> typeList = listenerList.get(event.getType()); for(EventHandler<?,?> fun: typeList) { fun.call(event); } return true; } /** * 对指定类型的监听派发数据 * @param type * @param data * @return * */ public boolean dispatch(String type, Object data) { if(!listenerList.containsKey(type)) return false; List<EventHandler<?,?>> typeList = listenerList.get(type); for(EventHandler<?,?> fun: typeList) { fun.call(data); } return true; } /** * 检查是否存在指定类型的监听 * @param type * @return * */ public boolean hasEventListener(String type) { return listenerList.containsKey(type); } } |