The Observer Pattern.

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.
Chapter 2: The Observer Pattern. Consider the Following Application Application specification Humidity Temperature Pressure Weather Station Weather Data.
OOP Design Patterns Chapters Design Patterns The main idea behind design patterns is to extract the high level interactions between objects and.
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,
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.
Winter 2007ACS-3913 Ron McFadyen1 Classes Represented by a rectangle with possibly 3 compartments Customer Name Address Customer Name Address getName()
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,
Threads II. Review A thread is a single flow of control through a program Java is multithreaded—several threads may be executing “simultaneously” If you.
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.
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.
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
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.
CS 210 Adapter Pattern October 19 th, Adapters in real life Page 236 – Head First Design Patterns.
An Introduction to Programming and Object Oriented Design using Java 3 rd Edition. Dec 2007 Jaime Niño Frederick Hosch Chapter 18 Integrating user interface.
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.
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.
Behavioral Patterns1 Nour El Kadri SEG 3202 Software Design and Architecture Notes based on U of T Design Patterns class.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAMMING PRACTICES Model View.
(1) Introduction to Java GUIs Philip Johnson Collaborative Software Development Laboratory Information and Computer Sciences University of Hawaii Honolulu.
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.
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.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
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
Event Driven (Asynchronous) Programming. Event handling in Unity Subclass a class that contains event handling methods, and then override those methods.
February 23, 2009Observer Pattern, OOA&D, Rubal Gupta, CSPP, Winter ‘09 Observer Pattern Defines a “one-to-many” dependency between objects so that when.
Behavioral Patterns Algorithms and the assignment of responsibilities among objects Describe not just patterns of objects or classes but also the patterns.
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)
Introduction to Event Handling
Design pattern Lecture 9.
Constructors, GUI’s(Using Swing) and ActionListner
CMPE 135 Object-Oriented Analysis and Design March 21 Class Meeting
Advanced ProgramMING Practices
8. Observer Pattern SE2811 Software Component Design
Advanced ProgramMING Practices
Software Design Lecture : 40.
Software Design Lecture 11.
Presentation transcript:

The Observer Pattern

Statement of Work Given WeatherData object, which tracks current weather conditions (temperature,humidity, barometric pressure). Our job is to Create an application that uses WeatherData object to create real time display for current conditions, weather statics, and simple forecast

WeatherData Object Display data Pull data

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

We are given WeatherData class Three getters to retrieve data WeatherData getTemperature() getHumidity() getPressure() measurementsChanged() //other methods When data change, this method is called to update three (?) displays

