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.


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>();



publicvoid registerObserver(theObserver o) {




publicvoid removeObserver(theObserver o) {




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);






Concrete Code for Observers

public class Subscriber implements theObserver{

private String name;

public Subscriber (String aName) { = aName;



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;



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



//The magazine company has sent out a new magazine


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

//magazine was sent out.

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



//Subscriber number 2


Subscriber_2 yetAnotherSubscriber = newSubscriber_2(“Billy Bob”);






The Result:

observer pattern_output







– 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.



– 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.