Hi Everyone,
Let’s understand the Observer Design Pattern. Observer design patterns are very useful when designing any scalable application or any high-traffic application.
How do you think Amazon or Walmart can send notifications to millions of users when an item is available? The answer is using an observer design pattern.
At the end of this article, we’ll implement a simple use case of the YouTube notification system. Let’s deep dive into it.
Introduction
The observer design pattern is used when we have one-to-many relationships. One example is YouTube notifications or any broadcasting service.
In this design pattern, there are 2 types of entities: Producer, and Observer.
The producer is the entity, that sends notifications. The observer is the entity, that receives the notifications or updates from the Producer.
The below diagram will illustrate that properly:
Implementation of Observer Design Pattern
Below is the class diagram to explain the implementation of observer design pattern.
In the above example, the Producer is following a has-a relationship with the Observer because the Producer “has” the Observer (or a list of observers).
Now, let’s implement the above design pattern.
IPublisher.java
package ObserverDesignPattern;
import ObserverDesignPattern.IObserver;
public interface IPublisher {
// Subscribe Function
void subscribe(IObserver observer);
// Unsubscribe Function
void unsubscribe(IObserver observer);
void notifyInform();
}
Publisher.java
package ObserverDesignPattern;
import ObserverDesignPattern.IObserver;
import ObserverDesignPattern.IPublisher;
import java.util.ArrayList;
import java.util.List;
public class Publisher implements IPublisher {
String name;
List<IObserver> observerList;
Publisher(String name){
this.name = name;
this.observerList = new ArrayList<>();
}
// Subscribe Function
@Override
public void subscribe(IObserver observer){
observerList.add(observer);
}
// Unsubscribe Function
@Override
public void unsubscribe(IObserver observer) {
observerList.remove(observer);
}
@Override
public void notifyInform(){
for(IObserver observer : observerList){
observer.inform("Whatever");
}
}
}
IObserver.java
package ObserverDesignPattern;
public interface IObserver {
void inform(String message);
}
Observer.java
package ObserverDesignPattern;
import ObserverDesignPattern.IObserver;
public class Observer implements IObserver {
String name;
String email;
Observer(String name, String email){
this.name = name;
this.email = email;
}
@Override
public void inform(String message){
System.out.println( "name : " + this.name + " message : " + message);
}
}
Main.java
package ObserverDesignPattern;
import ObserverDesignPattern.IObserver;
import ObserverDesignPattern.IPublisher;
public class Main {
public static void main(String[] args) {
// Create a few publishers
IPublisher publisher1 = new Publisher("PUB1");
IPublisher publisher2 = new Publisher("PUB2");
// Create a few observers
IObserver observer1 = new Observer("OBS1", "EM1");
IObserver observer2 = new Observer("OBS2", "EM2");
// Subscribe observer to publsher
publisher1.subscribe(observer1);
publisher1.subscribe(observer2);
publisher2.subscribe(observer2);
publisher2.subscribe(observer1);
// Now, let's hit the notifyInform function
System.out.println("PUB1");
publisher1.notifyInform();
System.out.println("PUB2");
publisher2.notifyInform();
}
}
In the above code, we’ve demonstrated the Observer design pattern while keeping SOLID principles in mind.
I hope it helps in clearing the concept. If you’ve any questions or have any doubts, please feel free to comment.
Also, I’m open to suggestions for improvements. Please let me know in the comments if you have any.
Top comments (0)