WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Design Patterns · Intermediate · question 23 of 100

What is the difference between the Observer pattern and the Event Aggregator pattern?

📕 Buy this interview preparation book: 100 Design Patterns questions & answers — PDF + EPUB for $5

Both the Observer and the Event Aggregator patterns are used to handle communication between different parts of a software system. However, they differ in the way they handle this communication.

The Observer pattern is used when there is a one-to-many relationship between objects, where the state of one object (the subject) affects the state of many other objects (the observers). The subject maintains a list of observers and notifies them when its state changes, so that they can update their own state accordingly. This pattern can be implemented in Java using the built-in ‘Observer‘ and ‘Observable‘ interfaces.

Here is an example implementation of the Observer pattern in Java:

import java.util.Observable;
import java.util.Observer;

public class Subject extends Observable {
    private int state;
    
    public void setState(int state) {
        this.state = state;
        setChanged();
        notifyObservers();
    }
    
    public int getState() {
        return state;
    }
}

public class ObserverImpl implements Observer {
    @Override
    public void update(Observable o, Object arg) {
        if (o instanceof Subject) {
            Subject subject = (Subject) o;
            int state = subject.getState();
            // Update observer's state based on subject's state
        }
    }
}

On the other hand, the Event Aggregator pattern is used to handle communication between many-to-many relationships. In this pattern, an event aggregator object collects events from many sources and notifies many listeners about them. The listeners subscribe to the aggregator for the events they are interested in, and the aggregator keeps track of them. When an event is published, the aggregator notifies all the subscribed listeners. This pattern can be implemented in Java using event listeners and dispatchers.

Here is an example implementation of the Event Aggregator pattern in Java:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class EventAggregator {
    private Map<String, List<EventListener>> listenersMap = new HashMap<>();
    
    public void subscribe(String eventType, EventListener listener) {
        List<EventListener> listeners = listenersMap.get(eventType);
        if (listeners == null) {
            listeners = new ArrayList<>();
            listenersMap.put(eventType, listeners);
        }
        listeners.add(listener);
    }
    
    public void unsubscribe(String eventType, EventListener listener) {
        List<EventListener> listeners = listenersMap.get(eventType);
        if (listeners != null) {
            listeners.remove(listener);
        }
    }
    
    public void publish(String eventType) {
        List<EventListener> listeners = listenersMap.get(eventType);
        if (listeners != null) {
            for (EventListener listener : listeners) {
                listener.onEvent(eventType);
            }
        }
    }
}

public interface EventListener {
    void onEvent(String eventType);
}

public class ListenerImpl implements EventListener {
    @Override
    public void onEvent(String eventType) {
        // Handle event
    }
}

In summary, the Observer pattern is used for one-to-many communication, whereas the Event Aggregator pattern is used for many-to-many communication. Both patterns are useful in different scenarios and can be implemented effectively in Java.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Design Patterns interview — then scores it.
📞 Practice Design Patterns — free 15 min
📕 Buy this interview preparation book: 100 Design Patterns questions & answers — PDF + EPUB for $5

All 100 Design Patterns questions · All topics