Observer Design Pattern
An era of reactive is going on. To keep this in mind observer design pattern concept is important to understand.
It can be simply stated as the 1-M i.e one subject to many observers. When the state of one object ( subject ) changes, all of its dependent objects get notified or updated automatically. Once observers gets notified from the subject then can get updated data.
Lets take a real life example of stock market where investor can buy or sell shares. its a simple concept if anyone sell some amount of share in stock marker then buyers transaction can happen. It means suppose 5 investor want to buy irctc share but no one is selling share at this moment. if any investor share X quantity of share then all buyers get automatically response to buy i.e Buyers get notify automatically where sellers sells transation happens.
In terms of Java there are 3 methods in Subject i.e. registerObserver, removeObserver and notify. All observer needs to implement this method.
package com.pattern.observer;
public interface Observer {
public void update(String availability);
}
package com.pattern.observer;
public interface StockMarketSubject {
public void subscribeObserver(Observer ob);
public void unsubscribeObserver(Observer ob);
public void notifyObserver();
}
package com.pattern.observer;
import java.util.ArrayList;
public class SellerInvestor implements StockMarketSubject {
private String shareName;
private int sharePrice;
private int quantity;
private String inStock;
private ArrayList<com.pattern.observer.Observer> obsList = new ArrayList<com.pattern.observer.Observer>();
public SellerInvestor(String shareName, int sharePrice, int quantity, String inStock) {
super();
this.shareName = shareName;
this.sharePrice = sharePrice;
this.quantity = quantity;
this.inStock = inStock;
}
public String getShareName() {
return shareName;
}
public void setShareName(String shareName) {
this.shareName = shareName;
}
public int getSharePrice() {
return sharePrice;
}
public void setSharePrice(int sharePrice) {
this.sharePrice = sharePrice;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
notifyObserver();
}
public String getInStock() {
return inStock;
}
public void setInStock(String inStock) {
this.inStock = inStock;
notifyObserver();
}
@Override
public void subscribeObserver(Observer ob) {
obsList.add(ob);
}
@Override
public void unsubscribeObserver(Observer ob) {
obsList.remove(ob);
}
@Override
public void notifyObserver() {
System.out.println(“Stock Name : “ + this.shareName + “Share price : “ + this.sharePrice + “ Shre Quantity : “
+ this.quantity + “, is now avaiable : “ + this.inStock + “ so notify all observer. \n”);
for (com.pattern.observer.Observer o : obsList) {
o.update(this.inStock);
}
}
}
package com.pattern.observer;
public class EndUser implements Observer {
String name;
public EndUser(String name, SellerInvestor sellerInvestor) {
this.name = name;
sellerInvestor.subscribeObserver(this);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void update(String availability) {
System.out.println(“Hello “ + name + “ ! we are glad to notify that your share is not “ + availability);
}
}
package com.pattern.observer;
public class ObserverDesignPattern {
public static void main(String[] args) {
SellerInvestor s = new SellerInvestor(“irctc”, 1090, 23, “Sold Out”);
EndUser e1 = new EndUser(“Mandal”, s);
EndUser e2 = new EndUser(“Nikhil”, s);
EndUser e3 = new EndUser(“Darshan”, s);
System.out.println(s.getInStock());
s.setInStock(“Avaiable for buy”);
}
}
You can run this simple example to understand observer pattern.