Observer Pattern Recipe

1.) First you need to decide what objects in your program are going to be observers and which will be subjects.  Think of the Observer Pattern like a newspaper service.  The newspaper company has subscribers and delivers the paper to the subscribers.  Think about what info your observers might need from another object.

2.) To use the Observer Pattern in java there are two ways of going about it: make your own, or use java’s built in classes java.util.Observable and java.util.Observer.  There are more drawbacks to using java’s built in methods then rolling your own observer pattern.  Java’s issues include: having to subclass their Observable class, there is no Observable interface, and Observable protects crucial methods such as setChanged().

3.) Once you’ve decide which method of usage of the Observer Pattern best fits your needs all you gotta do is put it into your code.  In my game I used java’s built in methods so that is what I will be describing in the next few steps.

4.) Extend Observable in the class you want to act as your subject.  Don’t forget to include java.util.Observable, else you won’t get far.  In your method that changes your information that you want sent out to all your observers you must make two method calls that are extended from the Observer class.  First you must call setChanged() which is just basically a flag that says its ok to call the next method which you need to call directly after setChanged() and that is notifyObservers().

5.)  When calling the notifyObservers() method it is important at this point that you have decided that either your subject will send data directly to the observers (ie push data) or that your subject will simply notify the observers when it is time for them to come and get the data (ie pull data).  If you are using the push method you must include your data object that you want to update your observers with.  If you are using the pull method you will want to include other functions that will return the data you want to update your observers with.

6.)  After you have put in your setChanged() and notifyObservers() methods its time to make your observers observe =) .  In your class that you want to make an observer you must implement Observer.  Also for this class you will want to import both java.util.Observable and java.util.Observer.

7.) Once you have implemented Observer you then need to make a parameter of type Observable in the constructor for that class. This will allow you to pass in an instance of your subject as an argument.  That way your observer knows specifcally which instance of your subject to observe. Also in this class you will want to create a local observable variable to copy your subject into for later use. Don’t create it in your constuctor just put it in the class somewhere.  You want to  copy your subject into your local observable variable in the constructor.  Then to make your observer official call this line of code in your constructor: observable_var_name.addObserver(this).  That will add your class as an observer.

8.)  The last thing you need to do in your class you are making an observer is put in this method:

public void update(Observable obs, Object arg) {

//call methods from your subject to pull data

//or process and store data that is being pushed in from your data object


9.) Once you have completed all these steps you should have successfully implemented the observer pattern!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: