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

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

Object-Oriented Design

Using Design Patterns in Java Application Development

SDC Design patterns GoF

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

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

COSC 3351 Software Design. Design Patterns Behavioral Patterns (I)

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

Dr. Xiaolin Hu. Review of last class

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

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

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

Applying the Observer Design Pattern

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

CS251 Software Engineering Lectures 18: Intro to DP

Refining the Observer Pattern: The Middle Observer Pattern

Patterns. Erich Gamma Richard Helm Ralph Johnson John Vlissides

Topics in Object-Oriented Design Patterns

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

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

Introduction and History

CSCI 253. Overview. The Elements of a Design Pattern. George Blankenship 1. Object Oriented Design: Iterator Pattern George Blankenship

Introduction to Software Engineering: Object Design I Reuse & Patterns

CS/CE 2336 Computer Science II

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

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

Design patterns. Valentina Presutti courtesy of Paolo Ciancarini

An Introduction to Patterns

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

Software Engineering - I An Introduction to Software Construction Techniques for Industrial Strength Software

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

Object-Oriented Design

3 Product Management Anti-Patterns by Thomas Schranz

Object-Oriented Oriented Programming

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

Last Lecture. Lecture 17: Design Patterns (part 2) Kenneth M. Anderson Object-Oriented Analysis and Design CSCI 4448/ Spring Semester, 2005

Lectures 24 and 25 Introduction to Architectural Styles and Design Patterns

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

Information systems modelling UML and service description languages

CS 247: Software Engineering Principles. Design Patterns

Design Patterns. Gunnar Gotshalks A4-1

DESIGN PATTERN - INTERVIEW QUESTIONS

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

UNIT I Introduction to Design Patterns

Design Patterns 2. Page 1. Software Requirements and Design CITS 4401 Lecture 10. Proxy Pattern: Motivation. Proxy Pattern.

1 Software Architecture

Brief Note on Design Pattern

POO. Observer Pattern (prezentare bazata pe GoF)

CPSC 310 Software Engineering. Lecture 11. Design Patterns

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

WS01/02 - Design Pattern and Software Architecture

Coordination Patterns

Design pa*erns. Based on slides by Glenn D. Blank

Design Patterns. An introduction

Application Architectures, Design Patterns

Design Patterns Reid Holmes

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

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

Applying the Decorator Design Pattern

Object-Oriented Oriented Programming

UNIT I Introduction to Design Patterns

CSCI 253. Overview. The Elements of a Design Pattern. George Blankenship 1. Object Oriented Design: Template Method Pattern. George Blankenship

Egon Borger (Pisa) Capturing Design Pattern Abstractions by ASMs

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

An Introduction to Patterns

2.1 Design Patterns and Architecture (continued)

2.1 Design Patterns and Architecture (continued)

COSC 3351 Software Design. Design Patterns Behavioral Patterns (II)

Foundations of Software Engineering Design Patterns -- Introduction

What is Design Patterns?

Design Patterns. CSE870: Advanced Software Engineering (Design Patterns): Cheng

Object-oriented Software Design Patterns

Design Patterns! Acknowledgements!

Tuesday, October 4. Announcements

EMBEDDED SYSTEMS PROGRAMMING Design Patterns

Why a Design Pattern. History of Design Pattern. Properties

The GoF Design Patterns Reference

Model-based Software Engineering (02341, spring 2016) Ekkart Kindler

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

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

A Metric of the Relative Abstraction Level of Software Patterns

Design patterns. OOD Lecture 6

EMBEDDED SYSTEMS PROGRAMMING Design Patterns

A Metric for Measuring the Abstraction Level of Design Patterns

CSE870: Advanced Software Engineering (Cheng) 1

Applying the Factory Method Design Pattern

Traditional Design Patterns -- I

Object Oriented Paradigm

CSC207H: Software Design Lecture 6

Introduction to Design Patterns

Design Patterns. GoF design patterns catalog

Java + Patterns = Object Engineering?

