Every Software Engineer certainly must have to deal with change. Change is a constant in Software Design : Adding feature, Changing of requirement, or Bug fixing.
What is design pattern ? In simplest way I can say, it is general solutions for common problem in Software Development. The purpose of design pattern is to help structure your code so it will be flexible and resilient when its changed.
There are 23 Common Design Pattern that being used by programmer around the world, in this chapter I am going to describe about Observer Pattern.
Observer pattern is one to many dependency between object, so when one object changes state, all its dependent object will be notified. This pattern sometimes also called by Publisher-Subscriber model.
An Object which is being watched by other objects is called Subject/Publisher and Object which is watching state of the subject is called Observer/Subscriber.
Each of fans (Observer) would like to get newest updates of their celebrity(Subject), they do follow their celebrity's account. Then every updates/events related to the celebrity will be notified to the fans as long they keep following. Or they can stops following the celebrity and won't be notified anymore.
Now we would like solve this case, retailers(Observer) want to get latest price of product every time the supplier(Subject) change their price, then retailers start following the supplier to get the update.
public interface Subject {
public void addObserver(Observer ob);
public void removeObserver(Observer ob);
public void notifyChange(int productId, int productPrice);
}
public Supplier implements Subject {
private List<Retailer> retailerList;
public Supplier() {
this.retailerList = new ArrayList<>();
}
@Override
public void addObserver(Observer observer) {
this.retailerList.add(observer);
}
@Override
public void removeObserver(Observer observer) {
this.retailerList.remove(observer);
}
public void updateProduct(int productId, int productPrice) {
notifyChange(productId, productPrice);
}
@Override
public void notifyChange(int productId, int productPrice) {
for(int i = 0; i < retailerList.size() ; i++) {
Observer obs = retailerList.get(i);
obs.update(productId, productPrice);
}
}
}
public interface Observer {
public void update(int prouctId, int productPrice);
}
public class Main {
public static void main(String[] args) {
Supplier supplier = new Supplier();
RetailerA retailerA = new RetailerA();
RetailerB retailerB = new RetailerB();
supplier.addObserver(retailerA);
supplier.addObserver(retailerB);
supplier.updateProduct(1, 35000);
supplier.updateProduct(2, 98000);
}
public class RetailerA implements Observer {
@Override
public void update(int productId, int productPrice) {
System.out.println("Retailer A Got Updated for productId = " + productId + " with price = " + productPrice);
}
}
public class RetailerB implements Observer {
@Override
public void update(int productId, int productPrice) {
System.out.println("Retailer B Got Updated for productId = " + productId + " with price = " + productPrice);
}
}
}
Above code show that RetailA and RetailB will act as Observer and registered to the Supplier, then when the supplier update the product, it will notify RetailA and RetailB to print new product price. Congratulation you have been implemented Observer Design Pattern.
For other tutorial, kindly follow my medium https://medium.com/@kovanchandra and go to my website https://kovanchandra.com
Keywords: Design Pattern, Design Pattern Java, Observer Pattern, Design Pattern Observer, PubSub, Publisher Subscriber