Browser docs

Event Aggregator


Event Aggregator


A system with lots of objects can lead to complexities when a client wants to subscribe to events. The client has to find and register for each object individually, if each object has multiple events then each event requires a separate subscription. An Event Aggregator acts as a single source of events for many objects. It registers for all the events of the many objects allowing clients to register with just the aggregator.


Real-world example

King Joffrey sits on the iron throne and rules the seven kingdoms of Westeros. He receives most of his critical information from King’s Hand, the second in command. King’s hand has many close advisors himself, feeding him with relevant information about events occurring in the kingdom.

In Plain Words

Event Aggregator is an event mediator that collects events from multiple sources and delivers them to registered observers.

Programmatic Example

In our programmatic example, we demonstrate the implementation of an event aggregator pattern. Some of the objects are event listeners, some are event emitters, and the event aggregator does both.

 1public interface EventObserver {
 2  void onEvent(Event e);
 5public abstract class EventEmitter {
 7  private final Map<Event, List<EventObserver>> observerLists;
 9  public EventEmitter() {
10    observerLists = new HashMap<>();
11  }
13  public final void registerObserver(EventObserver obs, Event e) {
14    ...
15  }
17  protected void notifyObservers(Event e) {
18    ...
19  }

KingJoffrey is listening to events from KingsHand.

2public class KingJoffrey implements EventObserver {
3  @Override
4  public void onEvent(Event e) {
5"Received event from the King's Hand: {}", e.toString());
6  }

KingsHand is listening to events from his subordinates LordBaelish, LordVarys, and Scout. Whatever he hears from them, he delivers to KingJoffrey.

 1public class KingsHand extends EventEmitter implements EventObserver {
 3  public KingsHand() {
 4  }
 6  public KingsHand(EventObserver obs, Event e) {
 7    super(obs, e);
 8  }
10  @Override
11  public void onEvent(Event e) {
12    notifyObservers(e);
13  }

For example, LordVarys finds a traitor every Sunday and notifies the KingsHand.

2public class LordVarys extends EventEmitter implements EventObserver {
3  @Override
4  public void timePasses(Weekday day) {
5    if (day == Weekday.SATURDAY) {
6      notifyObservers(Event.TRAITOR_DETECTED);
7    }
8  }

The following snippet demonstrates how the objects are constructed and wired together.

 1    var kingJoffrey = new KingJoffrey();
 3    var kingsHand = new KingsHand();
 4    kingsHand.registerObserver(kingJoffrey, Event.TRAITOR_DETECTED);
 5    kingsHand.registerObserver(kingJoffrey, Event.STARK_SIGHTED);
 6    kingsHand.registerObserver(kingJoffrey, Event.WARSHIPS_APPROACHING);
 7    kingsHand.registerObserver(kingJoffrey, Event.WHITE_WALKERS_SIGHTED);
 9    var varys = new LordVarys();
10    varys.registerObserver(kingsHand, Event.TRAITOR_DETECTED);
11    varys.registerObserver(kingsHand, Event.WHITE_WALKERS_SIGHTED);
13    var scout = new Scout();
14    scout.registerObserver(kingsHand, Event.WARSHIPS_APPROACHING);
15    scout.registerObserver(varys, Event.WHITE_WALKERS_SIGHTED);
17    var baelish = new LordBaelish(kingsHand, Event.STARK_SIGHTED);
19    var emitters = List.of(
20        kingsHand,
21        baelish,
22        varys,
23        scout
24    );
27        .<Consumer<? super EventEmitter>>map(day -> emitter -> emitter.timePasses(day))
28        .forEachOrdered(emitters::forEach);

The console output after running the example.

18:21:52.955 [main] INFO com.iluwatar.event.aggregator.KingJoffrey - Received event from the King's Hand: Warships approaching
18:21:52.960 [main] INFO com.iluwatar.event.aggregator.KingJoffrey - Received event from the King's Hand: White walkers sighted
18:21:52.960 [main] INFO com.iluwatar.event.aggregator.KingJoffrey - Received event from the King's Hand: Stark sighted
18:21:52.960 [main] INFO com.iluwatar.event.aggregator.KingJoffrey - Received event from the King's Hand: Traitor detected

Class diagram

alt text


Use the Event Aggregator pattern when

  • Event Aggregator is a good choice when you have lots of objects that are potential event sources. Rather than have the observer deal with registering with them all, you can centralize the registration logic to the Event Aggregator. As well as simplifying registration, an Event Aggregator also simplifies the memory management issues in using observers.