Summary of the course lectures

Lecture 19: Introduction to Design Patterns

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

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

Design Patterns. SE3A04 Tutorial. Jason Jaskolka

Design Pattern and Software Architecture: IV. Design Pattern

Overview of Patterns: Introduction

Transcription:

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

Classification of GoF Design Pattern Creational Structural Behavioural Factory Method Adapter Interpreter Abstract Factory Bridge Template Method E. Gamma, R. Helm, R. Johnson, J. Vlissides and Addison-Wesley Builder Prototype Singleton Composite Decorator Flyweight Facade Proxy Chain of Responsibility Command Iterator Mediator Memento Observer State Strategy Visitor

Observer Pattern I Name: Observer (Behavioural) Problem: A side-effect of partitioning a system into cooperating classes is the need to maintain consistency between related objects. You don t want to make the classes too tightly coupled. Intent: Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically Context: Many graphical user interfaces separate the presentational aspects of the user interface from underlying application data. When the user changes information in the application the user objects need to be informed. Example: In an auction, each bidder possesses a numbered paddle that is used to indicate a bid. The auctioneer starts the bidding, and observes when a paddle is raised to accept the bid. Bid acceptance changes the bid price, which is broadcast to all of the bidders in the form of a new bid.

Observer Pattern II Solution: Any number of observers observe a Subject. Observer class defines an updating interface for objects that should be notified of changes in a subject. Resulting context: Coupling between Subject and Observer Broadcast communication. Consequences: Modularity: subject and observers may vary independently Extensibility: can define and add any number of observers Customizability: different observers provide different views of subject Implementation Subject-observer mapping Registering modifications of interest explicitly

Observer Pattern III Also Known As: Publish-subscribe, Dependents Known Uses: CORBA event notification; Cache coherency algorithms; Model-View separation in MVC; ET++, etc.; Interviews (subjects and views); Andrew file system (data objects and views) Related Pattern: Mediator (A ChangeManager encapsulates complex update semantics between subjects and objects.) Ref: Design Patterns, E. Gamma et al., Addison-Wesley, 1994, pp 293-303.

