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.

Slides:



Advertisements
Similar presentations
Winter 2007ACS-3913 Ron McFadyen1 Duck Example Consider the text example (up to page 6). Each type of duck is a subclass of Duck Most subclasses implement.
Advertisements

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)
1 Dept. of Computer Science & Engineering, York University, Toronto CSE3311 – Software Development Observer Pattern.
Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
Chapter 2: The Observer Pattern. Consider the Following Application Application specification Humidity Temperature Pressure Weather Station Weather Data.
Figures – Chapter 7.
Design Patterns Pepper. Find Patterns Gang of Four created 23 Siemens published another good set x
Chapter 7 – Object-Oriented Design
Jan 29, Ron McFadyen1 UML Class Diagram Examples Based on well-known patterns Exhibit ways of providing dynamic structures and behaviour.
March Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem.
March Ron McFadyen1 Observer P Also known as Publish-Subscribe Applied in order to implement the Model-View Separation principle (see.
Fall 2009ACS-3913 Ron McFadyen1 Observer Problem: There are many objects (subscribers) needing to know of the state changes, or events, of another object.
Reza Gorgan Mohammadi AmirKabir University of Technology, Department of Computer Engineering & Information Technology Advanced design.
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.
Nov Ron McFadyen1 Figure The Observer Pattern in a DCD.
Nov Ron McFadyen1 Observer P Problem: There are many objects (subscribers) needing to know of the state changes, or events, of another.
Winter 2012ACS-3913 Ron McFadyen1 Model View Controller Originated with Smalltalk development (1979) Separates a system’s classes into View, Controller,
March Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem.
Winter 2007ACS-3913 Ron McFadyen1 Classes Represented by a rectangle with possibly 3 compartments Customer Name Address Customer Name Address getName()
Spring 2010ACS-3913 Ron McFadyen1 Command The command pattern encapsulates a request or unit of work into an object. An invoker will ask a concrete command.
Oct Ron McFadyen1 Collaborations Collaboration : an arrangement of classes, links, roles in a context to implement some behaviour. Useful for.
Nov 5, Ron McFadyen1 Observer Pattern Objects that participate in the observer pattern must either be publishers (subject) or subscribers (observers)
Winter 2007ACS-3913 Ron McFadyen1 Observer Pattern Problem: There are many objects (observers / subscribers) needing to know of the state changes, or events,
1 Observer Design Pattern By Eric Perret Pages in Applying UML and Patterns.
Spring 2010ACS-3913 Ron McFadyen1 Duck Example Consider the text example (up to page 6). Each type of duck is a subclass of Duck Most subclasses implement.
March Ron McFadyen1 Observer Problem: There are many objects (subscribers) needing to know of the state changes, or events, of another object.
DESIGN PATTENS - OBSERVER PATTERN
Behavioral Patterns  Behavioral patterns are patterns whose purpose is to facilitate the work of algorithmic calculations and communication between classes.
CS 537 Group 3 Report: Activity Diagrams and Observer Design Pattern Anna Deghdzunyan Xuan Yang Keenan Knaur John Hurley.
Basic OOP in C#.
02 - Behavioral Design Patterns – 2 Moshe Fresko Bar-Ilan University תשס"ח 2008.
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
GoF Design Patterns (Ch. 26). GoF Design Patterns Adapter Factory Singleton Strategy Composite Façade Observer (Publish-Subscribe)
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.
CS 350 – Software Design The Observer Pattern – Chapter 18 Let’s expand the case study to include new features: Sending a welcome letter to new customers.
Oct R McFadyen1 Facade P Problem: There are a set of classes, a subsystem, that you need to interact with for some purpose, but you don’t.
Observer Please Snarf the Code for Today’s Class..
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.
Observer Design Pattern
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.
CS 415 N-Tier Application Development By Umair Ashraf June 22,2013 National University of Computer and Emerging Sciences Lecture # 3 Design Patterns (Observer,Factory,Singleton)
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 Observer Pattern.
CS 210 Review October 3, 2006.
Oct R McFadyen1 Observer Pattern Example From: Designed Patterns Explained by Shalloway & Trott; Addison-Wesley; P Observers: objects.
The Observer Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Observer Pattern Keeping An Eye on Things Need to introduce observer pattern formally first, include book definition & design principle Keeping An Eye.
Event-driven design will set you free The Observer Pattern 1.
OBSERVER PATTERN OBSERVER PATTERN Presented By Presented By Ajeet Tripathi ISE
February 23, 2009Observer Pattern, OOA&D, Rubal Gupta, CSPP, Winter ‘09 Observer Pattern Defines a “one-to-many” dependency between objects so that when.
Observer Pattern Context:
Chapter 5 – Design and Implementation
Chapter 7 – Object-Oriented Design
Observer Design Pattern
CMPE 135: Object-Oriented Analysis and Design October 24 Class Meeting
Observer Design Pattern
OO Design - Observer Pattern
Introduction to Behavioral Patterns (1)
Observer Pattern 1.
CMPE 135 Object-Oriented Analysis and Design March 21 Class Meeting
Observer Pattern Example
8. Observer Pattern SE2811 Software Component Design
Software Design Lecture : 40.
Software Design Lecture 11.
Presentation transcript:

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 changes various displays are updated. Humidity device Temperature device Pressure device Weather Data Object display pulls data updates displays

Spring 2010ACS-3913 Ron McFadyen2 Weather Station Page 42 The getter methods just return current values for temperature, etc. measurementsChanged() is called whenever temperature etc have changed …. So when this is called, the system must update the displays, so a first approach is: Public void measurementsChanged() { float temp = getTemperature(); float humidity = getHumidity(); float pressure = getPressure(); currentConditionsDisplay.update(temp, humidity, pressure); statisticsDisplay.update(temp, humidity, pressure); forecastDisplay.update(temp, humidity, pressure);

Spring 2010ACS-3913 Ron McFadyen3 Weather Station Page 42 WeatherData... CurrentConditionsDisplay update() display() 1 StatisticsDisplay update() display() ForecastDisplay update() display() 11 WeatherData is tightly coupled to the displays To add or remove displays it is necessary to modify the code of WeatherData … can this be avoided?

Spring 2010ACS-3913 Ron McFadyen4 Also known as publish/subscribe The essence of this pattern is that one or more objects (called observers or listeners) are registered to observe an event which may be raised by the observed object (the subject). The subject maintains a collection of the observers. Observer Pattern

Spring 2010ACS-3913 Ron McFadyen5 Observer Pattern Problem: There are many objects (observers / subscribers) needing to know of the state changes, or events, of another object (subject / publisher), and we want to keep the coupling low. Solution: The object that is responsible for the event is given the responsibility of monitoring for the event – this object is the subject. Objects that are interested in the event must register with the subject as interested parties – as observers. The subject will notify its observers when the event occurs.

Spring 2010ACS-3913 Ron McFadyen6 Observer Pattern Observer: objects that want to be notified of a certain event. An observer must have an update method whereby it is notified of an event. Subject: the object that triggers the event. It must implement: attach (observer) - add an observer to its list of observers detach (observer) - remove an observer from … notify () - goes through its list of observers calling each observer’s update method As needed - additional methods to allow an observer to get additional information The Observer Pattern defines a one to many dependency between objects so that when one object changes state, all its dependents are notified automatically

Spring 2010ACS-3913 Ron McFadyen7 Interfaces «interface» Subject attach() detach() notify() «interface» Observer update() Different implementations may give different (but usually similar) names to the above methods

Spring 2010ACS-3913 Ron McFadyen8 Generic pattern «interface» Subject attach() detach() notify() «interface» Observer update() ConcreteSubject attach() detach() notify() … ConcreteObserver update() … *

Spring 2010ACS-3913 Ron McFadyen9 Generic pattern «interface» Subject attach() detach() notify() «interface» Observer update() ConcreteSubject attach() detach() notify() … ConcreteObserver update() … * The subject may have many observers. Their types are not known. They are only known as objects that implement the observer interface. Subjects and observers are loosely coupled. The observer knows the subject and registers with that object using the attach() message. The subject sends each observer the update() message when the event occurs.

Spring 2010ACS-3913 Ron McFadyen10 Weather Station Example «interface» Subject attach() detach() notify() «interface» Observer update() WeatherData attach() detach() notify() CurrentConditionsDisplay update() display() * StatisticsDisplay update() display() ForecastDisplay update() display() «interface» DisplayElement display() The class diagram

Spring 2010ACS-3913 Ron McFadyen11 Weather Station Example «interface» Subject attach() detach() notify() «interface» Observer update() WeatherData attach() detach() notify() CurrentConditionsDisplay update() display() * StatisticsDisplay update() display() ForecastDisplay update() display() «interface» DisplayElement display() Observer Concrete observer Concrete subject observersubject Concrete observer This class diagram is adorned with a collaboration showing the pattern’s participants

Spring 2010ACS-3913 Ron McFadyen12 Weather Station Example Consider the text example. Examine the code to ensure you understand how the observer pattern is implemented. Is it pushing or pulling data from the subject? Suppose you are a developer and there is a requirement for a new type of observer (e.g. heat index display) What must you change in the existing code? What classes must you write? What methods do they have? Draw sequence diagrams to illustrate behaviour. What messages are sent When an object registers? When an object unregisters? When the event of interest occurs?

Spring 2010ACS-3913 Ron McFadyen13 Pages Java: consider the Observable class and the Observer interface. How do we rewrite our weather station example to use these features of Java? What are the advantages/disadvantages of this approach? Observable: Observer: