I will attempt to explain what is one of the most widely used Design Patterns: Observer Pattern.
Problem domain: Implementing the Publisher/Subscriber scenario. We need a mechanism which allows us to notify subscriber objects whenever the publisher object changes state.
There is one publisher or subject class and one or more subscriber or observer classes.
In order for observers to receive a notification, they must implement some kind of update method which the subject can call whenever it changes state. The observers must have the freedom to register and un-register from subscription to the subject at any point in time.
The subject contains the stuff. So, whenever the stuff changes, observers need to be notified.
Here, the Observer Pattern comes to the rescue.
We need to create two Interfaces:
Observer. The interface for the subject contains the
notify method signatures, as follows:
public interface Subject
public void register(Observer o);
public void unregister(Observer o);
public void notify();
The interface for the observer contains an
update method signature:
public interface Observer
public void update(int value);
Now, let us assume that we are building a sample stock market gadget app. Here, the subject is the one which has the 'stuff', i.e., the stock market quotes. The observers are different classes which display the latest stock quotes to the user.
Let us have classes representing the subject/observer relationship: the
StockMarket class being the subject, and we have three different observer classes:
StockMarket class must implement the
public class StockMarket : Subject
ArrayList observers = new ArrayList();
int newValue = 0;
public void setValue(int v)
newValue = v;
public void register(Observer o)
public void unregister(Observer o)
int i = observers.indexOf(o);
public void notify()
for (int i=0;i < observers.size();i++)
Observer ob = (Observer)observers.get(i);
MSNStockGadget, must implement the
Note that the observer classes must maintain a reference variable of type
public class GoogleStockGadet : Observer
int latestValue = 0;
public GoogleStockGadet(Subject subject)
stockMarket = subject;
public void update(int value)
latestValue = value;
public void display()
System.out.println("Latest Quote=" + latestValue);
public void unsubscribe()
MSNStockGadget implement the observer interface.
Now, let's see how we make it all work in the main program.
public class MainProgram
public static void Main(string args)
StockMarket stockMarket = new StockMarket();
int latestQuote = 0;
GoogleStockGadget googleGadget = new GoogleStockGadget(stockMarket);
MSNStockGadget msnGadget = new MSNStockGadget(stockMarket);
YahooStockGadget yahooGadget = new YahooStockGadget(stockMarket);
With that, I end my article. Remember, the Observer Design Pattern solves the common problem of notifying several objects (i.e., Observers) about changes in the state of one principle object (or the Subject).
This, being my first article, might not be perfect, but hopefully, it has helped you in better understanding the concepts of the Observer Design Pattern.