Observer Pattern

Observer Pattern with Patterns, design, Creational Design, Abstract Factory Pattern, singleton design patterns, Adapter, Visitor Pattern, Template Pattern, Command Pattern, State Pattern, java etc.

Observer Pattern

Observer Pattern

An Observer Pattern says that "just define a one-to-one dependency so that when one object changes state, all its dependents are notified and updated automatically".

The Memento pattern is also known as Dependents or Publish-Subscribe.


Benefits:

  • It describes the coupling between the objects and the observer.
  • It provides the support for broadcast-type communication.

Usage:

  • When the change of a state in one object must be reflected in another object without keeping the objects tight coupled.
  • When the framework we writes and needs to be enhanced in future with new observers with minimal chamges.

UML for Observer Pattern:


Implementation of Observer Pattern

Step 1:

Create a ResponseHandler1 class the will implement the java.util.Observer interface.

  1. //This is a class.  
  2.   
  3. import java.util.Observable;  
  4. import java.util.Observer;  
  5.   
  6. public class ResponseHandler1 implements Observer {  
  7.     private String resp;  
  8.     public void update(Observable obj, Object arg) {  
  9.         if (arg instanceof String) {  
  10.             resp = (String) arg;  
  11.             System.out.println("\nReceived Response: " + resp );  
  12.         }  
  13.     }  
  14. }// End of the ResponseHandler1 interface.  

Step 2:

Create a ResponseHandler2 class the will implement the java.util.Observer interface.

  1. //This is a class.  
  2.   
  3. import java.util.Observable;  
  4. import java.util.Observer;  
  5.   
  6. public class ResponseHandler2 implements Observer {  
  7.     private String resp;  
  8.     public void update(Observable obj, Object arg) {  
  9.         if (arg instanceof String) {  
  10.             resp = (String) arg;  
  11.             System.out.println("\nReceived Response: " + resp );  
  12.         }  
  13.     }  
  14. }// End of the ResponseHandler2 interface.  

Step 3:

Create an EventSource class that will extend the java.util.Observable class .

  1. //This is a class.  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.IOException;  
  5. import java.io.InputStreamReader;  
  6. import java.util.Observable;  
  7.   
  8. public class EventSource extends Observable implements Runnable {  
  9.     @Override  
  10.     public void run() {  
  11.         try {  
  12.             final InputStreamReader isr = new InputStreamReader(System.in);  
  13.             final BufferedReader br = new BufferedReader(isr);  
  14.             while (true) {  
  15.                 String response = br.readLine();  
  16.                 setChanged();  
  17.                 notifyObservers(response);  
  18.             }  
  19.         }  
  20.         catch (IOException e) {  
  21.             e.printStackTrace();  
  22.         }  
  23.     }  
  24. }// End of the Eventsource class.