Pattern Resources. Lecture 25: Design Patterns. What are Patterns? Design Patterns. Pattern Languages of Programming. The Portland Pattern Repository

Similar documents
Lecture 13: Design Patterns

Goals of Lecture. Lecture 27: OO Design Patterns. Pattern Resources. Design Patterns. Cover OO Design Patterns. Pattern Languages of Programming

More on Design. CSCI 5828: Foundations of Software Engineering Lecture 23 Kenneth M. Anderson

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

Design Patterns. An introduction

Second Midterm Review

Facade and Adapter. Comp-303 : Programming Techniques Lecture 19. Alexandre Denault Computer Science McGill University Winter 2004

Factory Method Pattern Creational. » Define an interface for creating an object but lets subclasses decide the specific class to instantiate

Design Patterns. Definition of a Design Pattern

An Introduction to Patterns

Object-Oriented Design

Design Patterns. Gunnar Gotshalks A4-1

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

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

ADAPTER. Topics. Presented By: Mallampati Bhava Chaitanya

Laboratorio di Progettazione di Sistemi Software Design Pattern Creazionali. Valentina Presutti (A-L) Riccardo Solmi (M-Z)

4.1 Introduction Programming preliminaries Constructors Destructors An example... 3

CPSC 310 Software Engineering. Lecture 11. Design Patterns

DESIGN PATTERN MATCHING

Topics in Object-Oriented Design Patterns

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

SOFTWARE PATTERNS. Joseph Bonello

An Introduction to Patterns

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

Crash course on design patterns

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

The GoF Design Patterns Reference

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

Using Design Patterns in Java Application Development

Reuse at Design Level: Design Patterns

Design Patterns. CSC207 Fall 2017

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

Tuesday, October 4. Announcements

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

Lectures 24 and 25 Introduction to Architectural Styles and Design Patterns

Design Patterns. CSC207 Fall 2017

Creational Patterns. Factory Method (FM) Abstract Factory (AF) Singleton (SI) Prototype (PR) Builder (BU)

Design of Software Systems (Ontwerp van SoftwareSystemen) Design Patterns Reference. Roel Wuyts

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

Design Patterns. CSC207 Winter 2017

11/12/12. Objectives DESIGN PATTERNS. Design Pattern. Defined Design Patterns. Applying Design Patterns. Motivating Example

Lecture Material. Design Patterns. Visitor Client-Server Factory Singleton

2.1 Design Patterns and Architecture (continued)

The Singleton Pattern. Design Patterns In Java Bob Tarr

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

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

2.1 Design Patterns and Architecture (continued)

Chapter 12 (revised by JAS)

Programmazione. Prof. Marco Bertini

What is Design Patterns?

CHAPTER 6: CREATIONAL DESIGN PATTERNS

Object-Oriented Oriented Programming Adapter Pattern. CSIE Department, NTUT Woei-Kae Chen

Adapter Pattern Structural

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

James Newkirk

CHAPTER 6: CREATIONAL DESIGN PATTERNS

Introduction and History

Design patterns. OOD Lecture 6

Object-Oriented Oriented Programming Factory Method Pattern Abstract Factory Pattern. CSIE Department, NTUT Woei-Kae Chen

Coordination Patterns

Cocoa Design Patterns. Erik M. Buck October 17, 2009

The Singleton Pattern. Design Patterns In Java Bob Tarr

L23.1 Introduction... 2

Introduction to Design Patterns

Object-Oriented Oriented Programming Adapter Pattern

25.1 Introduction Façade Design Pattern Identification Problem Structure Participants...

INTERNAL ASSESSMENT TEST III Answer Schema

DESIGN PATTERN - INTERVIEW QUESTIONS

Design Patterns. GoF design patterns catalog

Design Pattern- Creational pattern 2015

Software Engineering I (02161)

Prototype Description. Interpreter. interpreter Calculator Design Rationales. Prototype Participants. Interpreter with Factory Method.

Dr. Xiaolin Hu. Review of last class

Outline. Design Patterns. Observer Pattern. Definitions & Classifications

3 Product Management Anti-Patterns by Thomas Schranz

Last Lecture. Lecture 26: Design Patterns (part 2) State. Goals of Lecture. Design Patterns

Introduction to Design Patterns

EMBEDDED SYSTEMS PROGRAMMING Design Patterns

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

What is a Pattern? Lecture 40: Design Patterns. Elements of Design Patterns. What are design patterns?

Tecniche di Progettazione: Design Patterns

Advanced Object Oriented PHP

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

Inheritance. EEC 521: Software Engineering. Dealing with Change. Polymorphism. Software Design. Changing requirements Code needs to be flexible

A reusable design concept

GoF Design Pattern Categories

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

