Docsity

Understanding and Applying Design Patterns: Creational, Structural, and Behavioral - Prof., Study notes of Computer Science

Toggle sidebar
bg1
Lecture 25: Design Patterns
Kenneth M. Anderson
Object-Oriented Analysis and Design
CSCI 6448 - Spring Semester, 2003
April 8, 2003 © University of Colorado, 2003 2
Pattern Resources
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 3
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
April 8, 2003 © University of Colorado, 2003 4
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
pf3
pf4
pf5
pf8

Partial preview of the text

Download Understanding and Applying Design Patterns: Creational, Structural, and Behavioral - Prof. and more Study notes Computer Science in PDF only on Docsity!

Lecture 25: Design Patterns

Kenneth M. Anderson

Object-Oriented Analysis and DesignCSCI 6448 - Spring Semester, 2003

April 8, 2003

© University of Colorado, 2003

Pattern Resources^ 

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

Design Patterns^ ^

Addison-Wesley book published in 1995^ 

Erich Gamma  Richard Helm  Ralph Johnson  John Vlissides  ISBN 0-201-63361- ^

Known as “The Gang of Four”

^

Presents 23 Design Patterns

^

Material in this lecture and lecture 26 is drawn fromthis book, and is thus copyright © 1995 by Addison-Wesley Publishing Company

April 8, 2003

© University of Colorado, 2003

What are Patterns?^ ^

Christopher Alexander talking about buildings andtowns^ 

“Each pattern describes a problem which occurs over andover again in our environment, and then describes the coreof the solution to that problem, in such a way that you canuse this solution a million times over, without ever doing itthe same way twice”  Alexander, et al., A Pattern Language. Oxford UniversityPress, 1977

© University of Colorado, 2003

Patterns, continued^ 

Patterns can have different levels ofabstraction  In Design Patterns (the book),

^

Patterns are not classes

^

Patterns are not frameworks

^

Instead, Patterns are descriptions ofcommunicating objects and classes that arecustomized to solve a general design problem in aparticular context

April 8, 2003

© University of Colorado, 2003

Patterns, continued^ 

So, patterns are formalized solutions todesign problems

^

They describe techniques for maximizingflexibility, extensibility, abstraction, etc.

^

These solutions can typically betranslated to code in a straightforwardmanner

© University of Colorado, 2003

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 thepattern  Describes when to apply a pattern

^

Often with a list of preconditions

April 8, 2003

© University of Colorado, 2003

Elements of a Pattern, continued^ 

The Solution

^

Describes the elements that make up thedesign, their relationships, responsibilities,and collaborations

^

Does not describe a concrete solution^ 

Instead a template to be applied in manysituations

© University of Colorado, 2003

Singleton, continued^ 

Applicability

^

Use the Singleton pattern when^ 

there must be exactly one instance of a class,and it must be accessible to clients from a well-known access point  when the sole instance should be extensible bysubclassing, and clients should be able to usean extended instance without modifying theircode

April 8, 2003

© University of Colorado, 2003

Singleton Structure

Singletonstatic Instance()

{return uniqueInstance}

public SingletonOperation()public GetSingletonData()private static uniqueInstanceprivate singletonData

© University of Colorado, 2003

Singleton, continued^ 

Participants

^

Just the Singleton class

^

Collaborations^ 

Clients access a Singleton instance solely throughSingleton’s Instance operation

^

Consequences^ 

Controlled access to sole instance  Reduced name space (versus global variables)  Permits a variable number of instances (if desired)

April 8, 2003

© University of Colorado, 2003

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; } }

© University of Colorado, 2003

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 2002Sun Apr 07 13:03:34 MDT 2002Singleton@136646Singleton@

April 8, 2003

© University of Colorado, 2003

Names of Classes in Patterns^ 

Are the class names specified in a patternrequired?

^

No!^ 

Consider an environment where a system has access toonly one printer  Would you want to name the class that provides accessto 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 canhelp to communicate their use to designers

^

“Oh, I see you have a “PrinterObserver” class, are youusing the Observable design pattern?

© University of Colorado, 2003

Names, continued^ 

So, if names are unimportant, what is?

^

Structure!

^

We can name our Singleton classanything so long as it^ 

has a private or protected constructor

^

need a protected constructor to allowsubclasses

^

has a static “instance” operation to retrievethe single instance

April 8, 2003

© University of Colorado, 2003

Factory Method^ 

Intent

^

Define an interface for creating an object, but letsubclasses decide which class to instantiate

^

Also Known As^ 

Virtual Constructor

^

Motivation^ 

Frameworks define abstract classes, but any particulardomain needs to use specific subclasses; how can theframework create these subclasses?

^

See example on page 107 of the design patterns book

© University of Colorado, 2003

Implementation^ 

See code example (available on classwebsite)  A factory can return balloons of differentcolors

^

The factory hides several specific creatorsand cycles among them to create balloons

^

A client retrieves multiple balloons anddisplays their colors

April 8, 2003

© University of Colorado, 2003

Adapter^ ^

Intent^ 

Convert the interface of a class into another interface clientsexpect. Adapter lets classes work together that could nototherwise because of incompatible interfaces ^

Also Known As^ 

Wrapper ^

Motivation^ 

Sometimes a toolkit class that is designed for reuse is notreusable because its interface does not match the domain-specific interface an application requires

^

Page 139-140 of Design Patterns provides an example

© University of Colorado, 2003

Adapter, continued^ 

Applicability

^

Use the Adapter pattern when^ 

you want to use an existing class, and itsinterface does not match the one you need  you want to create a reusable class thatcooperates with unrelated or unforeseenclasses

April 8, 2003

© University of Colorado, 2003

Adapter, continued^ 

Participants

^

Target^ 

defines the domain-specific interface that Client uses ^

Client^ 

collaborates with objects conforming to the Targetinterface ^

Adaptee^ 

defines an existing interface that needs adapting ^

Adapter^ 

adapts the interface of Adaptee to the Target interface

© University of Colorado, 2003

Adapter Structure^ Client

Target Request()

Adaptee

SpecificRequest()

AdapterRequest()

Class Adapter

SpecificRequest()

April 8, 2003

© University of Colorado, 2003

Adapter Structure

Client

Target Request() Adapter

Request()

Adaptee

SpecificRequest()

Object Adapter

adaptee.SpecificRequest()

adaptee

© University of Colorado, 2003

Adapter, continued^ 

Collaborations

^

Clients call operations on an Adapter instance. Inturn, the adapter calls Adaptee operations thatcarry out the request

^

Consequences^ 

Class Adapters

^

adapts Adaptee to Target by committing to concreteAdapter class; Adapter can override Adaptee behavior

^

Object Adapters^ 

lets a single Adapter work with many Adaptees; makes itharder to override Adaptee behavior

April 8, 2003

© University of Colorado, 2003

Implementation^ 

See code example (available on classwebsite)  Very simple implementation of theobject adapter but it shows the basicidea

^

object adapter chosen simply because Idon’t like multiple inheritance :-)