A word about the observer design pattern
Posted on 5/9/2007 10:16:00 AM
in #Design Pattern
Hi,/P>
A few days back on of my senior talked about different pattern and how useful they can be in the process of development. We were talking about the observer pattern. So I thought I will write a bit about the observer design pattern./P>
Based on the principal of implicit invocation, Observer design pattern (also known as publish Subscribe) is used in programming to observer the state of and object in a program. In many programming language the issue of this pattern are handled in native event handling syntax./P>
The bottom line of the pattern is that one or more listeners or observer (objects) register themselves to observer an event, which is raised by the subject. The object that raises the event maintains a collection of all the observer or listeners. We can also say that the pattern defines a one to many dependencies between objects so that one the state of a object is changed all the dependent are notified and updated automatically./P>
The main participants of this pattern are /P>
Subject /P>
- Provides and interface so that observer object can be attached or detached/LI>
- Also keeps a collection of the observers./LI>/UL>/UL>
ConcreteSubject /P>
o /SPAN>/SPAN>Stores the state of interest on concrete observers/P>
o /SPAN>Also sends notification to the observer when it state changes./P>
Observer /P>
o /SPAN>/SPAN>This class defines an updating interface for all observers, to receive update notification from the subject. This is mainly used as an abstract class for all concrete observers./P>
ConcreteObserver/P>
o /SPAN>/SPAN>Maintains a reference to a ConcreteSubject object/P>
o /SPAN>/SPAN>Stores the state, which is consistent with the subject./P>
When an event is raised all the observers receives a callback by the way of a virtual function or a function pointer. The function also passes some information of the event through parameters to be used by observer. Each concrete observer implements the notify function and as a consequence defines its own behavior when the notification occurs./P>
Thanks Vikram/P>
|