Applying the Observer Design Pattern

Similar documents
Using Design Patterns in Java Application Development

Applying the Decorator Design Pattern

Applying the Factory Method Design Pattern

Design Pattern. CMPSC 487 Lecture 10 Topics: Design Patterns: Elements of Reusable Object-Oriented Software (Gamma, et al.)

Ingegneria del Software Corso di Laurea in Informatica per il Management. Design Patterns part 1

SDC Design patterns GoF

Slide 1. Design Patterns. Prof. Mirco Tribastone, Ph.D

Design Patterns. An introduction

Ingegneria del Software Corso di Laurea in Informatica per il Management. Design Patterns part 1

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

The Strategy Pattern Design Principle: Design Principle: Design Principle:

Nat. Thomas Software Engineering Telephonics Inc. Long Island IEEE Lecture Series

Object-Oriented Design

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

Design Patterns. Manuel Mastrofini. Systems Engineering and Web Services. University of Rome Tor Vergata June 2011

Object Oriented Methods with UML. Introduction to Design Patterns- Lecture 8

EPL 603 TOPICS IN SOFTWARE ENGINEERING. Lab 6: Design Patterns

Object-Oriented Oriented Programming

DESIGN PATTERN - INTERVIEW QUESTIONS

Design patterns. Jef De Smedt Beta VZW

Design Patterns. CSCE Lecture 17-10/28/2015 (Partially adapted from Head First Design Patterns by Freeman, Bates, Sierra, and Robson)

Software Design Patterns. Background 1. Background 2. Jonathan I. Maletic, Ph.D.

Application Architectures, Design Patterns

1 Software Architecture

Software Design COSC 4353/6353 D R. R A J S I N G H

Object-oriented Software Design Patterns

Design Patterns. Dr. Rania Khairy. Software Engineering and Development Tool

What is Design Patterns?

Trusted Components. Reuse, Contracts and Patterns. Prof. Dr. Bertrand Meyer Dr. Karine Arnout

Design Patterns. Hausi A. Müller University of Victoria. Software Architecture Course Spring 2000

Socket attaches to a Ratchet. 2) Bridge Decouple an abstraction from its implementation so that the two can vary independently.

Laboratorio di Tecnologie dell'informazione

Tuesday, October 4. Announcements

Modellistica Medica. Maria Grazia Pia, INFN Genova. Scuola di Specializzazione in Fisica Sanitaria Genova Anno Accademico

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

Software Reengineering Refactoring To Patterns. Martin Pinzger Delft University of Technology

Idioms and Design Patterns. Martin Skogevall IDE, Mälardalen University

Design Patterns. Gunnar Gotshalks A4-1

Review Software Engineering October, 7, Adrian Iftene

Object Oriented Paradigm

CS560. Lecture: Design Patterns II Includes slides by E. Gamma et al., 1995

Topics in Object-Oriented Design Patterns

CS251 Software Engineering Lectures 18: Intro to DP

Recognizing And Understanding AntiPatterns In Java Application Development

APPLYING DESIGN PATTERNS TO SCA IMPLEMENTATIONS

Patterns. Erich Gamma Richard Helm Ralph Johnson John Vlissides

Object-Oriented Design

Introduction to Software Engineering: Object Design I Reuse & Patterns

MVC. Model-View-Controller. Design Patterns. Certain programs reuse the same basic structure or set of ideas

Overview of Patterns: Introduction

The GoF Design Patterns Reference

Design Patterns: Structural and Behavioural

Keywords: Abstract Factory, Singleton, Factory Method, Prototype, Builder, Composite, Flyweight, Decorator.

3 Product Management Anti-Patterns by Thomas Schranz

SOLID DESIGN PATTERNS FOR MERE MORTALS

Extensibility Design Patterns From The Initial Stage of Application Life-Cycle

Brief Note on Design Pattern

Applying Design Patterns to accelerate development of reusable, configurable and portable UVCs. Accellera Systems Initiative 1

Design Patterns. Observations. Electrical Engineering Patterns. Mechanical Engineering Patterns

UNIT I Introduction to Design Patterns

Introduction and History

CS/CE 2336 Computer Science II

Information systems modelling UML and service description languages

2.1 Design Patterns and Architecture (continued)

What is Design Patterns?

Design patterns. OOD Lecture 6

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

2.1 Design Patterns and Architecture (continued)

Applying Design Patterns to SCA Implementations

The Observer Pattern

Design Patterns. CSC207 Fall 2017

A Primer on Design Patterns

UNIT I Introduction to Design Patterns

