Observer Pattern Keeping An Eye on Things Need to introduce observer pattern formally first, include book definition & design principle Keeping An Eye.

Slides:



Advertisements
Similar presentations
The Model-View Approach in Java. OK, you’ve got “Hello World” running... What now?
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.
 Recent researches show that predicative programming can be used to specify OO concepts including classes, objects, interfaces, methods, single and multiple.
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
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.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
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.
The Observer Pattern. Formal Definition Define a one-to-many dependency between objects so that when one object changes state, all its dependents are.
Design Patterns 2 Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
3/15/05H-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Evaluating Class Diagrams Topics include: Cohesion, Coupling Law of Demeter (handout)
DESIGN PATTENS - OBSERVER PATTERN
CS 537 Group 3 Report: Activity Diagrams and Observer Design Pattern Anna Deghdzunyan Xuan Yang Keenan Knaur John Hurley.
Basic OOP in C#.
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.
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
Chapter 19 Designing the GUI front-end: the Model-View-Controller pattern.
Design Patterns Model – View – Controller. Copyright © 2001 DeLorme 28 November 2001 History ► A framework pattern for reusable applications. ► Depends.
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.
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)
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 Observer Pattern.
CS 210 Review October 3, 2006.
L10: Model-View-Controller General application structure. User Interface: Role, Requirements, Problems Design patterns: Model – View – Controller, Observer/Observable.
The Observer Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Observer / Observable COMP 401 Fall 2014 Lecture 14 10/7/2014.
My Observer Goes to 11 Model-View Controller 1. Which is the best class diagram? 2 ___Edge___ drop() _Vertex_ addEdge() dropEdge() _Iterable_ * 2 ___Edge___.
OBSERVER PATTERN OBSERVER PATTERN Presented By Presented By Ajeet Tripathi ISE
3/1/01H-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Evaluating Class Diagrams Topics include: Cohesion, Coupling Law of Demeter (handout)
February 23, 2009Observer Pattern, OOA&D, Rubal Gupta, CSPP, Winter ‘09 Observer Pattern Defines a “one-to-many” dependency between objects so that when.
Dec 2005 MSc Slide 1 Pattern that are most specifically concerned with communication between objects Cat 3: Behavioral Patterns.
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
Observer Design Pattern
Design Patterns Model – View – Controller
OO Design - Observer Pattern
Design Patterns - A few examples
Introduction to Behavioral Patterns (1)
Introduction to Event Handling
Model-View-Controller (MVC) Pattern
Observer Pattern 1.
Design pattern Lecture 9.
Advanced ProgramMING Practices
8. Observer Pattern SE2811 Software Component Design
Week 6, Class 2: Observer Pattern
Advanced ProgramMING Practices
Software Design Lecture : 40.
Software Design Lecture 11.
Presentation transcript:

Observer Pattern Keeping An Eye on Things Need to introduce observer pattern formally first, include book definition & design principle Keeping An Eye on Things Need to introduce observer pattern formally first, include book definition & design principle

Situation  WeatherData object pulls data from a weather station  Multiple ways we want to display the data  WeatherData object pulls data from a weather station  Multiple ways we want to display the data

What is WeatherData?  Initial Source Provided by Customer  Their code will call measurementsChanged any time the data changes  We can modify MeasurementsChanged and add new methods, but we can’t change anything else  Initial Source Provided by Customer  Their code will call measurementsChanged any time the data changes  We can modify MeasurementsChanged and add new methods, but we can’t change anything else

Design Thoughts  What’s Changing?  So let’s make a class for each display  We could make MeasurementsChanged call each display, but what’s wrong with that?  What’s Changing?  So let’s make a class for each display  We could make MeasurementsChanged call each display, but what’s wrong with that?

Loose Coupling  When objects interact without having knowledge of each other  Why is this good?  How does observer ensure that the coupling is loose?  When objects interact without having knowledge of each other  Why is this good?  How does observer ensure that the coupling is loose?

Let’s Look at the WeatherStation Code

Questions We Should Ponder...  Why do the observers have a reference to their subject?  What changes if they want us to add another type of display?  Why does the book have the DisplayElement interface?  Our update method is not loosely coupled (why?). What would be better?  Why do the observers have a reference to their subject?  What changes if they want us to add another type of display?  Why does the book have the DisplayElement interface?  Our update method is not loosely coupled (why?). What would be better?

How does the Observer Know what information has changed?

Pull  The Subject tells the observer “something changed”  the Observer is responsible for calling getters to get the information it wants  The Subject tells the observer “something changed”  the Observer is responsible for calling getters to get the information it wants

Push  When the subject notifies the observer, it puts the information that changed into the message

Observer In Java Is-a vs. Has-A again!

Observer Pattern > Subject registerObserver() removeObserver() notifyObservers() ConcreteSubject getState() setState() > Observer update() ConcreteObserver 1*

Observer Pattern in Java Observable addObserver() deleteObserver() notifyObservers() setChanged() ConcreteSubject getState() setState() > Observer update(Observable o, Object arg) ConcreteObserver 1*

Observable  Java’s version of Subject  Keeps track of all of the observers and notification for you  It’s a CLASS!  Java’s version of Subject  Keeps track of all of the observers and notification for you  It’s a CLASS!

Java WeatherStation

setChanged()  Tells Observable that the state of the instance has changed  notifyObservers() will only notify them if the state has changed  Why would they do that?  Tells Observable that the state of the instance has changed  notifyObservers() will only notify them if the state has changed  Why would they do that?

if (newTemp != currTemp) { currTemp = newTemp; setChanged(); } if (newHumidity != currHumidity) { currHumidity = newHumidity; setChanged(); } if (newPressure != currPressure) { currPressure = newPressure; setChanged(); } notifyObservers(); We can make many changes to the state and only call notifyObservers() Once. Update() is only called if something really changed

Pull in Java  notifyObservers() takes no parameters  Update(Observable o, Object arg)  o is reference to the subject observer can use to get the pieces of state it needs  arg is null  notifyObservers() takes no parameters  Update(Observable o, Object arg)  o is reference to the subject observer can use to get the pieces of state it needs  arg is null

Push in Java  notifyObservers(Object arg)  Update(Observable o, Object arg)  o is a reference to the subject causing the update  arg is the message from the subject  notifyObservers(Object arg)  Update(Observable o, Object arg)  o is a reference to the subject causing the update  arg is the message from the subject

Observable is a class  But can only have one super class  Can we contain a subject?  Nope - they protected notifyObservers  If you have a parent, implement it like we did earlier  But can only have one super class  Can we contain a subject?  Nope - they protected notifyObservers  If you have a parent, implement it like we did earlier