import java.util.ArrayList; public class SubjectImpl implements Subject{//Subject is an interface for binding observers, unbinding observers and notifications ArrayList<Observer> observers = new ArrayList<Observer>(); //Used to save the observer Observer is an interface that defines the method to be implemented by the observer // bind the observer @Override public void registerObserver(Observer o) { observers.add(o); } //unbind the observer @Override public void removeObserver(Observer o) { observers.remove(o); } //Notify observer @Override public void notifyObserver() { for(Observer o :observers){ o.change(); } } }
The Observer design pattern achieves decoupling between two objects. Both subjects and observers only need to be programmed according to their own interface specification.