Design Pattern Detection

What is Design Patterns?

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

Factory Method. Comp435 Object-Oriented Design. Factory Method. Factory Method. Factory Method. Factory Method. Computer Science PSU HBG.

C++ INTERFACE CLASSES STRENGTHENING ENCAPSULATION

CS251 Software Engineering Lectures 18: Intro to DP

Introduction To Design Patterns

EMBEDDED SYSTEMS PROGRAMMING Design Patterns

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

Software Engineering Prof. Rushikesh K.Joshi IIT Bombay Lecture-15 Design Patterns

CSC7203 : Advanced Object Oriented Development. J Paul Gibson, D311. Design Patterns

In this Lecture you will Learn: Design Patterns. Patterns vs. Frameworks. Patterns vs. Frameworks

Design Pattern Detection

Transcription:

Pattern Resources Lecture 25: Design Patterns Kenneth M. Anderson Object-Oriented Analysis and Design CSCI 6448 - Spring Semester, 2003 Pattern Languages of Programming Technical conference on Patterns The Portland Pattern Repository http://c2.com/ppr/ Patterns Homepage http://hillside.net/ Go to page then click on Patterns tab April 8, 2003 University of Colorado, 2003 2 Design Patterns Addison-Wesley book published in 1995 Erich Gamma Richard Helm Ralph Johnson John Vlissides ISBN 0-201-63361-2 Known as The Gang of Four Presents 23 Design Patterns Material in this lecture and lecture 26 is drawn from this book, and is thus copyright 1995 by Addison- Wesley Publishing Company What are Patterns? Christopher Alexander talking about buildings and towns Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice Alexander, et al., A Pattern Language. Oxford University Press, 1977 April 8, 2003 University of Colorado, 2003 3 April 8, 2003 University of Colorado, 2003 4

Patterns, continued Patterns can have different levels of abstraction In Design Patterns (the book), Patterns are not classes Patterns are not frameworks Instead, Patterns are descriptions of communicating objects and classes that are customized to solve a general design problem in a particular context Patterns, continued So, patterns are formalized solutions to design problems They describe techniques for maximizing flexibility, extensibility, abstraction, etc. These solutions can typically be translated to code in a straightforward manner April 8, 2003 University of Colorado, 2003 5 April 8, 2003 University of Colorado, 2003 6 Elements of a Pattern Pattern Name More than just a handle for referring to the pattern Each name adds to a designer s vocabulary Enables the discussion of design at a higher abstraction The Problem Gives a detailed description of the problem addressed by the pattern Describes when to apply a pattern Often with a list of preconditions Elements of a Pattern, continued The Solution Describes the elements that make up the design, their relationships, responsibilities, and collaborations Does not describe a concrete solution Instead a template to be applied in many situations April 8, 2003 University of Colorado, 2003 7 April 8, 2003 University of Colorado, 2003 8

Elements of a Pattern, continued Design Pattern Template The consequences Describes the results and tradeoffs of applying the pattern Critical for evaluating design alternatives Typically include Impact on flexibility, extensibility, or portability Space and Time tradeoffs Language and Implementation issues Pattern Name and Classification Creational Structural Behavioral Intent Also Known As Motivation Applicability Structure Participants Collaborations Consequences Implementation Sample Code Known Uses Related Patterns April 8, 2003 University of Colorado, 2003 9 April 8, 2003 University of Colorado, 2003 10 Examples Singleton Factory Method Adapter Singleton Intent Ensure a class has only one instance, and provide a global point of access to it Motivation Some classes represent objects where multiple instances do not make sense or can lead to a security risk (e.g. Java security managers) April 8, 2003 University of Colorado, 2003 11 April 8, 2003 University of Colorado, 2003 12