What needs to be done? Update three different displays /* WeatherData getTemperature() getHumidity() getPressure() measurementsChanged() /* * Call this method * whenever measurements are * Updated */ Public void measurementsChanged(){ // your code goes here } 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 Area of change which can be Managed better by encapsulation By coding to concrete implementations there is no way to add additional display elements without making code change

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

Newspaper subscriptions A publisher begins to publishing newspapers Subscribers subscribe to one(more) newspaper(s) Every time there are new editions of the newspapers, they are delivered to subscribers. Customers can subscribe/unsubscribe to the newspapers.

Publishers (Subject) + Subscribers (Observer) Observer Pattern Publishers (Subject) + Subscribers (Observer) = 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. 《interface》 Subject registerObserver() removeObserver() notifyObserver()

registerObserver(){…} 《interface》 Subject registerObserver() removeObserver() notifyObserver() 《interface》 Observer update() ConcreteSubject registerObserver(){…} removeObserver(){…} notifyObserver(){…} getState() setState() ConcreteObserver update(){…} // other methods

The Power of Loose Coupling Observer pattern provides an object design where subjects and observers are loosely coupled The only thing the subject knows about an observer is that it implements a certain interface New observers can be added at any time We never need to modify the subject to add new types of observers We can reuse subjects or observers independently of each other Changes to either the subject or an observer will not affect the other.

Design Principle #4 Strive for loosely coupled designs between objects that interact.

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

registerObserver(){…} removeObserver(){…} notifyObserver(){…} 《interface》 Subject registerObserver() removeObserver() notifyObserver() 《interface》 Observer update() ForecastDisplay update(){…} display(){…} // other methods WeatherData registerObserver(){…} removeObserver(){…} notifyObserver(){…} getTemperature() getHumidity() getPressure() measurementsChanged() CurrentConditionDisplay update(){…} display(){…} // other methods StatisticsDisplay update(){…} display(){…} // other methods

registerObserver(){…} removeObserver(){…} notifyObserver(){…} 《interface》 Subject registerObserver() removeObserver() notifyObserver() 《interface》 Observer update() ForecastDisplay update(){…} display(){…} // other methods WhateverDisplay update(){…} display(){…} // other methods WeatherData registerObserver(){…} removeObserver(){…} notifyObserver(){…} getTemperature() getHumidity() getPressure() measurementsChanged() CurrentConditionDisplay update(){…} display(){…} // other methods StatisticsDisplay update(){…} display(){…} // other methods

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();

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 something 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 implementation Look at API documentation java.util.Observable java.util.Observer Look at weather station re-implementation

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

Behind the scenes (pseudocode for Observable class) setChange(){ changed = true; } notifyObservers(Object arg){ if (changed){ for every observer on the list { call update(this, arg) } changed = false; notifyObservers(){ notifyObservers(null);

Behind the scenes (For Observer interface) update(Observable o, Object arg){ }

import java.util.Observable; import java.util.Observer; importing import java.util.Observable; import java.util.Observer; public class WeatherData extends Observable { private float temperature; private float humidity; private float pressure; public WeatherData() { } public void measurementsChanged() { setChanged(); notifyObservers(); } public void setMeasurements(float temperature, float humidity, float pressure) { this.temperature = temperature; this.humidity = humidity; this.pressure = pressure; measurementsChanged(); public float getTemperature() { return temperature; public float getHumidity() { return humidity; public float getPressure() { return pressure; subclassing Don’t keep track observers Constructor does not need to create data structure for observers Indicate “change of state” and PULL data

Implementing the interface import java.util.Observable; import java.util.Observer; public class CurrentConditionsDisplay implements Observer, DisplayElement { Observable observable; private float temperature; private float humidity; public CurrentConditionsDisplay(Observable observable) { this.observable = observable; observable.addObserver(this); } public void update(Observable obs, Object arg) { if (obs instanceof WeatherData) { WeatherData weatherData = (WeatherData)obs; this.temperature = weatherData.getTemperature(); this.humidity = weatherData.getHumidity(); display(); public void display() { System.out.println("Current conditions: " + temperature + "F degrees and " + humidity + "% humidity"); Takes an Observable and add the current Condition object as an Observer

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

Java Event Handling Example Name in Design Pattern Actual Name in JButton Event Handling Subject JButton Observer ActionListener ConcreteObserver The class that implements ActionListener interface Attach() addActionListener Notify() actionPerformed

class ButtonActionListener implement ActionListener{ public void actionPerformed(ActionEvent actionEvent) { ... } ActionListener listener = new ButtonActionListener(); JButton button = new JButton("Pick Me"); button.addActionListener(listener);

Add angel and devil as observers of the button public class SwingObserverExample { JFrame frame; public static void main(String[] args) { SwingObserverExample example = new SwingObserverExample(); example.go(); } public void go() { frame = new JFrame(); JButton button = new JButton("Should I do it?"); button.addActionListener(new AngelListener()); button.addActionListener(new DevilListener()); frame.getContentPane().add(BorderLayout.CENTER, button); // Set frame properties frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300,300); frame.setVisible(true); class AngelListener implements ActionListener { public void actionPerformed(ActionEvent event) { System.out.println("Don't do it, you might regret it!"); class DevilListener implements ActionListener { System.out.println("Come on, do it!"); Add angel and devil as observers of the button When state changes, actionPerformed is called

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

Design Principle for Observer Pattern Encapsulate what varies What varies in the Observer Pattern is the state of the Subject and the number and types of Observers. With this pattern, you can vary the objects that are dependent on the state of the Subject, without having to change that Subject. Plan ahead! Favor composition over inheritance The Observer Pattern uses composition to compose any number of Observers with their Subjects. These relationships are not set up by some kind of inheritance hierarchy. No, they are set up at runtime by composition! Program to interfaces not to implementations Strive for loosely coupled designs between objects that interact Both the Subject and Observer use interfaces. The Subject keeps track of objects implementing the Observer interface, while the observers register with, and get notified by, the Subject interface. This keeps things nice and loosely coupled.

Observer pattern Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. [GoF, p293] The "View" part of Model-View-Controller.

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

Typical usages Listening for an external event. Listening for changes of the value of an object property. In a mailing list, where every time an event happens (a new product, a gathering, etc.) a message is sent to the people subscribed to the list.

In the model-view-controller (MVC) paradigm, the observer pattern is used to create a loose coupling between the model and the view. Typically, a modification in the model triggers the notification of model observers which are actually the views.

Summary so far.. OO Basics OO Principles OO Patterns 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.