The first was that the code presented could only easily fire one type of event. Which is easy to fix. In Java, or any similar language, whenever you want to expand from "one type of thing" to "multiple types", consider a Map. We define Broadcasters, which is essentially a
HashMap<Class<? extends EventObject>, Broadcaster>
The three main methods are straightforward:
public synchronized void addListener(Class eventClass, EventListener l) { Broadcaster b = classMap.get(eventClass); if (b != null) b.addListener(l); else if (l instanceof GenericListener) { // automatically handle these b = new Broadcaster.Generic(); b.addListener(l); classMap.put(eventClass, b); } else throw new RuntimeException("no broadcaster for " + eventClass); } public void fireEvent(EventObject event) { Broadcaster b = classMap.get(event.getClass()); if (b != null) b.fireEvent(event); } public void removeListener(Class eventClass, EventListener l) { classMap.get(eventClass).removeListener(l); }
One problem solved. As for having multiple listeners in a class, that's trickier. A class cannot declare that it implements GenericListener
addListener(Foo.class, new GenericListener() { @Override public void handleEvent(Foo event) { // do something here } }
No comments:
Post a Comment