I am trying to create a system to respond to events that occur in my application, similar to the Observer pattern. In my system, EventProducertrigger events EventConsumerrespond to these events, and two are connected through a central hub:
For now, I will ignore EventProducerand focus on EventHuband EventConsumer:
interface EventConsumer<E extends Event> {
void respondToEvent(E event);
}
class EventHub {
private HashMap<Class<>, HashSet<EventConsumer<>>> subscriptions;
public <E extends Event> void fireEvent(E event) {
}
public <E extends Event> void subscribeToEvent(EventConsumer<E> consumer) {
}
}
The problem is the announcement HashMap: I want to be able to do something like
HashMap<Class<E extends Event>, HashSet<EventConsumer<E>>>
// or
<E extends Event> HashMap<Class<E>, HashSet<EventConsumer<E>>>
So, to EventConsumerbe parameterized with the same type Class, but the closest I can get
HashMap<Class<? extends Event>, HashSet<EventConsumer<? extends Event>>>
But then it would allow, for example, HashSet<EventConsumer<MouseClickEvent>>to Class<KeyPressEvent>be assigned , suggesting both KeyPressEvent, and a MouseClickEventsubclass Event.
subscribeToEvent: , , ,
subscriptions.get(E.class).put(consumer)
E .
? ?