An insight into observer pattern

One of the things which I often encounter these days is design patterns. If I’m to give a head first to design patterns, they articulate solutions to some of the commonly occurring design problems with related to software development. Hence, it promotes design reusability just as Object-Oriented Programming (OOP) encourages code reusability. The Observer, which is classified as a behavioral pattern can become quite handy in case of handling events (event-driven) specially, if your project adopts Model View Control (MVC) architecture.

At the heart of Observer pattern are Source and its Observers. The idea behind the pattern is to allow multiple observers (which can be views such as windows, web pages or any other simple UI element like a label) to be able to listen to its Model, known as the Source by registering to it. The relationship between the source and the observers is hence, known as publish-subscribe relationship.

An apt situation to employ Observer would be, modifying a certain field on a window while couple of more windows which also utilize the same field are kept opened. One would expect this modification to instantaneously take effect on all of the windows which utilize the particular field. This characteristic can easily be ensured with Observer. A simplified implementation of the pattern in java can be found below.

View1.java

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

public class View1 implements Observer
{
  public void update(Observable o, Object arg)
  {
    System.out.println("View 1: " + arg.toString());
  }
}

View2.java

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

public class View2 implements Observer
{
  public void update(Observable o, Object arg)
  {
    System.out.println("View 2: " + arg.toString());
  }
}

UserData.java

import java.util.Observable;

public class UserData extends MyObservable{
  private String name;
  private Long id;

  public UserData(){
    this.name = "";
    this.id = (long)0;
  }

  public String getName()
  {
    return name;
  }

  public void setName(String name)
  {
    this.name = name;
    setChanged();
    notifyObservers(name);
  }

  public Long getId()
  {
    return id;
  }

  public void setId(int id)
  {
    this.id = (long)id;
    setChanged();
    notifyObservers(id);
  }
}

TestMain.java

public class TestMain
{
  public static void main(String[] args)
  {
    UserData data = new UserData();
    View1 view1 = new View1();
    View2 view2 = new View2();
    data.addObserver(view1);
    data.addObserver(view2);

    data.setName("Lenard");
  }
}

Lumar – A new dimension to reality browsing.

After being in labs for couple of decades, Augmented Reality has become a highly popular phenomenon in recent times. Specially, Mobile Augmented Reality platforms promise to transform geographical information systems to give them a new look that will make such applications more meaningful and entertaining to use.

One of the finest examples of this would be Reality Browsing where geo-tagged data from various sources such as Wikipedia, Panoramio, Twitter, Flickr and etc are overlayed real-time over a video. Along with the inclusion of compass, GPS receiver, accelerometer and video camera which have become common features on most modern smart phones, reality browsing has already entered mainstream and continues to grow.

Explorer View

Explorer View


Ground View

Ground View

Lumar, a reality browser for android smart phones will become the world’s first speech powered browser of its kind. Now it is available for public downloads via Android Market. The first release candidate contains layers such as Wikipedia, Twitter and Buzz. It also features a unique stations layer where it can show all major bus and railway stations around you. The video below gives a glimpse of how Lumar operates. Enjoy!

Chronicles

It ‘s been a while since I last blogged. I wish everyone a happy new year 2010 as this happens to be the first post for this year. Lot has happened and importantly, a semester has gone past as I once again got busy doing a bundle of course works.

One of the first things to explore in the new year was gaming engines as I have been looking to create virtual 3D environments. And it became really serious after watching AVATAR, one of the first engines that impressed me was UNITY.

Being a desktop enhancement freak, yesterday after getting bored doing another coursework, thought of trying circle dock inspired by some of the concept videos. An open source version of circle dock from, Sourceforge was the one for me. It was really cool and below shared is a video of it.

However the immediate reason to write this post was QR Code. I knew there is a bar code scanner in ma phone but I have never used it or else I was never required to use it. However, today while I was going through few blogs I found it could be a great way to share urls and even contact details. You can read more on QR Code on, Nokia Mobilecodes. One of ma first attempt was to create a code carrying the blog url. It works tremendously well and I was able to visit the blog soon after scanning it on the phone which earlier I had to either type or to use a bookmark. Below I share the QR Code if you have a phone with bar code scanner you should be able to scan it with the camera.

Lonely Coder

Anticipating a release of fennac which is the Symbian version of Firefox. Browsing with Firefox on cellphone should be cool. Thank you. :)

« Older entries

Follow

Get every new post delivered to your Inbox.