Observer Design Pattern in Java
1. Introduction
The Observer Pattern is used when you want one object (called Subject) to notify multiple objects (Observers) automatically whenever its state changes.
This pattern follows the Publisher-Subscriber model.
2. Real-World Analogy
Think about YouTube Notifications:
- A Channel (Subject) has multiple Subscribers (Observers).
- Whenever the channel uploads a new video, all subscribers get notified.
- The channel doesn’t know who exactly is watching, it just broadcasts.
3. Structure of Observer Pattern
- Subject → Maintains a list of observers and notifies them of changes.
- Observer → Defines the update method to receive notifications.
- Concrete Subject → Implements Subject, stores state.
- Concrete Observers → Implement Observer, react to updates.
4. Custom Implementation Example
Let’s create a YouTube Channel – Subscriber Notification System.
Step 1 – Observer Interface
public interface Observer {
void update(String videoTitle);
}
Step 2 – Subject Interface
public interface Subject {
void subscribe(Observer observer);
void unsubscribe(Observer observer);
void notifyObservers(String videoTitle);
}
Step 3 – Concrete Subject (YouTubeChannel)
import java.util.ArrayList;
import java.util.List;
public class YouTubeChannel implements Subject {
private List subscribers = new ArrayList<>();
private String channelName;
public YouTubeChannel(String channelName) {
this.channelName = channelName;
}
@Override
public void subscribe(Observer observer) {
subscribers.add(observer);
System.out.println("New subscriber added!");
}
@Override
public void unsubscribe(Observer observer) {
subscribers.remove(observer);
System.out.println("Subscriber removed!");
}
@Override
public void notifyObservers(String videoTitle) {
System.out.println("\n" + channelName + " uploaded: " + videoTitle);
for (Observer observer : subscribers) {
observer.update(videoTitle);
}
}
}
Step 4 – Concrete Observers (Subscribers)
public class Subscriber implements Observer {
private String name;
public Subscriber(String name) {
this.name = name;
}
@Override
public void update(String videoTitle) {
System.out.println(name + " received notification: New video uploaded - " + videoTitle);
}
}
Step 5 – Client Code
public class ObserverPatternDemo {
public static void main(String[] args) {
YouTubeChannel channel = new YouTubeChannel("TechWithJava");
Observer s1 = new Subscriber("Alice");
Observer s2 = new Subscriber("Bob");
Observer s3 = new Subscriber("Charlie");
channel.subscribe(s1);
channel.subscribe(s2);
channel.subscribe(s3);
channel.notifyObservers("Observer Pattern Tutorial");
channel.unsubscribe(s2);
channel.notifyObservers("Memento Pattern Tutorial");
}
}
5. Output
New subscriber added!
New subscriber added!
New subscriber added!
TechWithJava uploaded: Observer Pattern Tutorial
Alice received notification: New video uploaded - Observer Pattern Tutorial
Bob received notification: New video uploaded - Observer Pattern Tutorial
Charlie received notification: New video uploaded - Observer Pattern Tutorial
Subscriber removed!
TechWithJava uploaded: Memento Pattern Tutorial
Alice received notification: New video uploaded - Memento Pattern Tutorial
Charlie received notification: New video uploaded - Memento Pattern Tutorial
6. Advantages
- Implements a loose coupling between subject and observers.
- Supports broadcast communication easily.
- Makes the system more scalable since multiple observers can be added dynamically.
7. Real-World Use Cases
- YouTube / Instagram notifications.
- Stock market monitoring systems.
- Weather forecasting apps.
- Event-driven systems like GUIs.
Next- State Design Pattern in Java