Design Pattern 1

Design Pattern

We can divide design patterns into three board categories.

1. Creational Design Pattern: These are the ones that creates objects for you rather than instantiating them directly. This gives your program more flexibility in deciding which objects needs to be created for a given case.
Ex:-

  • Factory
  • AbstartFactory
  • Singelton
  • Prototype

2. Structural Design Pattern : These concern with class and object composition. They use inheritance to compose interfaces and define ways to compose object to obtain new functionality.
Ex:-

  • Adapter
  • Bridge
  • Composite
  • Decorator
  • Facade
  • Flyweight
  • Proxy

3. Behavioural Design Pattern: Most of these patterns are specifically concerned with communication between objects.
Ex:-

  • Chain Of Responsibility
  • Command
  • Interpreter
  • Momento
  • Observer

Let’s start with the Observer Design Pattern:
==============================================
Observer design pattern menas we are concerned with the state of an object. If it get changed we should get notified. This object is called as Subject and the ones which are interested in the subject are called Observers.

So it is one to many dependency between objects, so that when one object changes state all its dependents are notified and updated automatically.

In JDK we can achieve this by Observer interface and Observable class in java.util package
Observable is a class and the subject will extends this and Observer is an interface and all observers will implement this.

An observable object can have one or more observers. An observer may be any object that implements interface Observer. After an observable instance changes, an application calling the Observable’s notifyObservers method causes all of its observers to be notified of the change by a call to their update method.

Let’s start this with an example:

First make a subject i.e. one object whose state is of interest.


import java.util.Observable;

public class WeatherData extends Observable{

private float temprature;
 private float humidity;
 private float pressure;

public void measurementChanged(){
 setChanged();
 notifyObservers();
 }

public void setMeasurements(float temprature, float humidity, float pressure){
 this.temprature = temprature;
 this.humidity = humidity;
 this.pressure = pressure;
 measurementChanged();
 }

/**
 * @return the temprature
 */
 public float getTemprature() {
 return temprature;
 }

/**
 * @return the humidity
 */
 public float getHumidity() {
 return humidity;
 }

/**
 * @return the pressure
 */
 public float getPressure() {
 return pressure;
 }
 }

From above we want that whenever the measurement changes out the observers should get notified and can display the new values.
The methods setChanged(); and notifyObservers(); are of supreclass i.e. of Observable class. whenever we wnat that the our observer should get notified we have to class first setChanged() method and then notifyObservers()

setChanged() method signifies that Obsevavle object has been changed.

notifyObservers() means that If this object has changed, as indicated by the setChanged method, then notify all of its observers and then call the observer update method called with two arguments: this observable object and with optional argument.

update method will get more clear once we look into the obsever class code.

Lets have a look into that:


import java.util.Observable;
 import java.util.Observer;

import com.observable.main.WeatherData;

public class ObserverTest implements Observer{

private float temprature;
 private float humidity;
 private float pressure;

public ObserverTest(Observable observable){
 observable.addObserver(this);
 }

@Override
 public void update(Observable o, Object arg) {
 if(o instanceof WeatherData){
 WeatherData weatherData = (WeatherData)o;
 this.temprature = weatherData.getTemprature();
 this.humidity = weatherData.getHumidity();
 this.pressure = weatherData.getPressure();
 display();
 }

}

public void display(){
 System.out.println(” Temprature:: “+ this.temprature +” Humidity:: ” +
 this.humidity +” Pressure:: ” + this.pressure);
 }

}

In our observer class first we should register this currrent object into the Observable that’s why you can see in the constructor the below code:
observable.addObserver(this);

Now you can see we have update method which is being called by the Observable class and when a call goes to this we are calling the display method.

Let’s now look into the main class. i.e. its time to run above


import com.observable.main.WeatherData;
 import com.observer.main.ObserverTest;

public class TestObserverPattern {

public static void main(String…strings){
 WeatherData weatherData = new WeatherData();
 ObserverTest observerTest = new ObserverTest(weatherData);

weatherData.setMeasurements(2, 43, 76);
 }
 }

The above one is self explaining.

Hope above example somewhat clears the Observer DP.

 

One comment on “Design Pattern

  1. Reply SEO Aug 10,2013 7:34 pm

    I like and thank you for blog post. Fantastic.

Leave a Reply