8. Observer Pattern SE2811 Software Component Design

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

 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.
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.
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,
Observer Pattern Tu Nguyen. General Purpose When one object changes state, all the dependent objects are notified and updated. Allows for consistency.
Winter 2007ACS-3913 Ron McFadyen1 Observer Pattern Problem: There are many objects (observers / subscribers) needing to know of the state changes, or events,
Chapter 13 Starting Design: Logical Architecture and UML Package Diagrams.
Design Patterns.
02 - Behavioral Design Patterns – 2 Moshe Fresko Bar-Ilan University תשס"ח 2008.
Observer Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al.
CSC 313 – Advanced Programming Topics. Observer Pattern Intent  Efficiently perform 1-to-many communication  Easy to respond dynamically when event(s)
Design Patterns Lecture III. What Is A Pattern? Current use comes from the work of the architect Christopher Alexander Alexander studied ways to improve.
Detailed design – class design Domain Modeling SE-2030 Dr. Rob Hasker 1 Based on slides written by Dr. Mark L. Hornick Used with permission.
CSE 331 Software Design & Implementation Dan Grossman Winter 2014 Events, Listeners, and Callbacks (Based on slides by Mike Ernst, David Notkin, Hal Perkins)
Design Pattern. The Observer Pattern The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all.
CS 210 Introduction to Design Patterns September 7 th, 2006.
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.
3-1 State Design Pattern C Sc 335 Rick Mercer. State Design Pattern State is one of the Behavioral patterns It is similar to Strategy Allows an object.
CSC 313 – Advanced Programming Topics. Observer Pattern in Java  Java ♥ Observer Pattern & uses everywhere  Find pattern in JButton & ActionListener.
Observer Please Snarf the Code for Today’s Class..
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.
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.
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.
CSE 331 Software Design & Implementation Hal Perkins Winter 2013 Events, Listeners, and Callbacks (slides by Mike Ernst and David Notkin) 1.
The Observer Pattern.
The Observer Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Slide design: Dr. Mark L. Hornick
Observer Pattern Keeping An Eye on Things Need to introduce observer pattern formally first, include book definition & design principle Keeping An Eye.
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.
Software Architecture and Design BITS C461/IS C341 Software Engineering First Semester Aditya P. Mathur Department of Computer Science Purdue.
Observer Pattern Context:
Facade Pattern Jim Fawcett CSE776 – Design Patterns Summer 2010
Slide design: Dr. Mark L. Hornick
Week 2, Day 1: The Factory Method Pattern
Observer Design Pattern
Observer Design Pattern
Note 11 Command Pattern SE2811 Software Component Design
Facade Pattern Jim Fawcett CSE776 – Design Patterns Summer 2010
Patterns of Interaction 2: Publish-Subscribe
Patterns of Interaction 2: Publish-Subscribe
Design Patterns - A few examples
Introduction to Behavioral Patterns (1)
Model-View-Controller (MVC) Pattern
Advanced Program Design with C++
Week 7, Class 1: The Command Pattern (cont.)
Observer Pattern 1.
Starting Design: Logical Architecture and UML Package Diagrams
Observer Design Pattern
SE2811 Software Component Design Dr. Rob Hasker
Design pattern Lecture 9.
Advanced ProgramMING Practices
14. Factory Pattern SE2811 Software Component Design
SE2811 Software Component Design Dr. Rob Hasker
10. Façade Pattern SE2811 Software Component Design
14. Factory Pattern SE2811 Software Component Design
Week 6, Class 2: Observer Pattern
12. Command Pattern SE2811 Software Component Design
Advanced ProgramMING Practices
Model, View, Controller design pattern
16. Visitors SE2811 Software Component Design
16. Visitors SE2811 Software Component Design
Software Design Lecture : 40.
Presentation transcript:

8. Observer Pattern SE2811 Software Component Design Dr. Rob Hasker (based on slides by Dr. Mark Hornick) 8. Observer Pattern

Problem: Information to multiple clients Consider what happens when move cursor in MSWord Enable, disable bold/italic/superscript/underline/etc. Update font, size Update centering/left justification/right justification How would this be implemented? How to get a message to all students? What if only some students want messages from me? Is there a better way? Discuss and present! Publish/subscribe model

