Presentation is loading. Please wait.

Presentation is loading. Please wait.

Behavioral Pattern: Observer C h a p t e r 5 – P a g e 186 A large monolithic design does not scale well as additional graphical and monitoring requirements.

Similar presentations


Presentation on theme: "Behavioral Pattern: Observer C h a p t e r 5 – P a g e 186 A large monolithic design does not scale well as additional graphical and monitoring requirements."— Presentation transcript:

1

2 Behavioral Pattern: Observer C h a p t e r 5 – P a g e 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.

3 The Observer Pattern C h a p t e r 5 – P a g e 187 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 Observer defines an updating interface for objects that should be notified of changes in a subject. 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.

4 C h a p t e r 5 – P a g e 188 Non-Software Example: Auction 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.

5 C h a p t e r 5 – P a g e 189 Software Example: Sensor System 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.

6 C h a p t e r 5 – P a g e 190 Sensor System Observer C++ Code #include using namespace std; // The Abstract Observer AlarmListener class AlarmListener { public: virtual void alarm() = 0; }; // The Subject SensorSystem class SensorSystem { public: void attach(AlarmListener *al) { listeners.push_back(al); } void soundTheAlarm() { for (int i = 0; i < listeners.size(); i++) listeners[i]->alarm(); } private: vector listeners; }; // The ConcreteObserver Lighting class Lighting: public AlarmListener { public: void alarm() { cout << " Lighting: Lights up." << endl; } };

7 C h a p t e r 5 – P a g e 191 // The ConcreteObserver Gates class Gates: public AlarmListener { public: void alarm() { cout << " Gates: Gates close." << endl; } }; class CheckList { public: void byTheNumbers() { 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: public CheckList, public AlarmListener { public: void alarm() { cout << " Surveillance (by the numbers):" << endl; byTheNumbers(); } private: void isolate() { cout << " Surveillance: Train the cameras." << endl; } };

8 C h a p t e r 5 – P a g e 192 void main() { SensorSystem ss; 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; }

9 Observer Pattern Advantages C h a p t e r 5 – P a g e 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.


Download ppt "Behavioral Pattern: Observer C h a p t e r 5 – P a g e 186 A large monolithic design does not scale well as additional graphical and monitoring requirements."

Similar presentations


Ads by Google