Behavioral Pattern: Observer
1 / 8

Behavioral Pattern: Observer - PowerPoint PPT Presentation

  • Uploaded on

Behavioral Pattern: Observer. Chapter 5 – Page 186. A large monolithic design does not scale well as additional graphical and monitoring requirements are added.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about ' Behavioral Pattern: Observer' - oma

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

Behavioral Pattern: Observer

Chapter 5 – Page 186

A large monolithic design does not scale well as additional graphical and monitoring requirements are added.

With the Observer pattern, a one-to-many dependency is established between objects so that when one object (the Subject) changes state, all of the dependent objects (the Observers) are notified and updated automatically.

Observers register with the Subject as they are created. Whenever the Subject changes, it broadcasts to all registered Observers that it has changed, and each Observer queries the Subject for that subset of the Subject's state that it is responsible for monitoring.

The Observer Pattern

Chapter 5 – Page 187

  • The Observer defines an updating interface for objects that should be notified of changes in a subject.

  • The Subject knows its observers and provides an interface for attaching and detaching Observer objects. Any number of Observer objects may observe a Subject.

  • The ConcreteSubject stores the state of interest to ConcreteObserver and sends a notification to its Observers when its state changes.

  • The ConcreteObserver maintains a reference to a ConcreteSubject object and stores a state that should stay consistent with the subject's, implementing the Observer updating interface to keep its state consistent with the subject's.

Non-Software Example: Auction

Chapter 5 – Page 188

In an auction/bidders scenario, the bidders may be viewed as Observers, with the auctioneer (and the high bid) as the object being viewed.

The bidders are observers, receiving notification from the auctioneer whenever a new high bid is placed.

The acceptance of a bid by the auctioneer results in a broadcast of the new high bid to the bidders.

Software Example: Sensor System

Chapter 5 – Page 189

In this sensor system scenario, the alarm listener is the AbstractObserver, with lighting, gates, and surveillance as ConcreteObservers.

When the sensor system Subject changes state, all alarm listeners are notified via alarms.

Sensor System Observer C++ Code

Chapter 5 – Page 190

#include <iostream>

#include <vector>

using namespace std;

// The Abstract Observer AlarmListener




virtual void alarm() = 0;


// The Subject SensorSystem




void attach(AlarmListener *al) { listeners.push_back(al); }



for (inti = 0; i < listeners.size(); i++)




vector < AlarmListener * > listeners;


// The ConcreteObserver Lighting

class Lighting: publicAlarmListener



void alarm() { cout << " Lighting: Lights up." << endl; }


Chapter 5 – Page 191

// The ConcreteObserver Gates

class Gates: publicAlarmListener



void alarm() { cout << " Gates: Gates close." << endl; }












virtual void localize() { cout << " CheckList: Establish a perimeter." << endl; }

virtual void isolate() { cout << " CheckList: Isolate the grid." << endl; }

virtual void identify() { cout << " CheckList: Identify the source." << endl; }


// The ConcreteObserver Surveillance

class Surveillance: publicCheckList, publicAlarmListener



void alarm()


cout << " Surveillance (by the numbers):" << endl;




voidisolate() { cout << " Surveillance: Train the cameras." << endl; }


void main()



cout << "Attaching the gates to the sensor system." << endl << endl;


cout << "Attaching the lighting to the sensor system." << endl << endl;


cout << "Attaching the surveillance to the sensor system." << endl << endl;


cout << "Sounding the alarm." << endl << endl;


cout << endl;


Chapter 5 – Page 192

Observer Pattern Advantages

Chapter 5 – Page 193

  • The Observer Pattern avoids direct object interactions and can be used when one or more objects are interested in the state changes of a given object.

  • Subject and the Observer objects can be reused separately and they can vary independently.

  • The Observer Pattern can be used to develop applications in layers. For example, a user interface application can contain spreadsheet, graph, and chart objects (Observers) that are dependent on the data from a database object (Subject). When the data in the database object changes, all of the Observers are automatically notified through the abstract operation defined in the Observer base class. Since the abstract operation is defined in the base class, the Subject need not know the concrete Observer classes and hence they are loosely coupled.