Design Patterns Reid Holmes

OODP Session 4. Web Page: Visiting Hours: Tuesday 17:00 to 19:00

A Rapid Overview of UML

Software Engineering I (02161)

Outline. Design Patterns. Observer Pattern. Definitions & Classifications

A Metric for Measuring the Abstraction Level of Design Patterns

LECTURE NOTES ON DESIGN PATTERNS MCA III YEAR, V SEMESTER (JNTUA-R09)

Summary of the course lectures

DESIGN PATTERNS MOCK TEST DESIGN PATTERNS MOCK TEST II

Design Patterns. "Gang of Four"* Design Patterns. "Gang of Four" Design Patterns. Design Pattern. CS 247: Software Engineering Principles

Design Patterns. it s about the Observer pattern, the Command pattern, MVC, and some GUI. some more

Applying Some Gang of Four Design Patterns CSSE 574: Session 5, Part 3

CS 247: Software Engineering Principles. Design Patterns

Modellistica Medica. Maria Grazia Pia, INFN Genova. Scuola di Specializzazione in Fisica Sanitaria Genova Anno Accademico

DESIGN PATTERNS FOR MERE MORTALS

be used for more than one use case (for instance, for use cases Create User and Delete User, one can have one UserController, instead of two separate

What is Design Patterns?

Design Patterns. CSC207 Winter 2017

Lecture 4: Observer Pattern, Event Library and Componentization

Design Pattern What is a Design Pattern? Design Pattern Elements. Almas Ansari Page 1

TDDB84: Lecture 6. Adapter, Bridge, Observer, Chain of Responsibility, Memento, Command. fredag 4 oktober 13

Design Patterns. Manuel Mastrofini. Systems Engineering and Web Services. University of Rome Tor Vergata June 2011

A Metric of the Relative Abstraction Level of Software Patterns

Object Oriented Programming. Michał Bereta

A Reconnaissance on Design Patterns

An Introduction to Patterns

Developing Java Applications with OSGi Capital District Java Developers Network. Michael P. Redlich March 20, 2008

Transcription:

Applying the Observer Design Pattern Trenton Computer Festival Professional Seminars Michael P. Redlich (908) 730-3416 michael.p.redlich@exxonmobil.com

About Myself Degree B.S. in Computer Science Rutgers University (go Scarlet Knights!) ExxonMobil Research & Engineering Clinton, New Jersey Senior Research Technician (1988-1998, 2004-present) Systems Analyst (1998-2002) Ai-Logix, Inc. Somerset, New Jersey Technical Support Engineer (2003-2004) 1

About Myself (continued) ACGNJ Java Users Group Leader Secretary Publications Avoid Excessive Subclassing with the Decorator Design Pattern + Barry Burd and Michael Redlich + Java Boutique, January 27, 2006 James: The Java Apache Mail Enterprise Server + Barry Burd and Michael Redlich + Java Boutique, September 30, 2005 2

Example Source Code The example source code was adapted from: Head First Design Patterns + Eric & Elisabeth Freeman (with Kathy Sierra & Bert Bates) Download example source code from: http://tcf.redlich.net/ 3

Gang of Four (GoF) Erich Gamma Richard Helm Ralph Johnson John Vlissides Design Patterns Elements of Reusable Object- Oriented Software ISBN 0-201-63361-2 1995 4

Gang of Four (GoF) Next Generation? Eric Freeman Elisabeth Freeman Kathy Sierra Bert Bates Head First Design Patterns ISBN 0-596-00712-4 2004 5

What are Design Patterns? A pattern is a solution to a problem in a context The context is the situation in which the pattern applies The problem refers to the desired goal in the context, but also refers to any constraints that may occur The solution is a general design that anyone can apply If you find yourself in a context with a problem that has a goal that is affected by a set of constraints, then you can apply a design that resolves the goal and constraints and leads to a solution. 6

What are Design Patterns? (continued) Recurring solutions to software design problems that are repeatedly found in real-world application development All about the design and interaction of objects Four essential elements: The pattern name The problem The solution The consequences 7

How Design Patterns Solve Design Problems Find appropriate objects Helps identify less obvious abstractions Design for change Avoid creating objects directly Avoid dependencies on specific operations Avoid algorithmic dependencies Avoid tight coupling 8

Thinking in Design Patterns Keep it simple Goal should be simplicity Design patterns are not a magic bullet No plug and play Know when to apply a design pattern Ensure that a pattern fits the design Consider patterns during refactoring Goal is to improve structure, not behavior Don t be afraid to remove a design pattern Especially if design has become too complex 9

Design Pattern Categories Creational Abstracts the instantiation process Dynamically create objects so that they don t have to be instantiated directly Structural Composes groups of objects into larger structures Behavioral Defines communication among objects in a given system Provides better control of flow in a complex application 10

Creational Patterns Abstract Factory Provides an interface for creating related objects without specifying their concrete classes Builder Reuses the construction process of a complex object Factory Method Lets subclasses decide which class to instantiate from a defined interface Prototype Creates new objects by copying a prototype 11

Creational Patterns (continued) Singleton Ensures a class has only one instance with a global point of access to it 12

Structural Patterns Adapter Converts the interface of one class to an interface of another Bridge Decouples an abstraction from its implementation Composite Composes objects into tree structures to represent hierarchies Decorator Attaches responsibilities to an object dynamically 13

Structural Patterns (continued) Façade Provides a unified interface to a set of interfaces Flyweight Supports large numbers of fine-grained objects by sharing Proxy Provides a surrogate for another object to control access to it 14

Behavioral Patterns Chain of Responsibility Passes a request along a chain of objects until the appropriate one handles it Command Encapsulates a request as an object Interpreter Defines a representation and an interpreter for a language grammar Iterator Provides a way to access elements of an object sequentially without exposing its implementation 15

Behavioral Patterns (continued) Mediator Defines an object that encapsulates how a set of objects interact Memento Captures an object s internal state so that it can be later restored to that state if necessary Observer Defines a one-to-many dependency among objects State Allows an object to alter its behavior when its internal state changes 16

Behavioral Patterns (continued) Strategy Encapsulates a set of algorithms individually and makes them interchangeable Template Method Lets subclasses redefine certain steps of an algortithm Visitor Defines a new operation without changing the classes on which it operates 17

Weather Monitoring Station Application Objective: Design an internet-based Weather Monitoring Station application that pulls weather-related data from a weather station and displays it onto a device Temperature, humidity, and barometric pressure data is sent to the weather station 18

Weather Monitoring Station Application Temperature Sensor pulls data displays Humidity Sensor Weather Station WeatherData Object Display Device Pressure Sensor 19

WeatherData Object WeatherData temperature humidity pressure gettemperature gethumidity getpressure measurementschanged 20

public class WeatherData { // instance variables... IS THIS A GOOD APPROACH? public void measurementschanged() { float temperature = gettemperature(); } float humidity = gethumidity(); Area we expect to change float pressure = getpressure(); currentconditions.update(temperature,humidity,pressure); statisticsdisplay.update(temperature,humidity,pressure); forecastdisplay.update(temperature,humidity,pressure); } // other methods here... Use of a common interface Coding to concrete implementations 21

Observer Intent Defines a one-to-many dependency among objects so that when one object changes state, all its dependents are notified and updated automatically A way of notifying change to a number of classes Also known as Dependents Publish-Subscribe Motivation To avoid making classes tightly coupled that would reduce their reusability 22

Observer (continued) Design Principle Strive for loosely coupled designs among objects that interact Use this pattern when: A change to one object requires changing others, and the number of objects to be changed is unknown An object should be able to notify other objects without making assumptions about who these objects are + Avoids having these objects tightly coupled 23

Observer (continued) 24

Publisher + Subscriber = Observer CurrentConditions (Observer) 82, 70%, 29.2 Hg 82, 70%, 29.2 Hg WeatherData (Subject) 82, 70%, 29.2 Hg StatisticsDisplay (Observer) HeatIndexDisplay (Is this an observer?) ForecastDisplay (Observer) 25

Weather Monitoring Station Application <<interface>> Subject registerobserver removeobserver notifyobservers <<interface>> Observer update <<interface>> DisplayElement display WeatherData CurrentConditions StatisticsDisplay ForecastDisplay registerobserver removeobserver notifyobservers gettemperature gethumidity getpressure measurementschanged update display update display update display 26

And Now for the code review and demonstration! 27

Resources Design Patterns Elements of Reusable Object- Oriented Software Erich Gamma, et. al ISBN 0-201-63361-2 Head First Design Patterns Eric & Elisabeth Freeman (with Kathy Sierra & Bert Bates) ISBN 0-596-00712-4 http://www.wickedlysmart.com/ Java Design Patterns James W. Cooper ISBN 0-201-48539-7 http://www.patterndepot.com/put/8/javapatterns.ht m 28

Resources (continued) UML Distilled Martin Fowler (with Kendall Scott) ISBN 0-201-32563-2 Data & Object Factory http://www.dofactory.com/ 29