The Observer design pattern is used when there is a one-to-many relationship between objects, and changes to one object need to be propagated to multiple dependent objects. It defines a dependency between objects so that when one object changes its state, all its dependents are notified and updated automatically.
A real world example of an Observer Design pattern is Stock Market Monitoring System. Let’s consider a scenario where investors want to receive a notification whenever the price of a particular stock changes.
In this example, the StockMarket
class represents a stock whose price can change. The Investor
class represents investors interested in receiving updates about the stock price. However, without the Observer pattern, investors need to manually check for updates by calling the getPrice
method of the StockMarket
class.
5 real world example of Observer Design Pattern
Stock market monitoring systems
Weather forecasting applications
Social media notification systems
Job application status tracking systems
Sports score tracking applications
Without Observer Design Pattern
public class StockMarket {
private String stockName;
private double price;
public StockMarket(String stockName, double price) {
this.stockName = stockName;
this.price = price;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
System.out.println(stockName + " price updated to: "+ price);
}
}
public class Investor {
private String name;
public Investor(String name) {
this.name = name;
}
public void update(double price){
System.out.println(name+ " received update: Price changed to "+ price);
}
}
public class Main {
public static void main(String[] args) {
StockMarket stock = new StockMarket("ABC", 100.00);
Investor investor1 = new Investor("Alice");
Investor investor2 = new Investor("Bob");
double currentPrice = stock.getPrice();
investor1.update(currentPrice);
investor2.update(currentPrice);
stock.setPrice(110.00);
}
}
With Observer Design Pattern
public interface Observer {
void update(String stockName, double price);
}
public class Investor implements Observer{
private String name;
public Investor(String name) {
this.name = name;
}
@Override
public void update(String stockName, double price) {
System.out.println(name +" received Update: "+ stockName +" price change to: "+ price);
}
}
public class StockMarket {
private String stockName;
private double price;
private List<Observer> observers = new ArrayList<>();
public StockMarket(String stockName, double price) {
this.stockName = stockName;
this.price = price;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
notifyObservers();
}
public void attach(Observer observer) {
observers.add(observer);
}
public void detach(Observer observer) {
observers.remove(observer);
}
public void notifyObservers(){
for(Observer observer: observers){
observer.update(stockName, price);
}
}
}
public class Main {
public static void main(String[] args) {
StockMarket stock = new StockMarket("ABC", 100.00);
Investor investor1 = new Investor("Alice");
Investor investor2 = new Investor("Bob");
stock.attach(investor1);
stock.attach(investor2);
stock.setPrice(110.00);
}
}