Thursday, September 11, 2014

Observer pattern

To describe the Observer pattern, lets take an example of Football game.
The specific design problem is,

When the position of a ball changes, all the players should be notified straight away. 


Define a one-to-many dependency between objects so that one object changes state, all its dependents are notified and updated automatically.




Subject (Interface) exposes us three methods Attach, detach and notify. 
ConcreteSubject is football. Which exposes the current position to observers.
Observer (interface) exposes the update method.
ConcreteObserver is player. Which implements update method and with in this method it calls for the position of the ball and set the observerstate to the same.

ref: applying design pattern