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 a design pattern? In simplest way I can say, it is a general solution for common problems in Software Development. The purpose of design patterns is to help structure your code so it will be flexible and resilient when its changed.
There are 23 common design patterns that are being used by programmers around the world. In this chapter I am going to describe the Observer Pattern.
The Observer pattern is a one to many dependency between objects, so when one object changes state, all its dependent objects will be notified. This pattern is also known as Publisher-Subscriber.
An Object which is being watched by other objects is called Subject/Publisher and an Object which is watching state of the Subject is called Observer/Subscriber.
Each fan (an Observer
) would like to get newest updates of his/her celebrity (the Subject
), so they do follow their celebrity's account.
Then every update/event 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 interface Observer {
public void update(int productId, int productPrice);
}
public Supplier implements Subject {
private List<Retailer> retailerList;
public Supplier() {
retailerList = new ArrayList<>();
}
@Override
public void addObserver(Observer observer) {
retailerList.add(observer);
}
@Override
public void removeObserver(Observer observer) {
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 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);
}
}
}
The above code shows 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 the Observer Design Pattern.
See also https://examples.javacodegeeks.com/core-java/java-observer-design-pattern-example/