Table of Contents
What is the Observer Pattern?
The Observer Pattern is a design pattern that allows any number of objects to observe state of a single object and be informed of any changes. The single object is typically described as the subject where the objects interested in changes of the subject are known as observers. The observers could be notified that were was a change and query the subject or be updated with what the change was.
What are the benefits of the Observer Pattern?
There are a few benefits of the Observer Pattern. First, it provides a nice objected oriented design by keeping the control of the subject’s data within the subject object itself versus allowing many objects (observers) have control over the same data. Second, the pattern, if implemented correctly, should have interfaces to the subject and the observers. This promotes a loose coupling. The subject would depend on the observer’s interface not the respective implementations. Loose coupling is a standard design principle which has several benefits of its own. For example, you can add any number of new observers without having to change the subject implementation. And as long as the subject and observer fulfill there responsibilities of their respective interfaces, changes to one won’t affect the other.
Example of the Observer Pattern using C++
A blog and its subscribers is a good example of where the observer pattern can be used. Consider the content of a blog as being the subject. Typically, blogs allow readers to subscribe to be informed when there is new content on a blog. These readers would be considered your observers. I have written a C++ program where I have three different users (observers) that can subscribe to get blog (subject) updates to show the Observer Pattern in action.
The Observer Interface : BlogObserver
The BlogObserver is an interface that defines one method, BlogUpdate(), for derived objects to implement.
BlogObserver.h
The Subject Interface: BlogSubject
The BlogSubject is an interface that defines methods that allow a BlogObserver to Subscribe or Unsubscribe to receive BlogSubject’s updates. In addition, the Observers are able to request notification of current state via Notify().
BlogSubject.h
The Subject : MyBlog
I have written MyBlog to implement the BlogSubject interface. It contains a BlogData member object as the data subscribed observers would like to be notified about when it changes. The BlogData object implements a StringData interface so its observers depend on the interface versus the concrete class.
MyBlog.h
MyBlog.cpp
The Observers
I have written three different observers. Boss, CoWorker and Friend. Each implement the BlogObserver interface. Notice that each observer depends on the concrete subject object, MyBlog. This is so the observer can get the specific state data the subject contains when notified via BlogUpdate() that the state data has changed.
Boss.h
Boss.cpp
CoWorker.h
CoWorker.cpp
Friend.h
Friend.cpp
The Observer Pattern in Action
So lets take a look at the program that makes use of these objects. First, we want to create a MyBlog object. We use this object to construct our three observers, Boss, CoWorker and Friend. Now we can Subscribe or Unsubscribe our observers with the MyBlog object. You will see that when observers are Subscribed, they are informed when the BlogData changes in MyBlog via SetData().
MyBlogTest.cpp
Here is the output
Boss received "My first blog"
CoWorker received "My first blog"
CoWorker received "My second blog"
Friend received "My second blog"
Friend received "My third blog"
I hope you found this post to be helpful and informative. Please feel free to leave a comment with further discussion, feedback and questions.
Recommended Resources
Here are some resources I recommend for more information on the Observer Pattern
Head First Design Patterns: A Brain-Friendly Guide
Design Patterns: Elements of Reusable Object-Oriented Software