1 / 8

Behavioral Pattern: Observer

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

oma
Download Presentation

Behavioral Pattern: Observer

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. Sensor System Observer C++ Code Chapter 5 – Page 190 #include <iostream> #include <vector> using namespace std; // The Abstract Observer AlarmListener classAlarmListener { public: virtual void alarm() = 0; }; // The Subject SensorSystem classSensorSystem { public: void attach(AlarmListener *al) { listeners.push_back(al); } voidsoundTheAlarm() { for (inti = 0; i < listeners.size(); i++) listeners[i]->alarm(); } private: vector < AlarmListener * > listeners; }; // The ConcreteObserver Lighting class Lighting: publicAlarmListener { public: void alarm() { cout << " Lighting: Lights up." << endl; } };

  6. Chapter 5 – Page 191 // The ConcreteObserver Gates class Gates: publicAlarmListener { public: void alarm() { cout << " Gates: Gates close." << endl; } }; classCheckList { public: voidbyTheNumbers() { localize(); isolate(); identify(); } private: 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 { public: void alarm() { cout << " Surveillance (by the numbers):" << endl; byTheNumbers(); } private: voidisolate() { cout << " Surveillance: Train the cameras." << endl; } };

  7. void main() { SensorSystemss; cout << "Attaching the gates to the sensor system." << endl << endl; ss.attach(&Gates()); cout << "Attaching the lighting to the sensor system." << endl << endl; ss.attach(&Lighting()); cout << "Attaching the surveillance to the sensor system." << endl << endl; ss.attach(&Surveillance()); cout << "Sounding the alarm." << endl << endl; ss.soundTheAlarm(); cout << endl; } Chapter 5 – Page 192

  8. 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.

More Related