7
Observer Design Pattern observing changes

Observer Design Pattern observing changes. Observer Motivation objects (observers) need to synchronize behavior with some other object (subject): observe

Embed Size (px)

Citation preview

Page 1: Observer Design Pattern observing changes. Observer Motivation objects (observers) need to synchronize behavior with some other object (subject): observe

Observer Design Pattern

observing changes

Page 2: Observer Design Pattern observing changes. Observer Motivation objects (observers) need to synchronize behavior with some other object (subject): observe

Observer Motivation• objects (observers) need to synchronize behavior with some other object

(subject): observe changing subject’s state

– ex: bar chart and bar graph need to know when original spreadsheet values change

2

Page 3: Observer Design Pattern observing changes. Observer Motivation objects (observers) need to synchronize behavior with some other object (subject): observe

Observer Implementation • observers subscribe to change state notifications, while subjects publish

changes by notifying observers or sending them messages (synonymous terms)

• another name for pattern – publish-subscribe

• common way to implement GUI interfaces (foundation of so-called model-view-controller architectural pattern)

– the “business logic” is in subject hierarchy

– the “presentation logic” is in observer hierarchy

• if using abstract/concrete classes

– abstract observer/subject – implement registration/notification functionality

– concrete observer/subject – implement subject state and state acquisition by observer

• there may be a registry of subjects for observers to subscribe to

• behavioral pattern

3

Page 4: Observer Design Pattern observing changes. Observer Motivation objects (observers) need to synchronize behavior with some other object (subject): observe

Communication Methods: Push & Pull

• observer needs to communicate state change information to subject

• two communication methods

– push – state change is in message itself

• may require large message

• not all concrete subjects need all the data

subject may push a reference to itself

– pull – observer queries the state of subject after receiving notification

• observer needs to keep reference to subject

• subject needs to implement getters

4

Page 5: Observer Design Pattern observing changes. Observer Motivation objects (observers) need to synchronize behavior with some other object (subject): observe

Observer UML Diagram(Push Method)

5

+notify()

Page 6: Observer Design Pattern observing changes. Observer Motivation objects (observers) need to synchronize behavior with some other object (subject): observe

Observer UML Diagram(Pull Method)

6

Page 7: Observer Design Pattern observing changes. Observer Motivation objects (observers) need to synchronize behavior with some other object (subject): observe

(Forward) Class Declaration• in C++, any named construct must be declared before use

• a class may be declared by

– class definition

– (forward) class declaration

• syntax

class NameOfClass; • after forward declaration, can declare pointers/references (but not objects) of

this class

• used when classes mutually refer to each other, or for better program structure

Class A; // declaration

Class B{

B(A*p):p_(p){}

private:

A *p_;

};

7

// definitionClass A{...};