CS 210 Introduction to Design Patterns September 7 th, 2006.

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

1 Dept. of Computer Science & Engineering, York University, Toronto CSE3311 – Software Development Observer Pattern.
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.
Chapter 2: The Observer Pattern. Consider the Following Application Application specification Humidity Temperature Pressure Weather Station Weather Data.
March Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem.
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,
The Observer Pattern. Formal Definition Define a one-to-many dependency between objects so that when one object changes state, all its dependents are.
March Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem.
Oct Ron McFadyen1 Collaborations Collaboration : an arrangement of classes, links, roles in a context to implement some behaviour. Useful for.
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.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
DESIGN PATTENS - OBSERVER PATTERN
Behavioral Patterns  Behavioral patterns are patterns whose purpose is to facilitate the work of algorithmic calculations and communication between classes.
Design Patterns.
CS 537 Group 3 Report: Activity Diagrams and Observer Design Pattern Anna Deghdzunyan Xuan Yang Keenan Knaur John Hurley.
Chapter 1: Introduction to Design Patterns. SimUDuck Example.
Basic OOP in C#.
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.
Implementing Design Patterns Using Java St. Louis Java Special Interest Group Eric M. Burke Object Computing, Inc. Presented on July 9, 1998 (updated July.
CS 210 Introduction to Design Patterns September 28 th, 2006.
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.
S OFTWARE D ESIGN Design Patterns 3 10/8/2015 Computer Science Department, TUC-N.
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.
Behavioral Design Patterns Morteza Yousefi University Of Science & Technology Of Mazandaran 1of 27Behavioral Design Patterns.
CS 210 Adapter Pattern October 19 th, Adapters in real life Page 236 – Head First Design Patterns.
Patterns in programming1. 2 What are patterns? Answers to common design problems. A language used by developers –To discuss answers to design problems.
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.
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)
CS 210 Final Review November 28, CS 210 Adapter Pattern.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAMMING PRACTICES Model View.
The Observer Pattern.
CS 210 Review October 3, 2006.
Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another using Arrays. How does one work with these.
Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for another object to control access to it by creating a representative object.
Watching the movie the hard way…. Page 256 – Head First Design Patterns.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
The Observer Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
StarBuzz Coffee Recipe Boil some water Brew coffee in boiling water Pour coffee in cup Add sugar and milk Tea Recipe Boil some water Steep tea in boiling.
Observer / Observable COMP 401 Fall 2014 Lecture 14 10/7/2014.
An object's behavior depends on its current state. Operations have large, multipart conditional statements that depend on the object's state.
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
CS 210 Introduction to Design Patterns September 14 th, 2006.
Observer Pattern Context:
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)
Design pattern Lecture 9.
ADAPTER FAÇADE FACTORY OBSERVER SINGLETON STRATEGY
CMPE 135 Object-Oriented Analysis and Design March 21 Class Meeting
Advanced ProgramMING Practices
Design Patterns Lecture part 1.
8. Observer Pattern SE2811 Software Component Design
Advanced ProgramMING Practices
Software Design Lecture : 40.
Software Design Lecture 11.
Presentation transcript:

CS 210 Introduction to Design Patterns September 7 th, 2006

Head First Design Patterns Chapter 2 Observer Pattern

Weather Monitoring Application Weather Station Humidity Sensor Temp Sensor Pressure Sensor Weather Data Object Display Device Pulls Data displays

What needs to be done? /* * Call this method * whenever measurements are * Updated */ Public void measurementsChanged(){ // your code goes here } WeatherData getTemperature() getHumidity() getPressure() measurementsChanged() Update three different displays

Problem specification weatherData class has three getter methods measurementsChanged() method called whenever there is a change Three display methods needs to be supported: current conditions, weather statistics and simple forecast System should be expandable

First cut at implementation public class WeatherData { 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); } // other methods }

First cut at implementation public class WeatherData { 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); } // other methods } By coding to concrete implementations there is no way to add additional display elements without making code change Area of change which can be Managed better by encapsulation

Basis for observer pattern Fashioned after the publish/subscribe model Works off similar to any subscription model Buying newspaper Magazines List servers

Observer Pattern Defined The Observer Pattern defines a one-to- many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically.

Observer Pattern – Class diagram > Subject registerObserver() removeObserver() notifyObservers() > Observer Update() observers ConcreteSubject registerObserver() removeObserver() notifyObservers() ConcreteObserver Update() subject

Observer pattern – power of loose coupling The only thing that the subject knows about an observer is that it implements an interface Observers can be added at any time and subject need not be modified to add observers Subjects and observers can be reused or modified without impacting the other [as long as they honor the interface commitments]

Observer Pattern – Weather data > Subject registerObserver() removeObserver() notifyObservers() > Observer update() observers WeatherData registerObserver() removeObserver() notifyObservers() getTemperature() getPressure() measurementsChanged() subject > DisplayElement display() StatisticsDisplay update() display() ForecastDisplay update() display() CurrentConditionsDisplay update() display()

Weather data interfaces public interface Subject { public void registerObserver(Observer o); public void removeObserver(Observer o); public void notifyObservers(); } public interface Observer { public void update(float temp, float humidity, float pressure); } public interface DisplayElement { public void display(); }

Implementing subject interface public class WeatherData implements Subject { private ArrayList observers; private float temperature; private float humidity; private float pressure; public WeatherData() { observers = new ArrayList(); }

Register and unregister public void registerObserver(Observer o) { observers.add(o); } public void removeObserver(Observer o) { int i = observers.indexOf(o); if (i >= 0) { observers.remove(i); }

Notify methods public void notifyObservers() { for (int i = 0; i < observers.size(); i++) { Observer observer = (Observer)observers.get(i); observer.update(temperature, humidity, pressure); } public void measurementsChanged() { notifyObservers(); }

Observer pattern More analysis

Push or pull The notification approach used so far pushes all the state to all the observers One can also just send a notification that some thing has changed and let the observers pull the state information Java observer pattern support has built in support for both push and pull in notification

Java Observer Pattern – Weather data Observable addObserver() deleteObserver() notifyObservers() setChanged() > Observer update() observers WeatherData registerObserver() removeObserver() notifyObservers() getTemperature() getPressure() measurementsChanged() subject > DisplayElement display() StatisticsDisplay update() display() ForecastDisplay update() display() CurrentConditionsDisplay update() display() Observable is a class And not an interface

Java implementation Look at API documentation java.util.Observable java.util.Observer Look at weather station re- implementation

Problems with Java implementation Observable is a class You have to subclass it You cannot add observable behavior to an existing class that already extends another superclass You have to program to an implementation – not interface Observable protects crucial methods Methods such as setChanged() are protected and not accessible unless one subclasses Observable. You cannot favor composition over inheritance. You may have to roll your own observer interface if Java utilities don’t work for your application

Other uses of the Observer pattern in Java GUI interface classes – JButton Look at Java API for AbstractButton and JButton

Summary so far.. Observer pattern defines one-to-many relationship between objects You can use push or pull with observer pattern Java has several implementations of observer pattern – in util, swing, javabeans and RMI Swing makes heavy use of this pattern

Summary so far.. OO Basics Encapsulation Inheritance Polymorphism OO Principles Encapsulate what varies Favor composition over inheritance Program to interfaces not to implementations Strive for loosely coupled designs between objects that interact OO Patterns The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically. The Strategy Pattern defines a family of algorithms, Encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.