Wednesday, 17 July 2013

Observer design Pattern with Examples .#Java

Observer design pattern  is a fundamental core pattern where Observe watch for any change in state or property of Subject. 

For Example Company updates all its shareHolder for any decision .

they make here Company is Subject and Shareholders are Observers, 
any change in policy of company , the company will notifies all its Shareholders (observers).



What is Observer design Pattern?


Observer design pattern is very important pattern and as name suggest it’s used to observe things. Suppose you want to notify for change in a particular object than you observer that object and changes are notified to you. Object which is being observed is refereed as Subject and classes which observe subject are called Observer

This pattern is used heavily along with Model View Controller Design pattern where change in model is propagated to view so that it can render it with modified information.



It Defines a one-to-many Dependency between objects , So that when one object changes state, all its dependents are notified and updated automatically 


How Observer Design Pattern is implemented?


The participants classes in this pattern are:
  • Subject (Observable) - interface or abstract class defining the operations for attaching and de-attaching observers to the client. In the GOF book this class/interface is known as Subject.
  • ConcreteSbject(Concrete Observable) :Subject implementation that it's data need to be handled. and notified to the observer 
  • Observer - interface or abstract class:  defining the operations to be used to notify this object.
  • ConcreteObserverA, ConcreteObserver2 - concrete Observer implementations.

methods for implementing this pattern.


Public Interface Observer:

Any class who implements this interface must be notified when subject or observable object change its status.

Update (Observable Ob, Object arg): This method is called when subject is changed.


Class Observable:
It’s a subject to whom observer wants to observe.

Some Important Method:
  • addObserver(Observer o):add Observers in the set of observers for this subject or observalbel object.
  • deleteObserver(Observer o): delete Observers in the set of observers .
  • hasChanged():check if object has changed.
  • clearChanged():this method will indicate that subject has no changes or all the observers has been notified when changes is made.
  • notifyObservers(): notify all the observers if object has changed .



Example using #java:

import java.util.ArrayList;

interface Observer {
       public void update(String state);
}

interface Subject {
       public void registerObserver(Observer observer);

       public void removeObserver(Observer observer);

       public void notifyObservers();
}

class Concreate implements Subject {
       private ArrayList<Observer> observers = new ArrayList<Observer>();
       private String type;
       private String[] states;
     
       public Loan(String type, string state) {
              this.type = type;
              this.states.add(state);
       }

       public String[] getStates() {
              return states;
       }

       public void addStates(String state) {
              this.States.add(state);
              notifyObservers();
       }

       public String getBank() {
              return this.bank;
       }

       public String getType() {
              return this.type;
       }

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

       }

       @Override
       public void removeObserver(Observer observer) {
              observers.remove(observer);

       }

       @Override
       public void notifyObservers() {
              for (Observer ob : observers) {
                     System.out
                                  .println("Notifying Observers on change in States");
                     ob.update(this.states);
              }

       }

}

class Newspaper implements Observer {
       @Override
       public void update(String States) {
              System.out.println("Newspaper: no of states has changed "
                           + states.count());
       }
}

class Internet implements Observer {
       @Override
       public void update(float interest) {
              System.out.println("Internet: Interest Rate updated, new Rate is: "
                           + interest);
       }
}


No comments:

Post a Comment