Saturday, November 29, 2014

Observer design pattern

Type : Behavioral Design Pattern


In Observer design pattern, an object called subject maintains one to many relationship with Observers, so that once the state of the Subject changes it can automatically inform Observers registered with the subject.


An Object(Subject) can notify other Objects(Observer) without being tightly coupled with Observer and depend on Observer Class.

Different instances of the same Subject can register its own list of Observers.


Due to the indirect communication between Subject and Observer's it can lead to performance issues.

Important Points

Multiple Subscribers for a Subject.
Subjects know its Subscribers.
Generally the Subject triggers the method that updates Observers.
New Observers can be added to the System with minimal changes.


Following are different participants in Observer Pattern

 Keeps track of its observers
 Provides an interface for attaching and detaching Observer objects

 Defines an interface for update notification

  The object being observed
  Sends a notification to its observers when its state changes

  The observing object
  Stores state that should stay consistent with the subject's
  Implements the Observer update interface to process any changes in Subject

Simple Example

Let's think about a simple use case for Ordering Food in Restaurant. We have a subject RestaurantOder. And a list of Observers like (DoMenuAvailabilityObserver, DoFoodPrepareObserver, DoBillPaymentObserver etc.)

Class Diagram

Source Code

 * Interface Subject
public interface Subject {

  public void registerObserver(Observer observer);

  public void notifyObserver();


 * Interface Observer
public interface Observer {
  public void update(Subject sub);


// concrete Subject
public class RestaurantOrder implements Subject {

  public String order;

  public RestaurantOrder(String order) {
    this.order = order;

  List observersList = new ArrayList();

  public void notifyObserver() {
    for (Observer observer : observersList) {

  public void registerObserver(Observer observer) {


// concrete Observer which initiate payment processing
public class DoBillPaymentObserver implements Observer {

  public void update(Subject subject) {

    System.out.println("Process payment for " + ((RestaurantOrder) subject).order);



No comments :

Post a Comment