Singleton, continued Singleton Structure Applicability Use the Singleton pattern when there must be exactly one instance of a class, and it must be accessible to clients from a wellknown access point when the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code Singleton static Instance() public SingletonOperation() public GetSingletonData() private static uniqueinstance private singletondata {return uniqueinstance April 8, 2003 University of Colorado, 2003 13 April 8, 2003 University of Colorado, 2003 14 Singleton, continued Participants Just the Singleton class Collaborations Clients access a Singleton instance solely through Singleton s Instance operation Consequences Controlled access to sole instance Reduced name space (versus global variables) Permits a variable number of instances (if desired) Implementation import java.util.date; public class Singleton { private static Singleton theonlyone; private Date d = new Date(); private Singleton() { public static Singleton instance() { if (theonlyone == null) { theonlyone = new Singleton(); return theonlyone; public Date getdate() { return d; April 8, 2003 University of Colorado, 2003 15 April 8, 2003 University of Colorado, 2003 16

Using our Singleton Class public class usesingleton { public static void main(string[] args) { Singleton a = Singleton.instance(); Singleton b = Singleton.instance(); System.out.println("" + a.getdate()); System.out.println("" + b.getdate()); System.out.println("" + a); System.out.println("" + b); Output: Sun Apr 07 13:03:34 MDT 2002 Sun Apr 07 13:03:34 MDT 2002 Singleton@136646 Singleton@136646 April 8, 2003 University of Colorado, 2003 17 Names of Classes in Patterns Are the class names specified in a pattern required? No! Consider an environment where a system has access to only one printer Would you want to name the class that provides access to the printer Singleton??!! No, you would want to name it something like Printer! On the other hand Incorporating the name of the classes of the pattern can help to communicate their use to designers Oh, I see you have a PrinterObserver class, are you using the Observable design pattern? April 8, 2003 University of Colorado, 2003 18 Names, continued So, if names are unimportant, what is? Structure! We can name our Singleton class anything so long as it has a private or protected constructor need a protected constructor to allow subclasses has a static instance operation to retrieve the single instance Factory Method Intent Define an interface for creating an object, but let subclasses decide which class to instantiate Also Known As Virtual Constructor Motivation Frameworks define abstract classes, but any particular domain needs to use specific subclasses; how can the framework create these subclasses? See example on page 107 of the design patterns book April 8, 2003 University of Colorado, 2003 19 April 8, 2003 University of Colorado, 2003 20

Factory Method, continued Applicability Use the Factory Method pattern when a class can t anticipate the class of objects it must create a class wants its subclasses to specify the objects it creates classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate In a nutshell A factory object creates products for a client; the type of products created depends on the subclass of the factory object used; the client knows only about the factory, not its subclasses April 8, 2003 University of Colorado, 2003 21 Factory Method, continued Participants Product Defines the interface of objects the factory method creates Concrete Product Implements the Product Interface Creator declares the Factory method which returns an object of type Product Concrete Creator overrides the factory method to return an instance of a Concrete Product April 8, 2003 University of Colorado, 2003 22 Factory Method Structure Factory Method Consequences Product ConcreteProduct «instantiate» Creator FactoryMethod() AnOperation() ConcreteCreator FactoryMethod() return new ConcreteProduct() product = FactoryMethod() Factory methods eliminate the need to bind application-specific classes into your code Potential disadvantage is that clients must use subclassing in order to create a particular ConcreteProduct In single-inherited systems, this constrains your partitioning choices Provides hooks for subclasses Connects parallel class hierarchies See page 110 of the design patterns book April 8, 2003 University of Colorado, 2003 23 April 8, 2003 University of Colorado, 2003 24

Implementation See code example (available on class website) A factory can return balloons of different colors The factory hides several specific creators and cycles among them to create balloons A client retrieves multiple balloons and displays their colors Adapter Intent Convert the interface of a class into another interface clients expect. Adapter lets classes work together that could not otherwise because of incompatible interfaces Also Known As Wrapper Motivation Sometimes a toolkit class that is designed for reuse is not reusable because its interface does not match the domainspecific interface an application requires Page 139-140 of Design Patterns provides an example April 8, 2003 University of Colorado, 2003 25 April 8, 2003 University of Colorado, 2003 26 Adapter, continued Applicability Use the Adapter pattern when you want to use an existing class, and its interface does not match the one you need you want to create a reusable class that cooperates with unrelated or unforeseen classes Adapter, continued Participants Target defines the domain-specific interface that Client uses Client collaborates with objects conforming to the Target interface Adaptee defines an existing interface that needs adapting Adapter adapts the interface of Adaptee to the Target interface April 8, 2003 University of Colorado, 2003 27 April 8, 2003 University of Colorado, 2003 28

Adapter Structure Adapter Structure Class Adapter Client Target Request() Adaptee SpecificRequest() Object Adapter Client Target Request() Adapter Request() SpecificRequest() Adapter Request() Adaptee SpecificRequest() adaptee adaptee.specificrequest() April 8, 2003 University of Colorado, 2003 29 April 8, 2003 University of Colorado, 2003 30 Adapter, continued Collaborations Clients call operations on an Adapter instance. In turn, the adapter calls Adaptee operations that carry out the request Consequences Class Adapters adapts Adaptee to Target by committing to concrete Adapter class; Adapter can override Adaptee behavior Object Adapters lets a single Adapter work with many Adaptees; makes it harder to override Adaptee behavior Implementation See code example (available on class website) Very simple implementation of the object adapter but it shows the basic idea object adapter chosen simply because I don t like multiple inheritance :-) April 8, 2003 University of Colorado, 2003 31 April 8, 2003 University of Colorado, 2003 32