This week, I will be preparing a tutorial on the Observer Design Pattern and I figured I could assist my own understanding of the pattern by writing a blog post to accompany it. This post will be a summary of the Observer pattern as it is described on this page of the Object Oriented Design website. The motivation behind the Observer pattern is the frequent need of objects, in object oriented programming, to be informed about the changes that occur in another object. An exemplification of this concept in the real-world can be imagined as a stock system which provides data for several types of client; the subject(stocks server) needs to be separated from its observers(client applications) in such a way that adding a new observer would be transparent to the servers.
The intention of implementing the Observer pattern is to define a one-to-many dependant relationship between objects so that when an object’s state is altered, its dependants are notified and updated automatically. The Observer pattern is effectively adhered to with the implementation of 4 classes:
- Observable/Subject(GOF) – interface or abstract class; defines operations for attaching/de-attaching observers to client
- ConcreteObservable – concrete Observable class; maintains state of an object and notifies Observers when a change occurs
- Observer – interface or abstract class; defines notification operations
- ConcreteObserverA, ConcreteObserver2, etc. – concrete Observer implementations
How it Works:
- ConcreteObserverable object is instantiated in main framework
- ConcreteObserver objects are instantiated and attached to ConcreteObservable object via methods defined in the Observable interface
- ConcreteObserver objects are notified each time the state of the ConcreteObservable object is changed
- ConcreteObservers are added to the application by simply instantiating them in the main framework and attaching them to the ConcreteObservable object
This allows for classes, that are already created, to remain unchanged.
The Observer pattern is applicable in situations where the change of a state in one object must be reflected in another object without keeping the objects coupled, and in situations where the main framework needs to introduce new observers with minimal changes.
The Observer pattern is often used, in conjunction, with other design patterns:
- Factory – A Factory pattern can be utilized to create observers so no changes will be required in main framework
- Template – An Observer pattern can be utilized to make sure that Subject state is self-consistent
- Mediator – A Mediator pattern can be utilized in cases of many subjects and many observers
The Observer pattern is so well-known because its implementation has proven to be quite powerful in event handling systems of languages such as Java and C# and in the model-view-controller architectural pattern. I am excited to put together a tutorial on how to use this pattern and I believe that having a good grasp on its motivations and intentions will aid me in future if I ever consider using it.
From the blog CS@Worcester – by Ryan Marcelonis and used with permission of the author. All other rights reserved by the author.