Design Patterns - Observer

design-patterns

https://www.sitepoint.com/understanding-the-observer-pattern/ - done reading

// Design Patterns - Behavioral - Observer:

The subject (the object that is being observed) must implement the attach, 
detach, notify, getState, and setState methods. The notify method can be 
private or protected. The getState and setState methods are normal gettter and 
setter methods, however, the setState method is modified so that whenever it is 
invoked, it will also invoke the notify method.

The observer object (the objects that is observing another object) is given the 
subject, and the observer invokes the attach method of the subject to register 
itself with the subject. The observer must implement the update method. The 
notify method of the subject will invoke the update method of each observer to 
notify the observer of the subject's state change.

The Observer Pattern (also known as Publish-Subscribe Pattern) is a behavioral 
design pattern which defines a one-to-many relationship between objects such 
that, when one object changes its state, all dependent objects are notified and 
updated automatically. Terminologies:

1. subject or publisher: this is the object that is being observed / subscribed

2. observers or subscribers: this is the objects that are listening for changes 
   in other object.

The Observer class provides an update() method which will be called by the 
subject to notify it of the subject’s state change. In this example, I’ve 
defined the update() method as a concrete method. If you like, you can define 
the method here as abstract and then provide a concrete implementation for it 
in your observer’s subclass.

// Create Subject class (the object that allows other objects to observe its 
// states)
// Subject.java
import java.util.ArrayList;
import java.util.List;

public class Subject {

   private List<Observer> observers = new ArrayList<Observer>();
   private int state;

   public int getState() {
      return state;
   }

   public void setState(int state) {
      this.state = state;
      notifyAllObservers();
   }

   public void attach(Observer observer){
      observers.add(observer);        
   }

   public void notifyAllObservers(){
      for (Observer observer : observers) {
         observer.update();
      }
   }     
}

// Step 2.  Create Observer class.
// Observer.java
public abstract class Observer {
   protected Subject subject;
   public abstract void update();
}

// Step 3.  Create concrete observer classes
// BinaryObserver.java
public class BinaryObserver extends Observer{

   public BinaryObserver(Subject subject){
      this.subject = subject;
      this.subject.attach(this);
   }

   @Override
   public void update() {
      System.out.println( "Binary String: " + 
        Integer.toBinaryString( subject.getState() ) ); 
   }
}

// OctalObserver.java
public class OctalObserver extends Observer{

   public OctalObserver(Subject subject){
      this.subject = subject;
      this.subject.attach(this);
   }

   @Override
   public void update() {
     System.out.println( "Octal String: " + 
        Integer.toOctalString( subject.getState() ) ); 
   }
}

// HexaObserver.java
public class HexaObserver extends Observer{

   public HexaObserver(Subject subject){
      this.subject = subject;
      this.subject.attach(this);
   }

   @Override
   public void update() {
      System.out.println( "Hex String: " + 
        Integer.toHexString( subject.getState() ).toUpperCase() ); 
   }
}

// Step 4.  Use Subject and concrete observer objects.
public class ObserverPatternDemo {
   public static void main(String[] args) {
      Subject subject = new Subject();

      new HexaObserver(subject);
      new OctalObserver(subject);
      new BinaryObserver(subject);

      System.out.println("First state change: 15");    
      subject.setState(15);
      System.out.println("Second state change: 10");    
      subject.setState(10);
   }
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License