CoursesGalaxy Courses Subscription Knowledge Hub
Knowledge Hub
Design Patterns
    Creational Patterns
    Structural Patterns
    Behavioral Patterns
        Chain of Responsibility
        Command
        Interpreter
        Iterator
        Mediator
        Memento
        Observer
        State
        Strategy
        Template Method
        Visitor


Observer Design Pattern


Observer design pattern is used to notify all the objects to update themselves when an object state gets changed. This is also known as Publish-Subscribe.


Intent
  • Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
The intent is to have the notification and update to all the objects when one changes its state.

Problem
  • Number of objects are dependent on another object and need to be notified for any changes to update themselves.
There may be dependency between different objects and they should be notified to update accordingly when one object changes its state. There may be requirement of adding and deleting new objects too and for different events of state changes.

Solution
  • Give the responsibility of monitoring the state change of object to separate object which should notify to all dependent objects.
Where it is applicable?
  • There is a one to many dependency between objects.
  • State change of one object requires changes in multiple objects.
  • Scenarios of Publish-Subscriber, Broadcasting.
Structure

Observer Design Pattern

Participant classes
  • Subject class provides interface Attach() and Detach() for attaching and detaching observer. The observer objects observe the subject.
  • ConcreteSubject class implements the method Attach() and Detach(). It keeps state information. It notifies to observers for change of state and also provides method for getting the state information.
  • Observer class provides Update() interface for updating the Observers.
  • ConcreteObserver class implements the method Update() for updating the state. It has instance of class ConcreteSubject which is used to get the state information of ConcreteSubject.
How they work together?
  • Observers are attached with ConcreteSubject. Whenever ConcreteSubject state gets changed, it uses Notify() method to notify all the observers. It calls method Update() of all observers while notification. ConcreteObserver gets the state information from ConcreteSubject using its instance and updates the applicable changes.
  • So you can see monitoring of state change of objects is given to Subject and it notifies to all observers for state change. Observers get the subject state information and update themselves. It is easy to add and delete the observer and also to make changes in subject as they do not affect each other. There are two ways to get the update information, one is the push model where subject sends detail information to observer; and another one is pull model where subject sends minimal information and observer gets the information from subject. Push model makes it less reusable and pull model is not much efficient. As the requirement of observer, state changes and need of multiple subject grows, there can be separate mechanism for update, and that responsibility can be given to separate object. The change of state in subject has to be handled properly, as it proceeds to update of lot of observer objects.
Example

Implementation Code

C++   Observer Pattern in C++   Example in C++
C#   Observer Pattern in C#   Example in C#
Java   Observer Pattern in Java   Example in Java
Python   Observer Pattern in Python   Example in Python
JavaScript   Observer Pattern in JavaScript   Example in JavaScript
PHP   Observer Pattern in PHP   Example in PHP
Ruby   Observer Pattern in Ruby   Example in Ruby
Swift   Observer Pattern in Swift   Example in Swift
Objective-C   Observer Pattern in Objective-C   Example in Objective-C



Suresh Kumar Srivastava is founder of online learning site coursegalaxy.com and author of popular books "C In Depth", "Data Structures Through C In Depth". He has 18+ years experience in industry and worked on architecture and design of multiple products. This article is from his courses on design patterns.







Yearly Subscription


Design Patterns in C++


Data Structures in C#

Explore

Courses

Subscription

Knowledge Hub

Design Patterns

Tech Articles

About Us

Overview

Contact Us

CourseGalaxy

CourseGalaxy is focused on transformation of Engineering graduate to Software Engineer to fill the gap between education and industry and provide the training on specific problems of industry to enhance the skills of Engineers in industry to do their job appropriately.

Learn From Us

2018 CourseGalaxy