Observer Pattern Fall 2005 OOPD John Anthony. What is a Pattern? “Each pattern describes a problem which occurs over and over again in our environment,

Slides:



Advertisements
Similar presentations
Winter 2007ACS-3913 Ron McFadyen1 Also known as publish/subscribe The essence of this pattern is that one or more objects (called observers or listeners)
Advertisements

Lecture 12 Observer Pattern UML Class Diagrams (repetition) Use Case Diagrams Sequence Diagrams Statechart Diagrams.
 Recent researches show that predicative programming can be used to specify OO concepts including classes, objects, interfaces, methods, single and multiple.
The Observer Pattern SE-2811 Dr. Mark L. Hornick 1.
Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
Figures – Chapter 7.
Design Patterns Pepper. Find Patterns Gang of Four created 23 Siemens published another good set x
Chapter 7 – Object-Oriented Design
Design Patterns Design Patterns Composite Pattern Observer Pattern.
CSE3308/CSC Software Engineering: Analysis and DesignLecture 5B.1 Software Engineering: Analysis and Design - CSE3308 Patterns CSE3308/CSC3080/DMS/2000/12.
OOP Design Patterns Chapters Design Patterns The main idea behind design patterns is to extract the high level interactions between objects and.
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
Spring 2010ACS-3913 Ron McFadyen1 Weather Station Page 39+ In this application, weather station devices supply data to a weather data object. As the data.
Reza Gorgan Mohammadi AmirKabir University of Technology, Department of Computer Engineering & Information Technology Advanced design.
Observer Pattern Tu Nguyen. General Purpose When one object changes state, all the dependent objects are notified and updated. Allows for consistency.
CSE Software Engineering: Analysis and Design, 2002Lecture 7B.1 Software Engineering: Analysis and Design - CSE3308 Patterns CSE3308/DMS/2002/15.
BehavioralCmpE196G1 Behavioral Patterns Chain of Responsibility (requests through a chain of candidates) Command (encapsulates a request) Interpreter (grammar.
March Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem.
Copyright © Active Frameworks Inc. - All Rights Reserved.More On Behavioral Patterns - Page L9-1 PS95&96-MEF-L16-1 Dr. M.E. Fayad Creationa l.
Winter 2007ACS-3913 Ron McFadyen1 Observer Pattern Problem: There are many objects (observers / subscribers) needing to know of the state changes, or events,
ECE 355 Design Patterns Tutorial Part 2 (based on slides by Ali Razavi) Presented by Igor Ivković
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
OOMPA Lecture 9 Design Patterns Composite Pattern Observer Pattern.
Introduction to software design patterns For CSE 3902 By: Matt Boggus.
BY VEDASHREE GOVINDA GOWDA
Behavioral Patterns  Behavioral patterns are patterns whose purpose is to facilitate the work of algorithmic calculations and communication between classes.
Design Patterns.
02 - Behavioral Design Patterns – 2 Moshe Fresko Bar-Ilan University תשס"ח 2008.
Design Patterns Lecture III. What Is A Pattern? Current use comes from the work of the architect Christopher Alexander Alexander studied ways to improve.
Design Patterns Part two. Structural Patterns Concerned with how classes and objects are composed to form larger structures Concerned with how classes.
Observer Behavioral Pattern. Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified.
Programming in C# Observer Design Pattern
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.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VII Observer, Command, and Memento.
Where Do Surrogates Fit into This Proxy Pattern Observer Pattern Visitor Pattern By Kurt Rehwinkel.
Factory Method Explained. Intent  Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method.
CSC 480 Software Engineering Design With Patterns.
Proxy, Observer, Symbolic Links Rebecca Chernoff.
Behavioural Design Patterns Quote du jour: ECE450S – Software Engineering II I have not failed. I've just found 10,000 ways that won't work. - Thomas Edison.
Design Pattern. Definition: A design pattern is a general reusable solution to a commonly occurring problem within a given context in software design.
OBSERVER DESIGN PATTERN. Behavioral Patterns  Behavioral patterns are those patterns that are most specifically concerned with communication between.
Manali Joshi1 The Observer Design Pattern Presented By: Manali Joshi.
Behavioral Patterns1 Nour El Kadri SEG 3202 Software Design and Architecture Notes based on U of T Design Patterns class.
BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAMMING PRACTICES Model View.
1 CSE 331 Model/View Separation and Observer Pattern slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia.
The Mediator Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
The Observer Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Duke CPS Programming Heuristics l Identify the aspects of your application that vary and separate them from what stays the same ä Take what varies.
OBSERVER PATTERN OBSERVER PATTERN Presented By Presented By Ajeet Tripathi ISE
The Observer Design Pattern Author :Erich Gamma, et al. Source :Elements of Reusable Object-Oriented Software Speaker : Chiao-Ping Chang Advisor : Ku-Yaw.
February 23, 2009Observer Pattern, OOA&D, Rubal Gupta, CSPP, Winter ‘09 Observer Pattern Defines a “one-to-many” dependency between objects so that when.
Design Patterns: MORE Examples
Chapter 5 – Design and Implementation
Chapter 7 – Object-Oriented Design
Observer Design Pattern
Observer Design Pattern
Presented by Igor Ivković
Design Patterns - A few examples
Observer Pattern 1.
Observer Design Pattern
Design pattern Lecture 9.
Introduction to Design Patterns
Advanced ProgramMING Practices
8. Observer Pattern SE2811 Software Component Design
Week 6, Class 2: Observer Pattern
Advanced ProgramMING Practices
Presented by Igor Ivković
Software Design Lecture : 40.
Presentation transcript:

Observer Pattern Fall 2005 OOPD John Anthony

What is a Pattern? “Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solutions to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice.” Christopher Alexander. “Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solutions to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice.” Christopher Alexander.

The Pattern of a Pattern Pattern Name provides high levels of abstraction Pattern Name provides high levels of abstraction Problem Definition when to apply the pattern Problem Definition when to apply the pattern Solution the elements that make up the pattern and their relationships. Solution the elements that make up the pattern and their relationships. Consequences the results and tradeoffs of the pattern. Consequences the results and tradeoffs of the pattern.

Observer Pattern Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. Example: Graphical editor with multiple views of the same graphic objects. If a graphical object is manipulated in one view, it notifies the other views to display the changes made to it. Example: Graphical editor with multiple views of the same graphic objects. If a graphical object is manipulated in one view, it notifies the other views to display the changes made to it.

Key Behaviors A common side-effect of partitioning a system into a collection of cooperating classes is the need to maintain consistency between related objects. A common side-effect of partitioning a system into a collection of cooperating classes is the need to maintain consistency between related objects. The key objects in the Observer pattern are Subject and Observer. The key objects in the Observer pattern are Subject and Observer. A subject may have any number of dependent observers. A subject may have any number of dependent observers. All observers are notified whenever the subject undergoes a change in state. All observers are notified whenever the subject undergoes a change in state. In response, each observer will query the subject to synchronize its state with the subject´s state. In response, each observer will query the subject to synchronize its state with the subject´s state.

When to Use the Observer Pattern Use the Observer pattern in any of the following situations Use the Observer pattern in any of the following situations When an abstraction has two aspects, one dependent on the other. Encapsulating these aspects in separate objects lets you vary and reuse them independtly. When an abstraction has two aspects, one dependent on the other. Encapsulating these aspects in separate objects lets you vary and reuse them independtly. When a change to one object requires changing others, and you do not know how many objects need to be changed. When a change to one object requires changing others, and you do not know how many objects need to be changed. When an object should be able to notify other objects without making assumptions about who these objects are. When an object should be able to notify other objects without making assumptions about who these objects are.

Class Diagram

Observer Participants Subject Subject Knows its observers. Any numberof Observer objects may observe a subject. Knows its observers. Any numberof Observer objects may observe a subject. Provides an interface for attaching and detaching Observer Objects. Provides an interface for attaching and detaching Observer Objects. Observer Observer Defines an updating interface for objects that should be notified of changes in a subject Defines an updating interface for objects that should be notified of changes in a subject

Observer Participants ConcreteSubject ConcreteSubject Stores a state of interest to ConcreteObserver objects. Stores a state of interest to ConcreteObserver objects. Sends a notification to its observers when its state changes. Sends a notification to its observers when its state changes. ConcreteObserver ConcreteObserver Maintains a reference to a ConcreteSubject object Maintains a reference to a ConcreteSubject object Stores state that should stay consistent with the subject state. Stores state that should stay consistent with the subject state. Implements the Observer updating interface to keep its state consistent with the subject state. Implements the Observer updating interface to keep its state consistent with the subject state.

Observer Collaborations ConcreteSubject notifies its observers whenever a change occurs that could make its observer’s state inconsistent with its own. ConcreteSubject notifies its observers whenever a change occurs that could make its observer’s state inconsistent with its own. After being informed of a change in the ConcreteSubject, a ConcreteObserver object may query the subject for information. ConcreteObserver uses this information to reconcile its state with that of the object. After being informed of a change in the ConcreteSubject, a ConcreteObserver object may query the subject for information. ConcreteObserver uses this information to reconcile its state with that of the object.

Sequence Diagram

Sequence Diagram Notes Note that the Observer object that initiates the change request with SetState() postpones its update until it gets a notification from the subject. Note that the Observer object that initiates the change request with SetState() postpones its update until it gets a notification from the subject. In this scenario Notify() is called by the subject, but it can be called by an observer or by another kind of object (see implementation issues) In this scenario Notify() is called by the subject, but it can be called by an observer or by another kind of object (see implementation issues)

Pattern Consequences The Observer pattern lets you vary subjects and observers independently. You can reuse subjects without reusing observers, and vice versa. It lets you add observers without modifying the subject or other observers. The Observer pattern lets you vary subjects and observers independently. You can reuse subjects without reusing observers, and vice versa. It lets you add observers without modifying the subject or other observers.

Consequences (Con’t) Abstract coupling between Subject and Object Abstract coupling between Subject and Object All a subject knows is that it has a list of observers, each conforming to the simple interface of the abstract Observer class. The subject does not know the concrete class of any observer. All a subject knows is that it has a list of observers, each conforming to the simple interface of the abstract Observer class. The subject does not know the concrete class of any observer. Support for broadcast communication Support for broadcast communication Unlike and ordinary request, the notification that a subject sends need not specify its receiver. The notification is broadcast automatically to all interested objects that subscribed to it. Unlike and ordinary request, the notification that a subject sends need not specify its receiver. The notification is broadcast automatically to all interested objects that subscribed to it.

Unexpected Updates Unexpected Updates Because observers have no knowledge of each other’s presence, they can be blind to the ultimate cost of changing the subject. A seemingly innocuous operation to the subject may cause a cascade of updates to observers and their dependent objects. Because observers have no knowledge of each other’s presence, they can be blind to the ultimate cost of changing the subject. A seemingly innocuous operation to the subject may cause a cascade of updates to observers and their dependent objects. This problem is aggravated by the fact that the simple update protocol provides no details on what changed in the subject. Without additional protocol observers have to query the entire state of the subject to deduce the changes. This problem is aggravated by the fact that the simple update protocol provides no details on what changed in the subject. Without additional protocol observers have to query the entire state of the subject to deduce the changes. Consequences (Con’t)

Observer Implementation Mapping subjects to their obervers Mapping subjects to their obervers The simplest way for a subject to keep track of the obervers it should notify is to store references to them explicitly in the subject. An alternative is to use an associative look-up (multimap) to maintain the subject-observer mapping. The simplest way for a subject to keep track of the obervers it should notify is to store references to them explicitly in the subject. An alternative is to use an associative look-up (multimap) to maintain the subject-observer mapping.

Observer Implementation Observing more than one subject Observing more than one subject It might make sense in some situations for an observer to depend on more than one subject. It is necessary to extend the Update interface in such cases to let the observer know which subject is sending the notification. The subject can simply pass itself as a parameter in the Update operation, thereby letting th observer know which subject to examine. It might make sense in some situations for an observer to depend on more than one subject. It is necessary to extend the Update interface in such cases to let the observer know which subject is sending the notification. The subject can simply pass itself as a parameter in the Update operation, thereby letting th observer know which subject to examine.

Who triggers the update Who triggers the update The subject and its observers rely on the notification mechanism to stay consistent. But what object actually calls Notify() to trigger the update? There are two options. The subject and its observers rely on the notification mechanism to stay consistent. But what object actually calls Notify() to trigger the update? There are two options. Have state-setting operations on the Subject call Notify after they change the subject’s state Have state-setting operations on the Subject call Notify after they change the subject’s state Make clients responsible for calling Notify at the right time. Make clients responsible for calling Notify at the right time. Observer Implementation

Subject calls Notify Subject calls Notify The advantage of this approach is that clients do not have to remember to call Notify on the subject. The disadvantage is that several consecuitve operations will cause several consecutive updates, which may be inefficient The advantage of this approach is that clients do not have to remember to call Notify on the subject. The disadvantage is that several consecuitve operations will cause several consecutive updates, which may be inefficient Clients calls Notify Clients calls Notify The advantage here is that the client can wait to trigger the update until after a series of state changes has been made. The disadvantage is that clients have an added responsibility to trigger the update, causing possible errors. The advantage here is that the client can wait to trigger the update until after a series of state changes has been made. The disadvantage is that clients have an added responsibility to trigger the update, causing possible errors. Observer Implementation

Avoiding observer-specific update protocols : the push and pull model Avoiding observer-specific update protocols : the push and pull model Implementations of the Observer pattern often have the subject broadcast additional information about the change. The subject passes this information as an argument to Update. Implementations of the Observer pattern often have the subject broadcast additional information about the change. The subject passes this information as an argument to Update. Push model: the subject sends obervers detailed information about the change Push model: the subject sends obervers detailed information about the change Pull model: the subject sends only a minimal notification and observers ask for details explicitly Pull model: the subject sends only a minimal notification and observers ask for details explicitly

Pull model: Pull model: The pull model emphasizes the subject’s ignorance of its obervers. The pull model may be inefficient, because Observer classes must infer what changed without help from Subject. The pull model emphasizes the subject’s ignorance of its obervers. The pull model may be inefficient, because Observer classes must infer what changed without help from Subject. Push model: Push model: The push model assumes that the subject knows something about its obervers’ needs. The push model assumes that the subject knows something about its obervers’ needs. The push model might make observers less reusable because Subject classes make assumptions about Observer classes that might not always be true. The push model might make observers less reusable because Subject classes make assumptions about Observer classes that might not always be true. Observer Implementation