Observer design pattern


Type : Behavioral Design Pattern

Summary

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.

Advantage

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.

Disadvantage

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.

Details

Following are different participants in Observer Pattern

Subject(Interface)
 Keeps track of its observers
 Provides an interface for attaching and detaching Observer objects

Observer(Interface)
 Defines an interface for update notification

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

ConcreteObserver
  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();

  @Override
  public void notifyObserver() {
    for (Observer observer : observersList) {
      observer.update(this);
    }
  }

  @Override
  public void registerObserver(Observer observer) {
    observersList.add(observer);
  }

}

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

  @Override
  public void update(Subject subject) {

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

  }

}



Comments

Popular posts from this blog

Difference between volatile and synchronized

Converting Java Map to String

Masking Credit Card number in Java