Observer Class Diagram Structure Subject -observers Observer E. Gamma, R. Helm, R. Johnson, J. Vlissides and Addison-Wesley +attach(in o : Observer) +detach(in o : Observer) +notify() ConcreteSubject -subjectstate +getstate() 1 {for all o in observers { o.update() -subject 1 * {return subjectstate * +update() ConcreteObserver -observerstate +update() {observerstate = subject.getstate()

Schematic Observer Example Observers Subject

Observer - C++ Sample Code class Subject { public: virtual ~Subject(); virtual void Attach(Observer*); virtual void Detach(Observer*); virtual void Notify(); protected: Subject(); private: List<Observer*> *_observers; ; void Subject::Attach (Observer* o) { _observers->insert(_observers->end(), o); void Subject::Detach (Observer* o) { _observers->remove(o); void Subject::Notify () { ListIterator<Observer*>i(_observers); for (i.first();!i.isdone(); i.next()) { i.currentitem()->update(this); class Observer { public: virtual ~Observer(); virtual void Update(Subject* thechangesubject) = 0; protected: Observer(); ; class ClockTimer : public Subject { public: ClockTimer(); virtual int GetHour(); virtual int GetMinute(); virtual int GetSecond(); void Tick(); ; void ClockTimer::Tick() { // update internal time-keeping state //... Notify();

Observer C++ Sample Code class DigitalClock: public Observer { public: DigitalClock(ClockTimer *); ~DigitalClock(); void Update(Subject *); void Draw(); private: ClockTimer *_subject; ; DigitalClock::DigitalClock (ClockTimer *s) { _subject = s; _subject->attach(this); DigitalClock::~DigitalClock () { _subject->detach(this); void DigitalClock::Update (Subject *thechangedsubject) { if(thechangedsubject == _subject) draw(); void DigitalClock::Draw () { int hour = _subject->gethour(); int minute = _subject->getminute(); int second = _subject->getsecond(); // draw operation class AnalogClock: public Observer { public: AnalogClock(ClockTimer *); ~AnalogClock(); void Update(Subject *); void Draw(); private: ClockTimer *_subject; ; int main(void) { ClockTimer *timer = new ClockTimer; AnalogClock *analogclock = new AnalogClock(timer); DigitalClock *digitalclock = new DigitalClock(timer); timer->tick(); return 0;

Strategy (Behavioural) Intent: Define a family of algorithms, encapsulate each one, and make them interchangeable to let clients and algorithms vary independently E. Gamma, R. Helm, R. Johnson, J. Vlissides and Addison-Wesley Applicability: When an object should be configurable with one of several algorithms, and all algorithms can be encapsulated, and one interface covers all encapsulations

Strategy II E. Gamma, R. Helm, R. Johnson, J. Vlissides and Addison-Wesley Consequences: Greater flexibility, reuse Can change algorithms dynamically Strategy creation & communication overhead Inflexible strategy interface Implementation: Exchanging information between a strategy and its context Static strategy selection via templates Also Known As: Policy Known uses: Interviews text formatting IC design - RTL register allocation & scheduling strategies ET++ SwapsManager calculation engines

Strategy - Structure E. Gamma, R. Helm, R. Johnson, J. Vlissides and Addison-Wesley Context (Composition) +contextinterface() 1 1 ConcreteStrategyA +algorithminterface() Strategy (Compositor) +algorithminterface() ConcreteStrategyB +algorithminterface() ConcreteStrategyC +algorithminterface()

Strategy - Example

Iterator (Behavioural) Intent: Provide a way to access the elements of a collection sequentially Applicability: To access the contents of a collection without exposing its internal representation To support multiple traversals of collection To provide a uniform interface for traversing different collections (i.e. to support polymorphic iteration) Also Known As: Cursor Know Uses: Widespread in access to data structures; Directly supported in languages such as Java and C++ STL

AbstractCollection iterator() Iterator hasnext() next() ConcreteCollection iterator() Creates ConcreteIterator hasnext() next()

List Example

Template Method (Behavioural) Intent: Define the skeleton of an algorithm in a method, deferring some steps to subclasses, thus allowing the subclasses to redefine certain steps of the algorithm. Applicability: To implement the invariant parts of an algorithm once and leave it up to the subclasses to implement the behaviour that can vary. To factorize and localize the common behaviour among subclasses to avoid code duplication.

Template Method Generic class implements a template method that defines the skeleton of the algorithm the template method calls the hook methods hook methods are abstract methods Concrete class implements the hook methods to carry out the subclass specific parts of algorithm

Template Method Diagram ref: Design pattern in simple examples, Shabbir, 2007.

Template Method Example Document Processing Example

Template Method example - Java abstract public class TemplateMethod { public TemplateMethod() { // implementation of processmessage is left to the subclasses. public abstract Object processmessage (Object message);

Patterns at Different Levels Applicable in most stages of the OO lifecycle Analysis, design, implementation, reviews, documentation, reuse, and refactoring Analysis patterns Typical solutions to recuring anlysis problems See Analysis Patterns, Fowler; Addison-Wesley, 1996 Architectural patterns See POSA for example (Pattern-Oriented Software Architecture, Buschmann, et al.; Wiley, 1996) Design patterns GoF design patterns (Design Patterns, Gamma, et al.; Addison-Wesley, 1995) Programming Idioms Smalltalk Best Practice Patterns, Beck; Prentice Hall, 1997 Concurrent Programming in Java, Lea; Addison-Wesley, 1997 Advanced C++, Coplien, Addison-Wesley, 1992 Effective C++: 50 Specific Ways to Improve Your Programs and Design (2nd Edition), Scott Meyers, Addison-Wesley, 1997 More Effective C++: 35 New Ways to Improve Your Programs and Designs, Scott Meyers, Addison-Wesley, 1995