Software Design Lecture : 40.

Slides:



Advertisements
Similar presentations
Design Patterns.
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)
Lecture 12 Observer Pattern UML Class Diagrams (repetition) Use Case Diagrams Sequence Diagrams Statechart Diagrams.
Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
CSE3308/CSC Software Engineering: Analysis and DesignLecture 5B.1 Software Engineering: Analysis and Design - CSE3308 Patterns CSE3308/CSC3080/DMS/2000/12.
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.
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,
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 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.
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.
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.
CSE 331 Software Design & Implementation Dan Grossman Winter 2014 Events, Listeners, and Callbacks (Based on slides by Mike Ernst, David Notkin, Hal Perkins)
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.
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.
CSC 313 – Advanced Programming Topics. Observer Pattern in Java  Java ♥ Observer Pattern & uses everywhere  Find pattern in JButton & ActionListener.
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.
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 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.
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 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.
High degree of user interaction Interactive Systems: Model View Controller Presentation-abstraction-control.
February 23, 2009Observer Pattern, OOA&D, Rubal Gupta, CSPP, Winter ‘09 Observer Pattern Defines a “one-to-many” dependency between objects so that when.
Construction Lecture Oo21 Gymnastics System Example Cont’d.
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:
Chapter 7 – Object-Oriented Design
A Brief Introduction to Design Patterns
Observer Design Pattern
Observer Design Pattern
Architectural Patterns for Interactive Software
Patterns of Interaction 2: Publish-Subscribe
Design Patterns - A few examples
CSE 331 Software Design and Implementation
CSE 331 Software Design and Implementation
Design Patterns, cont..
Introduction to Behavioral Patterns (1)
Mediator.
Observer Pattern 1.
Review: Design Pattern Structure
CSE 331 Software Design & Implementation
Observer Design Pattern
Design pattern Lecture 9.
Advanced ProgramMING Practices
Design Patterns Lecture part 1.
8. Observer Pattern SE2811 Software Component Design
Week 6, Class 2: Observer Pattern
Advanced ProgramMING Practices
Model, View, Controller design pattern
Software Design Lecture : 39.
CSE 331 Software Design & Implementation
Encapsulation.
Presentation transcript:

Software Design Lecture : 40

Observer Design Pattern OR Publish and Subscribe

Motivation for Observer Design Pattern When we partition a system into a collection of cooperation classes, it is desired that consistent state between participating objects is to be maintained. This should be not achieved via tight coupling as against our basis design principle because for obvious reason this will reduce reusability

One Data multiple Representations

Myth about the Diagram Bar Graph and Pie Chart don’t know about each other so that any one can be reused independent of each other, but the interesting thing is that it seems that they know each other. How??? When the data in the spreadsheet is changed it is reflected in pie chart and bar graph also immediately

Myth about the diagram This behavior implies that they are dependent on data of the spreadsheet and when ever there is a change in spreadsheet pie chart and bar graph is notified to update the change. There seems to be no reason to believe that the number of objects representing the data is to be limited may be i- e may be line graph is to be used in future to represent data

Observations There exists a consistent communication model between a set of dependent objects and an object that they are dependent on. This allows the dependent objects to have their state synchronized with the object that they are dependent on.

Observers and Subjects (Observable) The set of dependent objects are referred to as Observers i-e Graphs in our example The object on which Observer dependent is referred to as the subject. i-e Spreadsheet in our example

Newspaper Subscription Example

Case for Observer Design Pattern Observer pattern suggests a publisher-subscriber model leading to a clear boundary between the set of Observer objects and the Subject object. A typical observer is an object with interest or dependency in the state of the subject.

Case for Observer Design Pattern The subject cannot maintain a static list of such observers as the list of observers for a given subject could change dynamically. Hence any object with interest in the state of the subject needs to explicitly register itself as an observer with the subject Whenever the subject undergoes a change in its state, it notifies all of its registered observers. Upon receiving notification from the subject, each of the observers queries the subject to synchronize its state with that of the subject’s.

One – to Many Relationship In other words, the scenario contains a one-to-many relationship between a subject and the set of its observers. Each of the observer objects has to register itself with the subject to get notified when there is a change in the subject’s state

Observer Pattern Defined “This pattern defines a one-to-many relationship between objects so that when there is change in the state of the one object it should be notified and automatically updated to all of it’s dependent”

Communication Mechanism The subject should provide an interface for registering and unregistering for change notifications. In the pull model — The subject should provide an interface that enables observers to query the subject for the required state information to update their state. In the push model — The subject should send the state information that the observers may be interested in.

Class Diagram

Sequence Diagram

Consequences Support for event broadcasting Minimal coupling between the Subject and the Observer . Reuse obervers without using subject and vice verca

Consequences Liabilities: Possible cascading of notifications Observers are not necessarily aware of each other and must be careful about triggering updates

Observer Pattern in Java Java provides the Observable/Observer classes as built-in support for the Observer pattern. The java.util.Observable class is the base Subject class. Any class that wants to be observed extends this class. Provides methods to add/delete observers Provides methods to notify all observers A subclass only needs to ensure that its observers are notified in the appropriately Uses a Vector for storing the observer references

Observer Interface The java.util.Observer interface is the Observer interface. It must be implemented by any observer class.

Sample Code

public class ConcreteSubject extends Observable { private String name; // To be observed private float price; public ConcreteSubject(String name, float price) { this.name = name; this.price = price; System.out.println("ConcreteSubject created: " + name + " at “ + price); }

public String getName() {return name; } public float getPrice() {return price;} public void setName(String name) { this.name = name; setChanged(); // Methods Implemented in Java notifyObservers(name); public void setPrice(float price) { this.price = price; setChanged(); notifyObservers(new Float(price)); }}

// An observer of name changes // An observer of name changes. public class NameObserver implements Observer { private String name; public NameObserver() { name = null; System.out.println("NameObserver created: Name is " + name);} public void update(Observable obj, Object arg) { if (arg instanceof String) { name = (String)arg; System.out.println("NameObserver: Name changed to " + name); } else { System.out.println("NameObserver: Some other change to subject!");}}}

// An observer of price changes // An observer of price changes. public class PriceObserver implements Observer { private float price; public PriceObserver() { price = 0; System.out.println("PriceObserver created: Price is " + price); } public void update(Observable obj, Object arg) { if (arg instanceof Float) { price = ((Float)arg).floatValue(); System.out.println("PriceObserver: Price changed to " + price); } else { System.out.println(”PriceObserver: Some other change to subject!"); }

public class TestObservers { public static void main(String args[]) { // Create the Subject and Observers. ConcreteSubject s = new ConcreteSubject("Corn Pops", 1.29f); NameObserver nameObs = new NameObserver(); PriceObserver priceObs = new PriceObserver(); // Add those Observers! s.addObserver(nameObs); s.addObserver(priceObs); // Make changes to the Subject. s.setName(“Corn Flakes"); s.setPrice(4.57f); s.setPrice(9.22f); s.setName("Sugar Crispies");}}

Output of the Program ConcreteSubject created: Corn Pops at 1.29 NameObserver created: Name is null PriceObserver created: Price is 0.0 PriceObserver: Some other change to subject NameObserver: Name changed to Corn Flakes PriceObserver: Price changed to 4.58 NameObserver: Some other change to subject! PriceObserver: Price changed to 9.22 NameObserver: Name changed to Sugar Crispies