In continuation with the series on design pattern, I am currently going
through Behavioral Patterns. Today I will write about the Observer
Pattern. You can read about rest of the patterns from the following
The Observer pattern deals with providing a relation between objects
such that when one objects changes its state, then all the other objects
that have a relation to it are notified of the change. This could be
achieved by using a single publisher of the state of object and many
subscribers who may want the notification for the changed state.
The Observer is usually composed of 2 class viz. a Subject whose state
may change periodically and the Observer itself who maybe notified of
the change, if they wish to receive the state change information.
The following example is based on the example from Dofactory website
We can use the observer pattern when changes in one object needs to be
propagated to other objects or when the object sending the changes does
not need to know about the receivers.
In my next post I will be writing about the Visitor Pattern