Observer Design Pattern in Java
The Observer design pattern is a behavioral pattern that defines a one-to-many dependency between objects. When one object (the subject) changes state, all its dependent objects (observers) are notified and updated automatically. This pattern is widely used in event-driven programming and is particularly useful in designing loosely coupled systems. Real-World Analogy Imagine a YouTube channel. When a user subscribes to a channel, they become an observer. When the channel uploads a new video, all subscribers get notified. The channel is the subject, and the subscribers are observers. Implementing Observer Pattern in Java Java provides built-in support for the Observer pattern through the java.util.Observer interface and java.util.Observable class (deprecated in Java 9). However, it is common to implement the pattern manually to gain more flexibility. Step 1: Define the Observer Interface interface Observer { void update(String message); } Each observer needs to implement this interface and define what happens when they receive an update. Step 2: Create the Subject (Observable) Interface import java.util.ArrayList; import java.util.List; interface Subject { void addObserver(Observer observer); void removeObserver(Observer observer); void notifyObservers(String message); } The Subject interface maintains a list of observers and provides methods to add, remove, and notify observers. Step 3: Implement the Concrete Subject class YouTubeChannel implements Subject { private List observers = new ArrayList(); private String channelName; public YouTubeChannel(String channelName) { this.channelName = channelName; } @Override public void addObserver(Observer observer) { observers.add(observer); } @Override public void removeObserver(Observer observer) { observers.remove(observer); } @Override public void notifyObservers(String message) { for (Observer observer : observers) { observer.update("New video from " + channelName + ": " + message); } } public void uploadVideo(String title) { System.out.println(channelName + " uploaded a new video: " + title); notifyObservers(title); } } Step 4: Implement the Concrete Observers class Subscriber implements Observer { private String name; public Subscriber(String name) { this.name = name; } @Override public void update(String message) { System.out.println(name + " received notification: " + message); } } Step 5: Test the Implementation public class ObserverPatternDemo { public static void main(String[] args) { YouTubeChannel channel = new YouTubeChannel("Tech Explained"); Observer subscriber1 = new Subscriber("Alice"); Observer subscriber2 = new Subscriber("Bob"); channel.addObserver(subscriber1); channel.addObserver(subscriber2); channel.uploadVideo("Observer Pattern in Java"); channel.removeObserver(subscriber1); channel.uploadVideo("Singleton Pattern in Java"); } } Expected Output Tech Explained uploaded a new video: Observer Pattern in Java Alice received notification: New video from Tech Explained: Observer Pattern in Java Bob received notification: New video from Tech Explained: Observer Pattern in Java Tech Explained uploaded a new video: Singleton Pattern in Java Bob received notification: New video from Tech Explained: Singleton Pattern in Java Conclusion The Observer pattern is a powerful tool for designing decoupled and maintainable code. It is commonly used in event-driven programming, GUI applications, and distributed systems. By following this pattern, you can build scalable and flexible systems where objects communicate without tight dependencies.

The Observer design pattern is a behavioral pattern that defines a one-to-many dependency between objects. When one object (the subject) changes state, all its dependent objects (observers) are notified and updated automatically. This pattern is widely used in event-driven programming and is particularly useful in designing loosely coupled systems.
Real-World Analogy
Imagine a YouTube channel. When a user subscribes to a channel, they become an observer. When the channel uploads a new video, all subscribers get notified. The channel is the subject, and the subscribers are observers.
Implementing Observer Pattern in Java
Java provides built-in support for the Observer pattern through the java.util.Observer
interface and java.util.Observable
class (deprecated in Java 9). However, it is common to implement the pattern manually to gain more flexibility.
Step 1: Define the Observer Interface
interface Observer {
void update(String message);
}
Each observer needs to implement this interface and define what happens when they receive an update.
Step 2: Create the Subject (Observable) Interface
import java.util.ArrayList;
import java.util.List;
interface Subject {
void addObserver(Observer observer);
void removeObserver(Observer observer);
void notifyObservers(String message);
}
The Subject
interface maintains a list of observers and provides methods to add, remove, and notify observers.
Step 3: Implement the Concrete Subject
class YouTubeChannel implements Subject {
private List<Observer> observers = new ArrayList<>();
private String channelName;
public YouTubeChannel(String channelName) {
this.channelName = channelName;
}
@Override
public void addObserver(Observer observer) {
observers.add(observer);
}
@Override
public void removeObserver(Observer observer) {
observers.remove(observer);
}
@Override
public void notifyObservers(String message) {
for (Observer observer : observers) {
observer.update("New video from " + channelName + ": " + message);
}
}
public void uploadVideo(String title) {
System.out.println(channelName + " uploaded a new video: " + title);
notifyObservers(title);
}
}
Step 4: Implement the Concrete Observers
class Subscriber implements Observer {
private String name;
public Subscriber(String name) {
this.name = name;
}
@Override
public void update(String message) {
System.out.println(name + " received notification: " + message);
}
}
Step 5: Test the Implementation
public class ObserverPatternDemo {
public static void main(String[] args) {
YouTubeChannel channel = new YouTubeChannel("Tech Explained");
Observer subscriber1 = new Subscriber("Alice");
Observer subscriber2 = new Subscriber("Bob");
channel.addObserver(subscriber1);
channel.addObserver(subscriber2);
channel.uploadVideo("Observer Pattern in Java");
channel.removeObserver(subscriber1);
channel.uploadVideo("Singleton Pattern in Java");
}
}
Expected Output
Tech Explained uploaded a new video: Observer Pattern in Java
Alice received notification: New video from Tech Explained: Observer Pattern in Java
Bob received notification: New video from Tech Explained: Observer Pattern in Java
Tech Explained uploaded a new video: Singleton Pattern in Java
Bob received notification: New video from Tech Explained: Singleton Pattern in Java
Conclusion
The Observer pattern is a powerful tool for designing decoupled and maintainable code. It is commonly used in event-driven programming, GUI applications, and distributed systems. By following this pattern, you can build scalable and flexible systems where objects communicate without tight dependencies.