OO Design - Observer Pattern

Slides:



Advertisements
Similar presentations
©Ian Sommerville 2000 Software Engineering, 6th edition. Chapter 12Slide 1 Software Design l Objectives To explain how a software design may be represented.
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
Decorator Pattern Lecture Oo29 Artificial Life Simulation.
Chapter 7 – Object-Oriented Design
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.
A Brief Introduction to Software Design and Design Quality By Laura Leventhal.
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,
1 Object Oriented Design & Patterns Part 1. 2 Design Patterns Derived from architectural patterns: –rules for design of buildings –describe common problems,
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
Design Patterns.
Observer Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al.
Design Patterns Part two. Structural Patterns Concerned with how classes and objects are composed to form larger structures Concerned with how classes.
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 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.
Observer Please Snarf the Code for Today’s Class..
Design Patterns. OO-Concepts Don’t rewrite code Encapsulation Inheritance Write flexible code.
CSE 332: Design Patterns Review: Design Pattern Structure A design pattern has a name –So when someone says “Adapter” you know what they mean –So you can.
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.
Inspired by the Oulipu. The 3 Tenets of OO Encapsulation Polymorphism Inheritance.
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:
04 - OOD Intro.CSC4071 Software Design ‘Requirements’ defines –The goals the system needs to satisfy. ‘Specification’ defines –The externally-observable.
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
The Observer Design Pattern Author :Erich Gamma, et al. Source :Elements of Reusable Object-Oriented Software Speaker : Chiao-Ping Chang Advisor : Ku-Yaw.
Microsoft Foundation Classes MFC
Design Patterns: MORE Examples
Chapter 5 – Design and Implementation
Chapter 7 – Object-Oriented Design
Introduction to Design Patterns
Observer Design Pattern
CMPE 135: Object-Oriented Analysis and Design October 24 Class Meeting
Observer Design Pattern
Architectural Patterns for Interactive Software
Presented by Igor Ivković
Patterns of Interaction 2: Publish-Subscribe
Model-View-Controller
State Design Pattern 1.
Introduction to Behavioral Patterns (1)
OO Design Patterns - Decorator
Object-Oriented Programming
Object Oriented Design Patterns - Behavioral Patterns
Review: Design Pattern Structure
Design pattern Lecture 9.
State Design Pattern Brandon Jacobsen.
CMPE 135 Object-Oriented Analysis and Design March 21 Class Meeting
Introduction to Design Patterns
Design Patterns Lecture part 1.
8. Observer Pattern SE2811 Software Component Design
Presented by Igor Ivković
Software Design Lecture 11.
Presentation transcript:

OO Design - Observer Pattern Object-Oriented Programming

The Weather Monitoring application Đại học Công nghệ - ĐHQG HN OO Design Pattern

WeatherData API Our job: to implement measurementsChanged() so that it updates displays Đại học Công nghệ - ĐHQG HN OO Design Pattern

Our job to implement measurementsChanged() so that it updates the displays: Given: Getter methods available for temperature, humidity, and pressure measurementsChanged() is called each time new weather data is available We need to implement three displays that update every time WeatherData has new data Can add new displays and remove displays Đại học Công nghệ - ĐHQG HN OO Design Pattern

First draft solution Đại học Công nghệ - ĐHQG HN OO Design Pattern

What’s wrong? We are coding to concrete implementation, not interfaces For every new display element, we need to alter code. We have no way to add (or remove) display elements at run time The display elements don’t implement a common interface. We haven’t encapsulated the part that changes We are violating encapsulation of the WeatherData class. What’s wrong? Đại học Công nghệ - ĐHQG HN OO Design Pattern

What’s wrong? We are coding to concrete implementation, not interfaces For every new display element, we need to alter code. We have no way to add (or remove) display elements at run time The display elements don’t implement a common interface. We haven’t encapsulated the part that changes We are violating encapsulation of the WeatherData class. What’s wrong? Đại học Công nghệ - ĐHQG HN OO Design Pattern

The Observer Pattern How newspaper or magazine subscriptions work? Subjects + Observers = ObserverPattern Đại học Công nghệ - ĐHQG HN OO Design Pattern

The Observer Pattern Đại học Công nghệ - ĐHQG HN OO Design Pattern

The Observer Pattern Đại học Công nghệ - ĐHQG HN OO Design Pattern

The Observer Pattern Đại học Công nghệ - ĐHQG HN OO Design Pattern

Đại học Công nghệ - ĐHQG HN OO Design Pattern

Đại học Công nghệ - ĐHQG HN OO Design Pattern

Đại học Công nghệ - ĐHQG HN OO Design Pattern

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 Đại học Công nghệ - ĐHQG HN OO Design Pattern

In Class Diagram Đại học Công nghệ - ĐHQG HN OO Design Pattern

Benefits of the Observer Pattern Subjects and observers can interact but have very little knowledge of each other. The only thing the subject knows about an observer is that it implements the Observer interface We can add new observers at any time. We never need to modify the subject to add new type 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 Đại học Công nghệ - ĐHQG HN OO Design Pattern

Đại học Công nghệ - ĐHQG HN OO Design Pattern

Back to your task to implement measurementsChanged() so that it updates the displays: Given: Getter methods available for temperature, humidity, and pressure measurementsChanged() is called each time new weather data is available We need to implement three displays that update every time WeatherData has new data Can add new displays and remove displays Đại học Công nghệ - ĐHQG HN OO Design Pattern

Đại học Công nghệ - ĐHQG HN OO Design Pattern

Homework Reimplement using java.util.Observable Đại học Công nghệ - ĐHQG HN OO Design Pattern