一个很简单方便的事件处理方法。
使用效果
事件发布者:
//定义事件
public static EventTrans<String> AuthFailed = new EventTrans<String>();
//事件触发
AuthFailed.trigger(this, "内容");
事件处理者:
DataProess.AuthFailed.addHandler(new EventHandle<String>() {
@Override
public void Handle(Object sender, String e) {
//事件处理
}
});
实现
事件处理器:
public abstract class EventHandle<EventType> {
public abstract void Handle(Object sender, EventType e);
}
事件管理:
public class EventTrans<EventType> {
List<EventHandle<EventType>> handler;
public EventTrans() {
handler = new ArrayList<EventHandle<EventType>>();
}
public void addHandler(EventHandle<EventType> handle) {
handler.add(handle);
}
public void removeHandler(EventHandle<EventType> handle) {
handler.remove(handle);
}
public void trigger(Object sender, EventType e) {
for (EventHandle<EventType> handle : handler) {
handle.Handle(sender, e);
}
}
}