Saturday, September 19, 2009

Observer Pattern in Java

We should use observer pattern when one object (say Subject) changes state and we would like to notify/update all its dependents (say Observers). Observer pattern achieve this objective without making classes tightly coupled (NOTE: Tightly coupled classes reduces the reusability of the classes.)

Lets look at the example : We would like to present application data in many formats i.e table, graph, chart etc. So as and when application data changes, all the presentation should be notified.


(image source : IBM reasearch)

Java provides Observable class and Observer interface to implement Observer pattern easily.

Subject class should extends Observable class. Whenever state changed, subject call setChanged() method and notifyObservers() method. These method do all the work for you. You do not need to iterate over all the observers and call method of observers.

import java.util.Observable;

public class Subject extends Observable {
  public void stateChanged(String i) {
    setChanged();
    notifyObservers(i);
  }
}

Lets look at the observer's code now. Observer need to implements update method.

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

public class Observer1 implements Observer {
  public void update(Observable arg0, Object arg1) {
    if (arg1 instanceof String) {
      System.out.println("Observer1  " + (String)arg1);
    }
  }
}

We need to add observers to the subject. Lets look at the MainClass which does this. Also assume we created one more observer class name Observer2.

public class MainClass {
  public static void main(String[] args) {
    Subject source = new Subject();
    Observer1 one = new Observer1();
    Observer2 two = new Observer2();
    source.addObserver(one);
    source.addObserver(two);
    source.stateChanged("Hello");
    source.stateChanged("world");
  }
}

As you noticed, we added observer to subject class. When we call stateChanged method of Subject class it notifies all the observers class. Output of the class looks like this.

Observer2  Hello
Observer1  Hello
Observer2  world
Observer1  world

Enjoy Design Patterns !!!

Friday, September 4, 2009

Add Google Translation Widgets to Your Site


Google's widget uses a machine translation system developed by Google, that's available for 29 language pairs. The widget works in most browsers and it's actually a Google gadget loaded in an iframe. Its gives ability to your reader to read your site in their preferred language.

Google translation widgets looks like this.

It is very easy to add this gadget to your website. Add following script to your website:

<script src="http://www.gmodules.com/ig/ifr?url=http://www.google.com/ig/modules/translatemypage.xml&up_source_language=en&w=160&h=60&title=&border=&output=js"></script>

Lets look at the example once some one clicks on any language (i.e. Hindi). Original site looks like this http://ayurvedictipofaday.blogspot.com/ . After clicking on Hindi language the site looks like this.



Enjoy Google Translation ...