Observer Pattern Context Core idea: One-to-many dependency between objects One object changes state All dependents are notified and updated automatically

Observer pattern: key components Subject Subject has dependent observers. Observer(s) When the state of the subject changes, each dependent observer is notified.

What are we trying to achieve with the Observer Pattern ? Separation of software subsystems Separation between GUI & Domain objects Loosely-coupled classes Because tightly-coupled classes reduce reusability & understanding A generic/elegant way for the classes to communicate

Generic Subject class class SubjectClass implements Subject { public SubjectClass(); public void attach(Observer obs); public void detach(Observer obs); public void notifyObservers(); private ArrayList <Observer> observers; } Subject::attach(Observer* pObserver) { m_hObservers.push_back(pObserver); } Subject::detach(Observer* pObserver) m_hObservers.remove(pObserver); Subject::notify() Vector<Observer*>::iterator m_ppObserver; for (m_ppObserver = m_hObservers.begin();m_ppObserver = m_hObservers.end(); ++m_ppObserver) (*m_ppObserver)->update(); Note: Some texts define a notify() instead of notifyObservers() method. However, Java’s Object class already has a notify() method, which we don’t want to override.

Generic Observer class ObserverClass implements Observer { public ObserverClass(); public void update(???); } Key question: What is the appropriate argument for the update() method?

Basic class relationships

Collaborations between objects in the Observer pattern s:SubjectClass o1:ObserverClass1 o2:ObserverClass2 attach() attach() notifyObservers() update(???) getContextSpecificInfo() update(???) getContextSpecificInfo()

Weather Program example class WeatherData implements Subject { //private data attributes List<Observer> observers; ... public WeatherData(){…} public void getTemp() {…} public int getWindSpeed() {…} public void attach(Observer obs) {…} public void detach(Observer obs) {…} public void notifyObservers() {…} ... } See sample code (available on website) original_weather: must update both revised_weather: uses observer note Observer, Subject classes weather program, original: no observer; must update both panels; revised: implements observer pattern

Example (contd.) public void acquireDataFromSensors() { // acquire updated weather data …… notifyObservers(); // notify observers }

Example (contd.) class mainDisplay extends Observer { public mainDisplay (WeatherData wd) {...} public void update(???) {...} public void updateDisplayUI() {...} }

Example (contd.) public mainDisplay(WeatherData wd) { Subject wdSubject = wd; wdSubject.attach(this); } // What do we pass to update()? public void update(???) // How do we get data from the Subject? updateDisplayUI(???); // mainDisplay class method

Implementation Questions What should be the arguments of the update method? Should we send the Subject as the argument? Alternatives: pull vs. push – which was used in the weather example? What would be the danger of pushing simple data to observers? Should each instance of the Observer store the “concrete subject” as a data attribute, or just an Interface reference? Can Subject be an abstract class instead of an Interface?

Consequences (positive) Coupling between Subject and Observers: Subject knows it has a list of Observers, but not specific classes Each Observer conforms to the simple interface of the abstract Observer Interface. Hence, coupling is Abstract Minimal Cohesion: observers highly cohesive – no code to monitor multiple places; subjects don’t have code checking on observers, so also cohesive

Consequences (negative) Broadcast communication Notification is broadcast to all interested objects. Observers can be added/removed at any time. Observer decides when it needs to be notified. Unexpected updates Observers have no knowledge Of each other’s presence. About the cost of “state change of subject” Cascade of updates.

Observer pattern in Java See: Observer, Observable Note discussion of notifyObservers Must call setChanged for notifications to be sent setChanged, clearChanged: can be used to suppress updates Issues Observer is a class – can’t subclass something else Observable.setChanged is protected – must subclass to be useful Doesn’t scale: if have large numbers of observables, type system does little to help What to do?

Review So what’s this pattern about? When to use it? Where can we use interfaces, abstract classes in this pattern? Where must we use them? Why should we consider rolling our own in Java Draw a UML diagram What methods might be private/protected?