The Observer Design Patterns

What is Observer Design Patterns

The Observer design pattern is a pattern that has a one-to-many dependency between a subject object and all of its observers object. The subject object will send an update to its listeners if something has changed. In other words, the observers will listen to the subject for any new information. A well-used example of this pattern is a button in a user interface. A button will wait and listen for the user’s command.

 

Some other examples of observers and observed are:

– Price of a stock (subject)

– Weather stations that gather data (observers)

– Magazine subscription (subject)

 

Out of these examples, I will create a simple magazine subscription watch to show how the concept works.

What are the functions of each part

The subject is in charge of:

– Keep track of the observers

– Registering new observers

– Removing any observer that is not needed

– Notifying observers.

The observer will listen to the subject for any update and execute any code that the programmer sets up.

Getting into the Code

Here is the UML for my example.

observer_patterns

The interfaces

By looking at the UML, you can see that I am using two interfaces. One is for the subject and another is for the observer. They are self-explanatory.

 

Concrete Code for Subject

import java.util.ArrayList;

public class Magazine implements theSubject {

//This will keep track of the observers

private ArrayList<theObserver> listOfObservers;

public Magazine() {

listOfObservers = new ArrayList<theObserver>();

}

@Override

publicvoid registerObserver(theObserver o) {

this.listOfObservers.add(o);

}

@Override

publicvoid removeObserver(theObserver o) {

listOfObservers.remove(o);

}

@Override

publicvoid notifyObserver() {

//This will go through the array of observers and call the observers

for(int i = 0; i < listOfObservers.size(); i++) {

theObserver oberver = (theObserver) listOfObservers.get(i);

oberver.update();

}

}

}

 

Concrete Code for Observers

public class Subscriber implements theObserver{

private String name;

public Subscriber (String aName) {

this.name = aName;

}

@Override

publicvoid update() {

System.out.println(name + ” got a magazine to read!!”);

}

}

Another Subscriber

public class Subscriber_2 implements theObserver{

 

String name;

public Subscriber_2 (String aName) {

name = aName;

}

@Override

publicvoid update() {

System.out.println(“I am a subscriber from another town. My name is “+name);

}

}

 

The Main Code

publicclass theMainCode {

 

public static void main(String[] args) {

//Setup the initial subscribers

Subscriber aSubscriber = new Subscriber(“Josh”);

Subscriber anotherSubscriber = new Subscriber(“Marry”);

//Initialize the Subject object

Magazine aMagazine = new Magazine();

//Tell the magazine about the subscribers

aMagazine.registerObserver(aSubscriber);

aMagazine.registerObserver(anotherSubscriber);

//The magazine company has sent out a new magazine

aMagazine.notifyObserver();

//A subscriber has discontinued his subscription right before the next

//magazine was sent out.

System.out.println(“\nJosh is leaving the magazine.”);

aMagazine.removeObserver(aSubscriber);

aMagazine.notifyObserver();

//Subscriber number 2

System.out.println(“”);

Subscriber_2 yetAnotherSubscriber = newSubscriber_2(“Billy Bob”);

aMagazine.registerObserver(yetAnotherSubscriber);

aMagazine.notifyObserver();

}

}

 

The Result:

observer pattern_output

 

 

 

 

 

  Advantages:

– Loss Coupling

– Support for broadcast communication

– This pattern will allow you to send data to many observers in an efficient manner with out changing too much code.

 

Disadvantages:

– If you get a lot of observers going, then you can add to the complexity of the code

– And keeping track of the order in which observers get notification can be tricky.

 

As a note: Java does have a built-in class Observable but it forces the use of inheritance, not interfaces. That does not go well with code re-usability and its usefulness.