Design an in-memory event system that supports registering listeners and firing events.
Implement an EventDispatcher (or EventBus) supporting:
subscribe(eventType, listener)
unsubscribe(eventType, listener)
fire(eventType, eventPayload)
— invokes all listeners currently subscribed to
eventType
Where listener is a callback/function.
Assume subscribe, unsubscribe, and fire can be called concurrently from multiple threads.
Answer/discuss:
fire
be synchronous (caller thread) or asynchronous (thread pool / queue)? How does that change your design?
You may state reasonable assumptions (e.g., per-event-type ordering is or isn’t required; at-least-once vs exactly-once delivery within a single process).
Login required