Vlissides John - West Virginia Universitycommunity.wvu.edu/~hhammar/rts/adv rts/design...

39

Transcript of Vlissides John - West Virginia Universitycommunity.wvu.edu/~hhammar/rts/adv rts/design...

Page 1: Vlissides John - West Virginia Universitycommunity.wvu.edu/~hhammar/rts/adv rts/design patterns-tutorial.pdfAn Intro duction to Design P atterns John Vlissides IBM T.J. W atson Resea

An Introduction to

Design Patterns

John Vlissides

IBM T.J. Watson Research

[email protected]

Text c 1994-1999 Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides.

Diagrams c 1995 by Addison-Wesley Publishing Company. All rights reserved. Diagrams takenfrom Design Patterns: Elements of Reusable Object-Oriented Software may not be reproduced,stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical,photocopying, recording, or otherwise, without the prior written permission of the publisher.

1

Overview

Part I: Motivation and Concept

� the issue

� what design patterns are

� what they're good for

� how we develop and categorize them

2

Page 2: Vlissides John - West Virginia Universitycommunity.wvu.edu/~hhammar/rts/adv rts/design patterns-tutorial.pdfAn Intro duction to Design P atterns John Vlissides IBM T.J. W atson Resea

Overview (cont'd)

Part II: Application

� use patterns to design a document editor

� demonstrate usage and bene�ts

Part III: Wrap-Up

� observations, caveats, and conclusion

3

Part I: Motivation and Concept

OOD methods emphasize design notations

Fine for speci�cation, documentation

But OOD is more than just drawing diagrams

Good draftsmen 6= good designers

Good OO designers rely on lots of experience

At least as important as syntax

Most powerful reuse is design reuse

Match problem to design experience

4

Page 3: Vlissides John - West Virginia Universitycommunity.wvu.edu/~hhammar/rts/adv rts/design patterns-tutorial.pdfAn Intro duction to Design P atterns John Vlissides IBM T.J. W atson Resea

OO systems exploit recurring design structures that promote

� abstraction

� exibility

� modularity

� elegance

Therein lies valuable design knowledge

Problem: capturing, communicating, and applying this knowledge

5

A Design Pattern

� abstracts a recurring design structure

� comprises class and/or object

{ dependencies

{ structures

{ interactions

{ conventions

� names & speci�es the design structure explicitly

� distills design experience

6

Page 4: Vlissides John - West Virginia Universitycommunity.wvu.edu/~hhammar/rts/adv rts/design patterns-tutorial.pdfAn Intro duction to Design P atterns John Vlissides IBM T.J. W atson Resea

A Design Pattern has 4 basic parts:

1. Name

2. Problem

3. Solution

4. Consequences and trade-o�s of application

Language- and implementation-independent

A \micro-architecture"

Adjunct to existing methodologies (UML/P, Fusion, etc.)

7

Example: Observer

observers

a = 50%b = 30%c = 20%

subject

change notification

requests, modifications

window

a

b

c

window

a b cxyz

30 1050 30 2060

80 10 10

window

a b c

8

Page 5: Vlissides John - West Virginia Universitycommunity.wvu.edu/~hhammar/rts/adv rts/design patterns-tutorial.pdfAn Intro duction to Design P atterns John Vlissides IBM T.J. W atson Resea

Goals

Codify good design

Distill and disseminate experience

Aid to novices and experts alike

Abstract how to think about design

Give design structures explicit names

Common vocabulary

Reduced complexity

Greater expressiveness

Capture and preserve design information

Articulate design decisions succinctly

Improve documentation

Facilitate restructuring/refactoring

Patterns are interrelated

Additional exibility

9

Design Pattern Space

Cla

ssO

bjec

t

Creational Structural Behavioral

Factory Method Adapter (class)

Adapter (object)BridgeCompositeDecoratorFlyweightFacadeProxy

Chain of ResponsibilityCommandIteratorMediatorMementoObserverStateStrategyVisitor

InterpreterTemplate Method

Purpose

Sco

pe

Abstract FactoryBuilderPrototypeSingleton

Scope: domain over which a pattern applies

Purpose: re ects what a pattern does

10

Page 6: Vlissides John - West Virginia Universitycommunity.wvu.edu/~hhammar/rts/adv rts/design patterns-tutorial.pdfAn Intro duction to Design P atterns John Vlissides IBM T.J. W atson Resea

Design Pattern Template (�rst half)

Name scope purpose

Intent

short description of pattern and its purpose

Also Known As

other names that people have for the pattern

Motivation

motivating scenario demonstrating pattern's use

Applicability

circumstances in which pattern applies

Structure

graphical representation of the pattern using modi�ed OMT notation

Participants

participating classes and/or objects and their responsibilities

...

11

Design Pattern Template (second half)

...

Collaborations

how participants cooperate to carry out their responsibilities

Consequences

the results of application, bene�ts, liabilities

Implementation

implementation pitfalls, hints, or techniques, plus any language-dependentissues

Sample Code

sample implementations in C++ or Smalltalk

Known Uses

examples drawn from existing systems

Related Patterns

discussion of other patterns that relate to this one

12

Page 7: Vlissides John - West Virginia Universitycommunity.wvu.edu/~hhammar/rts/adv rts/design patterns-tutorial.pdfAn Intro duction to Design P atterns John Vlissides IBM T.J. W atson Resea

Modi�ed OMT Notation

one

manyAbstractClass

ConcreteSubclass2ConcreteSubclass1

object reference

aggregation

creates

instanceVariable

ConcreteClass

implementationpseudo−code

abstractOperation()

operation()

13

Observer object behavioral

Intent

de�ne a one-to-many dependency between objects so that when one objectchanges state, all its dependents are noti�ed and updated automatically

Applicability

� when an abstraction has two aspects, one dependent on the other

� when a change to one object requires changing others, and you don't knowhow many objects need to be changed

� when an object should notify other objects without making assumptionsabout who these objects are

Structure

subject

observersSubject

ConcreteSubject

ConcreteObserver

Observer

subjectState

observerStatereturn subjectState

attach(Observer)detach(Observer)notify()

update()

update() observerState = subject.getState()

getState()

for all o in observers { o.update()}

14

Page 8: Vlissides John - West Virginia Universitycommunity.wvu.edu/~hhammar/rts/adv rts/design patterns-tutorial.pdfAn Intro duction to Design P atterns John Vlissides IBM T.J. W atson Resea

Observer (cont'd) object behavioral

Consequences

+ modularity: subject and observers may vary independently

+ extensibility: can de�ne and add any number of observers

+ customizability: di�erent observers provide di�erent views of subject

� unexpected updates: observers don't know about each other

� update overhead: might need hints

Implementation

� subject-observer mapping

� dangling references

� avoiding observer-speci�c update protocols: the push and pull models

� registering modi�cations of interest explicitly

Known Uses

Smalltalk Model-View-Controller (MVC)

InterViews (Subjects and Views)

Andrew (Data Objects and Views)

15

Bene�ts

� design reuse

� uniform design vocabulary

� enhance understanding, restructuring

� basis for automation

16

Page 9: Vlissides John - West Virginia Universitycommunity.wvu.edu/~hhammar/rts/adv rts/design patterns-tutorial.pdfAn Intro duction to Design P atterns John Vlissides IBM T.J. W atson Resea

Part II: Application

7 Design Problems:

� document structure

� formatting

� embellishment

� multiple look & feels

� multiple window systems

� user operations

� spelling checking &hyphenation

17

Document Structure

Goals:

� present document's visual aspects

� drawing, hit detection, alignment

� support physical structure (e.g., lines, columns)

Constraints:

� treat text and graphics uniformly

� no distinction between one and many

18

Page 10: Vlissides John - West Virginia Universitycommunity.wvu.edu/~hhammar/rts/adv rts/design patterns-tutorial.pdfAn Intro duction to Design P atterns John Vlissides IBM T.J. W atson Resea

Document Structure (cont'd)

Solution: Recursive composition

space image

G g

characters

composite (column)

composite (row)

19

Document Structure (cont'd)

Object structure

composite

spaceG

composite

g

composite(column)

(row) (row)

20

Page 11: Vlissides John - West Virginia Universitycommunity.wvu.edu/~hhammar/rts/adv rts/design patterns-tutorial.pdfAn Intro duction to Design P atterns John Vlissides IBM T.J. W atson Resea

Document Structure (cont'd)

Glyph: base class for composable graphical objects

Basic interface:

Task Operations

appearance void draw(Window)

hit detection boolean intersects(Coord, Coord)

structure void insert(Glyph)

void remove(Glyph)

Glyph child(int)

Glyph parent()

Subclasses: Character, Image, Space, Row, Column

21

Document Structure (cont'd)

Glyph Hierarchy

Rowchildren

PolygonRectangleCharacter

char c Coord l, bCoord r, t

Columnchildren

Glyph

...

draw(Window)intersects(Coord, Coord)insert(Glyph, int)

draw(...) draw(...) draw(...) draw(...) draw(...)intersects(...)insert(...)

intersects(...) intersects(...) intersects(...) intersects(...)insert(...)

Coord x[]Coord y[]

22

Page 12: Vlissides John - West Virginia Universitycommunity.wvu.edu/~hhammar/rts/adv rts/design patterns-tutorial.pdfAn Intro duction to Design P atterns John Vlissides IBM T.J. W atson Resea

Document Structure (cont'd)

Composite object structural

Intent

treat individual objects and multiple, recursively-composed objects uniformly

Applicability

objects must be composed recursively,

and there should be no distinction between individual and composed elements,

and objects in the structure can be treated uniformly

Structure

childrenCompositeLeaf

Component

operation()add(Component)remove(Component)getChild(int)

operation() operation()add(Component)remove(Component)getChild(int)

forall g in children g.operation();

23

Document Structure (cont'd)

Composite (cont'd) object structural

Consequences

+ uniformity: treat components the same regardless of complexity

+ extensibility: new Component subclasses work wherever old ones do

� overhead: might need prohibitive numbers of objects

Implementation

� do Components know their parents?

� uniform interface for both leaves and composites?

� don't allocate storage for children in Component base class

� responsibility for deleting children

Known Uses

ET++ VObjects

InterViews Glyphs, Styles

Unidraw Components, MacroCommands

24

Page 13: Vlissides John - West Virginia Universitycommunity.wvu.edu/~hhammar/rts/adv rts/design patterns-tutorial.pdfAn Intro duction to Design P atterns John Vlissides IBM T.J. W atson Resea

Questions

What does the pattern let you vary?

Where have you applied this pattern in your designs?

What are the

� objects

� interfaces

� classes

� interactions

etc.?

25

Formatting

Goals:

� automatic linebreaking, justi�cation

Constraints:

� support multiple linebreaking algorithms

� don't mix up with document structure

26

Page 14: Vlissides John - West Virginia Universitycommunity.wvu.edu/~hhammar/rts/adv rts/design patterns-tutorial.pdfAn Intro duction to Design P atterns John Vlissides IBM T.J. W atson Resea

Formatting (cont'd)

Solution: Encapsulate linebreaking strategy

Compositor

� base class abstracts linebreaking algorithm

� subclasses for specialized algorithms, e.g., SimpleCompositor,TeXCompositor

Composition

� composite glyph

� supplied a compositor and leaf glyphs

� creates row-column structure as directed by compositor

27

Formatting (cont'd)

New object structure

spaceG g

composition

compositor

row row

column

glyphsgenerated

composition−

28

Page 15: Vlissides John - West Virginia Universitycommunity.wvu.edu/~hhammar/rts/adv rts/design patterns-tutorial.pdfAn Intro duction to Design P atterns John Vlissides IBM T.J. W atson Resea

Formatting (cont'd)

Strategy object behavioral

Intent

de�ne a family of algorithms, encapsulate each one, and make theminterchangeable to let clients and algorithms vary independently

Applicability

when an object should be con�gurable with one of several algorithms,

and all algorithms can be encapsulated,

and one interface covers all encapsulations

Structure

Strategy

ConcreteStrategyA ConcreteStrategyB ConcreteStrategyC

Context (Compositor)(Composition)

contextInterface() algorithmInterface()

algorithmInterface() algorithmInterface() algorithmInterface()

29

Formatting (cont'd)

Strategy (cont'd) object behavioral

Consequences

+ greater exibility, reuse

+ can change algorithms dynamically

� strategy creation & communication overhead

� in exible Strategy interface

Implementation

� exchanging information between a Strategy and its context

� static strategy selection via templates

Known Uses

InterViews text formatting

RTL register allocation & scheduling strategies

ET++SwapsManager calculation engines

30

Page 16: Vlissides John - West Virginia Universitycommunity.wvu.edu/~hhammar/rts/adv rts/design patterns-tutorial.pdfAn Intro duction to Design P atterns John Vlissides IBM T.J. W atson Resea

Embellishment

Goals:

� add a frame around text composition

� add scrolling capability

Constraints:

� embellishments should be reusable without subclassing

� should go unnoticed by clients

31

Embellishment (cont'd)

Solution: \Transparent" enclosure

MonoGlyph

� base class for glyphs having one child

� operations on MonoGlyph pass through to child

MonoGlyph subclasses:

� Frame: adds a border of speci�ed width

� Scroller: scrolls/clips child, adds scrollbars

32

Page 17: Vlissides John - West Virginia Universitycommunity.wvu.edu/~hhammar/rts/adv rts/design patterns-tutorial.pdfAn Intro duction to Design P atterns John Vlissides IBM T.J. W atson Resea

Embellishment (cont'd)

Glyph

Frame

componentCharacter RowMonoGlyph

Scroller

draw(Window) draw(Window) draw(Window)

draw(Window) draw(Window)

draw(Window)

void MonoGlyph.draw (Window w) {component.draw(w);

}

void Frame.draw (Window w) {super.draw(w);drawFrame(w);

}

33

Embellishment (cont'd)

New object structure

spaceG g

composition

row row

column

frame

scroller

34

Page 18: Vlissides John - West Virginia Universitycommunity.wvu.edu/~hhammar/rts/adv rts/design patterns-tutorial.pdfAn Intro duction to Design P atterns John Vlissides IBM T.J. W atson Resea

Embellishment (cont'd)

Decorator object structural

Intent

augment objects with new responsibilities

Applicability

� when extension by subclassing is impractical

� for responsibilities that can be withdrawn

Structure

componentConcreteComponent Decorator

ConcreteDecoratorA

component−>Operation()

ConcreteDecoratorB

addedState

(MonoGlyph)

Component (Glyph)

operation()

operation() operation()

operation() operation()

component.operation()

addedBehavior()

super.operation()addedBehavior()

35

Embellishment (cont'd)

Decorator (cont'd) object structural

Consequences

+ responsibilities can be added/removed at run-time

+ avoids subclass explosion

+ recursive nesting allows multiple responsibilities

� interface occlusion

� identity crisis

Implementation

� interface conformance

� use a lightweight, abstract base class for Decorator

� heavyweight base classes make Strategy more attractive

Known Uses

embellishment objects from most OO-GUI toolkits

ParcPlace PassivityWrapper

InterViews DebuggingGlyph

36

Page 19: Vlissides John - West Virginia Universitycommunity.wvu.edu/~hhammar/rts/adv rts/design patterns-tutorial.pdfAn Intro duction to Design P atterns John Vlissides IBM T.J. W atson Resea

Multiple Look & Feels

Goals:

� support multiple look and feel standards

� generic, Motif, PM, Macintosh, Windows, ...

� extensible for future standards

Constraints:

� don't recode existing widgets or clients

� switch look and feel without recompiling

37

Multiple Look & Feels (cont'd)

Solution:Abstract the process of creating objects

Instead of

Scrollbar sb = new MotifScrollbar();

use

Scrollbar sb = factory.createScrollbar();

where factory is an instance of MotifFactory

38

Page 20: Vlissides John - West Virginia Universitycommunity.wvu.edu/~hhammar/rts/adv rts/design patterns-tutorial.pdfAn Intro duction to Design P atterns John Vlissides IBM T.J. W atson Resea

Multiple Look & Feels (cont'd)

Factory interface

� de�nes \manufacturing interface"

� subclasses produce speci�c products

� subclass instance chosen at run-time

interface Factory {

Scrollbar createScrollbar();Menu createMenu();...

}

39

Multiple Look & Feels (cont'd)

Factory

MotifFactoryPMFactory

PMScrollbar MotifScrollbar

Glyph

Character

createScrollbar()createMenu()

createScrollbar()createMenu()

createScrollbar()createMenu()

draw(Window) draw(Window)

Scrollbar

draw(Window)

draw(Window)

draw(Window)

Scrollbar MotifFactory.createScrollBar () {return new MotifScrollbar();

}

Scrollbar PMFactory.createScrollBar () {return new PMScrollbar();

}

40

Page 21: Vlissides John - West Virginia Universitycommunity.wvu.edu/~hhammar/rts/adv rts/design patterns-tutorial.pdfAn Intro duction to Design P atterns John Vlissides IBM T.J. W atson Resea

Multiple Look & Feels (cont'd)

Abstract Factory object creational

Intent

create families of related objects without specifying class names

Applicability

when clients cannot anticipate groups of classes to instantiate

Structure

AbstractFactory

ConcreteFactory1 ConcreteFactory2

AbstractProductA

AbstractProductB

Client

ProductA2 ProductA1

ProductB2 ProductB1

createProductA()createProductB()

createProductA()createProductB()

createProductA()createProductB()

41

Multiple Look & Feels (cont'd)

Abstract Factory (cont'd) object creational

Consequences

+ exibility: removes type dependencies from clients

+ abstraction: hides product's composition

� hard to extend factory interface to create new products

Implementation

� parameterization as a way of controlling interface size

� con�guration with Prototypes

Known Uses

InterViews Kits

ET++ WindowSystem

42

Page 22: Vlissides John - West Virginia Universitycommunity.wvu.edu/~hhammar/rts/adv rts/design patterns-tutorial.pdfAn Intro duction to Design P atterns John Vlissides IBM T.J. W atson Resea

Multiple Window Systems

Goals:

� make composition appear in a window

� support multiple window systems

Constraints:

� minimize window system dependencies

43

Multiple Window Systems (cont'd)

Solution: Encapsulate implementation dependencies

Window

� user-level window abstraction

� displays a glyph (structure)

� window system-independent

� task-related subclasses (e.g., IconWindow, PopupWindow)

44

Page 23: Vlissides John - West Virginia Universitycommunity.wvu.edu/~hhammar/rts/adv rts/design patterns-tutorial.pdfAn Intro duction to Design P atterns John Vlissides IBM T.J. W atson Resea

Multiple Window Systems (cont'd)

Window interface

interface Window {...void iconify(); // window-managementvoid raise();...

void drawLine(...); // device-independentvoid drawText(...); // graphics interface...

}

45

Multiple Window Systems (cont'd)

Window uses a WindowRep

� abstract implementation interface

� encapsulates window system dependencies

� window systems-speci�c subclasses(e.g., XWindowRep, SunWindowRep)

An Abstract Factory can produce the right WindowRep!

46

Page 24: Vlissides John - West Virginia Universitycommunity.wvu.edu/~hhammar/rts/adv rts/design patterns-tutorial.pdfAn Intro duction to Design P atterns John Vlissides IBM T.J. W atson Resea

Multiple Window Systems (cont'd)

Window

XWindowRep SunWindowRep

WindowReprep

IconWindow PopupWindow

Character

contents Glyph

drawLine()

drawText()

deviceText()

deviceText()

deviceText()

draw(Window)

draw(Window)

void Character.draw (Window w) {w.drawText(...);

}

void Window.drawText (...) {rep.deviceText(...);

}

void XWindowRep.deviceText (...) {XText(...);

}

47

Multiple Window Systems (cont'd)

New object structure

window

window

window_rep

spaceG g

composition

row row

column

frame

renders

scroller

48

Page 25: Vlissides John - West Virginia Universitycommunity.wvu.edu/~hhammar/rts/adv rts/design patterns-tutorial.pdfAn Intro duction to Design P atterns John Vlissides IBM T.J. W atson Resea

Multiple Window Systems (cont'd)

Bridge object structural

Intent

separate an abstraction from its implementation

Applicability

� when interface and implementation should vary independently

� require a uniform interface to interchangeable class hierarchies

Structure

Abstractionimp

Implementor

RefinedAbstraction

Client

ConcreteImplementorA ConcreteImplementorB

(Window) (WindowRep)

operation() operationImp()

operationImp() operationImp()

imp.operationImp()

49

Multiple Window Systems (cont'd)

Bridge (cont'd) object structural

Consequences

+ abstraction and implementation are independent

+ implementations may vary dynamically

� one-size-�ts-all Abstraction and Implementor interfaces

Implementation

� sharing Implementors

� creating the right implementor

Known Uses

ET++ Window/WindowPort

libg++ Set/fLinkedList,HashTableg

50

Page 26: Vlissides John - West Virginia Universitycommunity.wvu.edu/~hhammar/rts/adv rts/design patterns-tutorial.pdfAn Intro duction to Design P atterns John Vlissides IBM T.J. W atson Resea

User Operations

Goals:

� support execution of user operations

� support unlimited-level undo

Constraints:

� scattered operation implementations

� must store undo state

� not all operations are undoable

51

User Operations (cont'd)

Solution: Encapsulate the request for a service

Command encapsulates

� an operation (execute())

� an inverse operation (unexecute())

� a operation for testing reversibility (boolean reversible())

� state for (un)doing the operation

Command may

� implement the operations itself, or

� delegate them to other object(s)

52

Page 27: Vlissides John - West Virginia Universitycommunity.wvu.edu/~hhammar/rts/adv rts/design patterns-tutorial.pdfAn Intro duction to Design P atterns John Vlissides IBM T.J. W atson Resea

User Operations (cont'd)

MenuItem Commandcommand

CopyCommand PasteCommand ACommand

Glyph

draw(Window)

clicked() execute()

execute() execute() execute()

void MenuItem.clicked () {command.execute();

}

void PasteCommand.execute () {// do the paste

}

void CopyCommand.execute () {// do the copy

}

53

User Operations (cont'd)

List of commands de�nes execution history

Undo:

past future

cmd

unexecute()

Redo:

past future

cmd

execute()

54

Page 28: Vlissides John - West Virginia Universitycommunity.wvu.edu/~hhammar/rts/adv rts/design patterns-tutorial.pdfAn Intro duction to Design P atterns John Vlissides IBM T.J. W atson Resea

User Operations (cont'd)

Command object behavioral

Intent

encapsulate the request for a service

Applicability

� to parameterize objects with an action to perform

� to specify, queue, and execute requests at di�erent times

� for a history of requests

� for multilevel undo/redo

Structure

state

Command

targetConcreteCommand

Target

Client Invoker

execute()

execute()

action()

target.action()

55

User Operations (cont'd)

Command (cont'd) object behavioral

Consequences

+ abstracts executor of a service

+ supports arbitrary-level undo-redo

+ composition yields macro-commands

� might result in lots of trivial command subclasses

Implementation

� copying a command before putting it on a history list

� handling hysteresis

� supporting transactions

Known Uses

InterViews Actions

MacApp, Unidraw Commands

56

Page 29: Vlissides John - West Virginia Universitycommunity.wvu.edu/~hhammar/rts/adv rts/design patterns-tutorial.pdfAn Intro duction to Design P atterns John Vlissides IBM T.J. W atson Resea

Spelling Checking and Hyphenation

Goals:

� analyze text for spelling errors

� introduce potential hyphenation sites

Constraints:

� support multiple algorithms

� don't mix up with document structure

57

Spelling Checking and Hyphenation (cont'd)

Solution: Encapsulate traversal

Iterator

� encapsulates a traversal algorithm

� uses Glyph's child enumeration operation

1

2

iterator

4

3

"a" "_"

58

Page 30: Vlissides John - West Virginia Universitycommunity.wvu.edu/~hhammar/rts/adv rts/design patterns-tutorial.pdfAn Intro duction to Design P atterns John Vlissides IBM T.J. W atson Resea

Spelling Checking and Hyphenation (cont'd)

Iterator object behavioral

Intent

access elements of an aggregate sequentially without exposing itsrepresentation

Applicability

� require multiple traversal algorithms over an aggregate

� require a uniform traversal interface over di�erent aggregates

� when aggregate classes and traversal algorithm must vary independently

Structure

Client Iterator

ConcreteIterator

Aggregate

ConcreteAggregate

return new ConcreteIterator(this)

(Glyph)

createIterator()

createIterator()

next()isDone()currentItem()

first()

59

Spelling Checking and Hyphenation (cont'd)

Iterator (cont'd) object behavioral

Consequences

+ exibility: aggregate and traversal are independent

+ multiple iterators ! multiple traversal algorithms

� additional communication overhead between iterator and aggregate

Implementation

� internal versus external iterators

� violating the object structure's encapsulation

� robust iterators

Known Uses

Penpoint traversal driver/slave

InterViews ListItr

Unidraw Iterator

60

Page 31: Vlissides John - West Virginia Universitycommunity.wvu.edu/~hhammar/rts/adv rts/design patterns-tutorial.pdfAn Intro duction to Design P atterns John Vlissides IBM T.J. W atson Resea

Spelling Checking and Hyphenation (cont'd)

Visitor

� de�nes action(s) at each step of traversal

� avoids wiring action(s) into Glyphs

� iterator calls glyph's accept(Visitor) at each node

� accept calls back on visitor

void Character.accept (Visitor v) { v.visit(this); }

interface Visitor {void visit(Character);void visit(Rectangle);void visit(Row);// etc. for all relevant Glyph subclasses

}

61

Spelling Checking and Hyphenation (cont'd)

SpellingCheckerVisitor

� gets character code from each character glyph

Can de�ne getCharCode operation just on Character class

� checks words accumulated from character glyphs

� combine with PreorderIterator

62

Page 32: Vlissides John - West Virginia Universitycommunity.wvu.edu/~hhammar/rts/adv rts/design patterns-tutorial.pdfAn Intro duction to Design P atterns John Vlissides IBM T.J. W atson Resea

Spelling Checking and Hyphenation (cont'd)

Accumulating Words

1

2

iterator

4

3

"a"

visitor

"_"

"a_"

Spelling check on each non-alphabetic character

63

Spelling Checking and Hyphenation (cont'd)

Interaction Diagram

checkscompletedword

aSpellingCheckeraCharacter ("a") anotherCharacter ("_")

accept(aSpellingChecker)

accept(aSpellingChecker)

visit(this)

getCharCode()

visit(this)

getCharCode()

64

Page 33: Vlissides John - West Virginia Universitycommunity.wvu.edu/~hhammar/rts/adv rts/design patterns-tutorial.pdfAn Intro duction to Design P atterns John Vlissides IBM T.J. W atson Resea

Spelling Checking and Hyphenation (cont'd)

HyphenationVisitor

� gets character code from each character glyph

� examines words accumulated from character glyphs

� at potential hyphenation point, inserts a...

65

Spelling Checking and Hyphenation (cont'd)

Discretionary glyph

� looks like a hyphen when it falls at the end of a line

� has no appearance otherwise

� Compositor considers its presence when determining linebreaks

"a" "l" "l" "o" "y"discretionary

aluminum alloy

loy

aluminum al−or

66

Page 34: Vlissides John - West Virginia Universitycommunity.wvu.edu/~hhammar/rts/adv rts/design patterns-tutorial.pdfAn Intro duction to Design P atterns John Vlissides IBM T.J. W atson Resea

Spelling Checking and Hyphenation (cont'd)

Visitor object behavioral

Intent

centralize operations on an object structure so that they can varyindependently but still behave polymorphically

Applicability

� when classes de�ne many unrelated operations

� class relationships of objects in the structure rarely change, but theoperations on them change often

� algorithms over the structure maintain state that's updated during traversal

Structure

ConcreteVisitor

ObjectStructure

ConcreteElement1 ConcreteElement2

Client

Visitor

Element

visitConcreteElement1(ConcreteElement1)visitConcreteElement2(ConcreteElement2)

visitConcreteElement1(ConcreteElement1)visitConcreteElement2(ConcreteElement2)

accept(Visitor)

accept(Visitor v) accept(Visitor v)

v.visitConcreteElement1(this) v.visitConcreteElement2(this)

67

Spelling Checking and Hyphenation (cont'd)

Visitor (cont'd) object behavioral

Consequences

+ exibility: visitor and object structure are independent

+ localized functionality

� circular dependency between Visitor and Element interfaces

� Visitor brittle to new ConcreteElement classes

Implementation

� double dispatch

� overloading visit operations

� catch-all operation

� general interface to elements of object structure

Known Uses

ProgramNodeEnumerator in Smalltalk-80 compiler

IRIS Inventor scene rendering

68

Page 35: Vlissides John - West Virginia Universitycommunity.wvu.edu/~hhammar/rts/adv rts/design patterns-tutorial.pdfAn Intro duction to Design P atterns John Vlissides IBM T.J. W atson Resea

Part III: Wrap-Up

Observations

Applicable in all stages of the OO lifecycle

Design & reviews

Realization & documentation

Reuse & refactoring

Permit design at a more abstract level

Treat many class/object interactions as a unit

Often bene�cial after initial design

Targets for class refactorings

Variation-oriented design

Consider what design aspects are variable

Identify applicable pattern(s)

Vary patterns to evaluate tradeo�s

Repeat

69

But...

Resist branding everything a pattern

Articulate speci�c bene�ts

Demonstrate wide applicability

Find at least two existing examples

Don't apply them blindly

Added indirection ! increased complexity, cost

Pattern design even harder than OOD!

70

Page 36: Vlissides John - West Virginia Universitycommunity.wvu.edu/~hhammar/rts/adv rts/design patterns-tutorial.pdfAn Intro duction to Design P atterns John Vlissides IBM T.J. W atson Resea

Conclusion

Design patterns promote

� design reuse

� uniform design vocabulary

� understanding, restructuring

� automation

� a new way of thinking about design

71

(Design) Pattern References

The Timeless Way of Building, Alexander; Oxford, 1979;ISBN 0-19-502402-8

A Pattern Language, Alexander; Oxford, 1977; ISBN 0-19-501-919-9

Design Patterns, Gamma, et al.; Addison-Wesley, 1995;ISBN 0-201-63361-2; CD version ISBN 0-201-63498-8

Pattern-Oriented Software Architecture, Buschmann, et al.; Wiley,1996; ISBN 0-471-95869-7

Analysis Patterns, Fowler; Addison-Wesley, 1996; ISBN 0-201-89542-0

Smalltalk Best Practice Patterns, Beck; Prentice Hall, 1997;ISBN 0-13-476904-X

The Design Patterns Smalltalk Companion, Alpert, et al.;Addison-Wesley, 1998; ISBN 0-201-18462-1

AntiPatterns, Brown, et al.; Wiley, 1998; ISBN 0-471-19713-0

72

Page 37: Vlissides John - West Virginia Universitycommunity.wvu.edu/~hhammar/rts/adv rts/design patterns-tutorial.pdfAn Intro duction to Design P atterns John Vlissides IBM T.J. W atson Resea

More Books:

Pattern Languages of Program Design (Addison-Wesley)Vol. 1, Coplien, et al., eds.; 1995; ISBN 0-201-60734-4Vol. 2, Vlissides, et al., eds.; 1996; ISBN 0-201-89527-7Vol. 3, Martin, et al., eds.; 1998; ISBN 0-201-31011-2Vol. 4, Harrison, et al., eds.; 2000; ISBN 0-201-43304-4

Concurrent Programming in Java, Lea; Addison-Wesley, 1997;ISBN 0-201-69581-2

Applying UML and Patterns, Larman; Prentice Hall, 1997;ISBN 0-13-748880-7

Pattern Hatching: Design Patterns Applied, Vlissides;Addison-Wesley, 1998; ISBN 0-201-43293-5

Future Books:

The Pattern Almanac, Rising; Addison-Wesley, 2000;ISBN 0-201-61567-3

73

Early Papers:

\Object-Oriented Patterns," P. Coad; Comm. of the ACM, 9/92

\Documenting Frameworks using Patterns," R. Johnson;OOPSLA '92

\Design Patterns: Abstraction and Reuse of Object-OrientedDesign," Gamma, Helm, Johnson, Vlissides, ECOOP '93.

Columns:

C++ Report, Dr. Dobbs Sourcebook, JOOP, ROAD

74

Page 38: Vlissides John - West Virginia Universitycommunity.wvu.edu/~hhammar/rts/adv rts/design patterns-tutorial.pdfAn Intro duction to Design P atterns John Vlissides IBM T.J. W atson Resea

Conferences:

PLoP 2000: Pattern Languages of ProgramsSeptember 2000, Monticello, Illinois, USA

EuroPLoP 2000July 2000, Kloster Irsee, Germany

ChiliPLoP 2000March 2000, Wickenburg, Arizona, USA

KoalaPLoP 2000May 2000, Melbourne, Australia

See http://hillside.net/patterns/conferences for up-to-the-minute information.

75

Mailing Lists:

[email protected]: present and re�ne patterns

[email protected]: general discussion on patterns

[email protected]: discussion on Design Patterns

[email protected]: discussion on Pattern-Oriented

Software Architecture

[email protected]: discussion on user interface design patterns

[email protected]: discussion on patterns for businessprocesses

[email protected]: discussion on patterns for distributedsystems

See http://hillside.net/patterns/Lists.html for an up-to-date list.

76

Page 39: Vlissides John - West Virginia Universitycommunity.wvu.edu/~hhammar/rts/adv rts/design patterns-tutorial.pdfAn Intro duction to Design P atterns John Vlissides IBM T.J. W atson Resea

URLs:

Generalhttp://hillside.net/patterns

http://www.research.ibm.com/designpatterns

Conferenceshttp://hillside.net/patterns/conferences/

Bookshttp://hillside.net/patterns/books/

Mailing Listshttp://hillside.net/patterns/Lists.html

Portland Patterns Repositoryhttp://c2.com/ppr/index.html

77