Introduction to Design Patterns...Introduction to Design Patterns Pr. Jean-Marc Jézéquel IRISA -...

42
Introduction to Design Patterns Pr. Jean-Marc Jézéquel IRISA - Univ. Rennes 1 Campus de Beaulieu F-35042 Rennes Cedex Tel : +33 299 847 192 Fax : +33 299 847 171 e-mail : [email protected] http://www.irisa.fr/prive/jezequel

Transcript of Introduction to Design Patterns...Introduction to Design Patterns Pr. Jean-Marc Jézéquel IRISA -...

Page 1: Introduction to Design Patterns...Introduction to Design Patterns Pr. Jean-Marc Jézéquel IRISA - Univ. Rennes 1 Campus de Beaulieu F-35042 Rennes Cedex Tel : +33 299 847 192 Fax

Introduction to Design Patterns

Pr. Jean-Marc JézéquelIRISA - Univ. Rennes 1

Campus de BeaulieuF-35042 Rennes Cedex

Tel : +33 299 847 192 Fax : +33 299 847 171e-mail : [email protected]

http://www.irisa.fr/prive/jezequel

Page 2: Introduction to Design Patterns...Introduction to Design Patterns Pr. Jean-Marc Jézéquel IRISA - Univ. Rennes 1 Campus de Beaulieu F-35042 Rennes Cedex Tel : +33 299 847 192 Fax

Outline

z1. Context and Originz2. A Concrete Example: the Observerz3. Using Design Patternsz4. Describing Design Patternsz5. Overview of GoF’s catalogz6. More into the details...z7. Conclusion

Page 3: Introduction to Design Patterns...Introduction to Design Patterns Pr. Jean-Marc Jézéquel IRISA - Univ. Rennes 1 Campus de Beaulieu F-35042 Rennes Cedex Tel : +33 299 847 192 Fax

1. Context and Origin

Page 4: Introduction to Design Patterns...Introduction to Design Patterns Pr. Jean-Marc Jézéquel IRISA - Univ. Rennes 1 Campus de Beaulieu F-35042 Rennes Cedex Tel : +33 299 847 192 Fax

Introduction

zDesign Patterns = Tricks of the TradeyRecurring Structures of Solutions to Design

Problems (both static & dynamic)ySemi-formalization of OO design tricksyRelative independence from (OO) languages

zRelated NotionsyAnalysis Patterns / Architectural Patterns /

Code Patterns (idioms)yFrameworks (reuse design + code)

Page 5: Introduction to Design Patterns...Introduction to Design Patterns Pr. Jean-Marc Jézéquel IRISA - Univ. Rennes 1 Campus de Beaulieu F-35042 Rennes Cedex Tel : +33 299 847 192 Fax

Framework Characteristics

zProvides an integrated set of domain specific functionalityye.g., business applications, telecommunications, window systems,

databases, distributed applications, OS kernels

zSemi-complete applicationyComplete applications are developed by inheriting from, and

instantiating parameterized framework components

zOften exhibit inversion of control at runtimeyi.e., the framework determines which methods to invoke in

response to events

Page 6: Introduction to Design Patterns...Introduction to Design Patterns Pr. Jean-Marc Jézéquel IRISA - Univ. Rennes 1 Campus de Beaulieu F-35042 Rennes Cedex Tel : +33 299 847 192 Fax

Class Libraries vs. OO Frameworks

APPLICATIONSPECIFIC

LOGIC

EVENTLOOP

NETWORKING

MATH ADTS

DATA BASEUSER INTERFACE

MATH

ADTS

APPLICATIONSPECIFIC

LOGICEVENTLOOP

USER INTERFACENETWORKING

DATA BASE

INVOKES

CALLSBACKSINVOKES

CLASS LIBRARIES

OBJECT-ORIENTED FRAMEWORK

EVENTLOOP

Page 7: Introduction to Design Patterns...Introduction to Design Patterns Pr. Jean-Marc Jézéquel IRISA - Univ. Rennes 1 Campus de Beaulieu F-35042 Rennes Cedex Tel : +33 299 847 192 Fax

Origin of Design PatternszGoF’s Book: A catalogyDesign Patterns: Elements of Reusable Object-Oriented

Software (Gamma, Helm, Johnson, Vlissides). Addison Wesley, 1995

zEarlier works by Beck, Coplien and others...zOrigin of Patterns in Architecture (C. Alexander)yEach pattern describes a problem which occurs over

and over again in our environment, and then describes the core of the solution to this problem in such a way that you can use this solution a million times over, without ever doing it the same way twice.

Page 8: Introduction to Design Patterns...Introduction to Design Patterns Pr. Jean-Marc Jézéquel IRISA - Univ. Rennes 1 Campus de Beaulieu F-35042 Rennes Cedex Tel : +33 299 847 192 Fax

2. Example

The Observer Pattern

Page 9: Introduction to Design Patterns...Introduction to Design Patterns Pr. Jean-Marc Jézéquel IRISA - Univ. Rennes 1 Campus de Beaulieu F-35042 Rennes Cedex Tel : +33 299 847 192 Fax

A Distributed File System

roo tEiffe l

SourceBin

Latex

Do c uments

ro ot

Eiffe l

S o urce Bin

Latex Documents

Remote File S e rver

roo tEiffe l

SourceBin

Latex

Do c uments

ro otEiffe l

S o urceBin

Latex

Documents

Client PC

Clie nt PCClien t PC

Page 10: Introduction to Design Patterns...Introduction to Design Patterns Pr. Jean-Marc Jézéquel IRISA - Univ. Rennes 1 Campus de Beaulieu F-35042 Rennes Cedex Tel : +33 299 847 192 Fax

Structure of the Observer Pattern

foreach o in observers loopo->update()

end loop

Subject

notify()attach(observer)detach(observer)

Concrete

get_state()

subject_stateSubject

Observer

update()

Concrete

update()

Observer

*

subject -> get_state()return subject_state

{abstract}{abstract}

1 < subject

Page 11: Introduction to Design Patterns...Introduction to Design Patterns Pr. Jean-Marc Jézéquel IRISA - Univ. Rennes 1 Campus de Beaulieu F-35042 Rennes Cedex Tel : +33 299 847 192 Fax

Collaborations in the Observer Pattern

ConcreteSubject

ConcreteObserver 1

ConcreteObserver 2

set_state()

notify()

update()

get_state()

update()

get_state()

Page 12: Introduction to Design Patterns...Introduction to Design Patterns Pr. Jean-Marc Jézéquel IRISA - Univ. Rennes 1 Campus de Beaulieu F-35042 Rennes Cedex Tel : +33 299 847 192 Fax

Another Problem...

zAny number of views on a Data Table in a windowing system…yclose, open views at

will…ychange the data from

any view⌧… and the other are

updated

0

20

40

60

80

100

1er trim.

2e trim.

3e trim.

4e trim.

0%

20%

40%

60%

80%

100%1er trim. 2e t r im. 3e t r im. 4e t r im.

Est 20,4 27,4 90 20,4Ouest 30,6 38,6 34,6 31,6Nord 45,9 46,9 45 43,9

1er trim. 2e trim. 3e trim. 4e trim.Est 20,4 27,4 90 20,4Ouest 30,6 38,6 34,6 31,6Nord 45,9 46,9 45 43,9

Page 13: Introduction to Design Patterns...Introduction to Design Patterns Pr. Jean-Marc Jézéquel IRISA - Univ. Rennes 1 Campus de Beaulieu F-35042 Rennes Cedex Tel : +33 299 847 192 Fax

ObserverzObjectifyDéfinir une dépendance de un-vers-plusieurs entre les

objets de façon à ce que, quand un objet change d’état,tous ses dépendants sont informés et mis à jour automatiquement

zContraintes clésyIl peut y avoir plusieurs observateursyChaque observateur peut réagir différemment à la même

informationyLe sujet devrait être aussi découplé que possible des

observateurs (ajout/suppr. dynamique d’observateurs)

Page 14: Introduction to Design Patterns...Introduction to Design Patterns Pr. Jean-Marc Jézéquel IRISA - Univ. Rennes 1 Campus de Beaulieu F-35042 Rennes Cedex Tel : +33 299 847 192 Fax

Yet Another Problem...

SUBJECTMODEL

REAL-TIMEMARKET

DATA FEED

STOCKQUOTES

OBSERVERS

Page 15: Introduction to Design Patterns...Introduction to Design Patterns Pr. Jean-Marc Jézéquel IRISA - Univ. Rennes 1 Campus de Beaulieu F-35042 Rennes Cedex Tel : +33 299 847 192 Fax

3. Using Design Patterns

In a Software Engineering Context...

Page 16: Introduction to Design Patterns...Introduction to Design Patterns Pr. Jean-Marc Jézéquel IRISA - Univ. Rennes 1 Campus de Beaulieu F-35042 Rennes Cedex Tel : +33 299 847 192 Fax

What Design Patterns are all about

zAs much about problems as about solutionsypairs problem/solution in a context

zNot about classes & objects but collaborationszAbout non-functional forces yreusability, portability, and extensibility…

zEmbody architectural know-how of experts

Page 17: Introduction to Design Patterns...Introduction to Design Patterns Pr. Jean-Marc Jézéquel IRISA - Univ. Rennes 1 Campus de Beaulieu F-35042 Rennes Cedex Tel : +33 299 847 192 Fax

Key Points ofDesign Patterns

zIdentification of reusable micro-architecturesycodifying good design ysuitable for classification (see GoF’s catalog)

zDefinition of a vocabulary for thinking about design at a higher level of abstraction⌧Analogy with chess playing (borrowed from Doug

Schmidt)

Page 18: Introduction to Design Patterns...Introduction to Design Patterns Pr. Jean-Marc Jézéquel IRISA - Univ. Rennes 1 Campus de Beaulieu F-35042 Rennes Cedex Tel : +33 299 847 192 Fax

Becoming a Chess Master

z First learn rules and physical requirementsy e.g., names of pieces, legal movements, chess board geometry

and orientation, etc.

z Then learn principlesye.g., relative value of certain pieces, strategic value of center

squares, power of a threat, etc.

z However, to become a master of chess, one must study the games of other mastersyThese games contain patterns that must be understood,

memorized, and applied repeatedly

z There are thousands upon thousands of these patterns

Page 19: Introduction to Design Patterns...Introduction to Design Patterns Pr. Jean-Marc Jézéquel IRISA - Univ. Rennes 1 Campus de Beaulieu F-35042 Rennes Cedex Tel : +33 299 847 192 Fax

Becoming a Master Software Designer

z First one learns the rulesye.g., the algorithms, data structures and languages of software

z Later, one learns the principles of software designye.g., structured programming, modular programming, object

oriented programming, generic programming, etc.

z But to truly master software design, one must study the designs of other mastersyThese designs contain patterns must be understood,

memorized, and applied repeatedly

z There are thousands upon thousands of these patterns

Page 20: Introduction to Design Patterns...Introduction to Design Patterns Pr. Jean-Marc Jézéquel IRISA - Univ. Rennes 1 Campus de Beaulieu F-35042 Rennes Cedex Tel : +33 299 847 192 Fax

Design Patterns in the Software Life-Cycle

Design

Software Design Expert

Domain

Model of the problem

Constraints(flexibility,

...)

Model of the implementation

PatternsCatalog

Expert

reusability,performances,

Page 21: Introduction to Design Patterns...Introduction to Design Patterns Pr. Jean-Marc Jézéquel IRISA - Univ. Rennes 1 Campus de Beaulieu F-35042 Rennes Cedex Tel : +33 299 847 192 Fax

4. Describing Design Patterns

In a Software Engineering Context...

Page 22: Introduction to Design Patterns...Introduction to Design Patterns Pr. Jean-Marc Jézéquel IRISA - Univ. Rennes 1 Campus de Beaulieu F-35042 Rennes Cedex Tel : +33 299 847 192 Fax

Interest of Documenting Design Patterns

zCommunication of architectural knowledge among developerszProvide a common vocabulary for

common design structuresyReduce complexityyEnhance expressiveness, abstractness

zDistill and disseminate experienceyAvoid development traps and pitfalls that are

usually learned only by experience

Page 23: Introduction to Design Patterns...Introduction to Design Patterns Pr. Jean-Marc Jézéquel IRISA - Univ. Rennes 1 Campus de Beaulieu F-35042 Rennes Cedex Tel : +33 299 847 192 Fax

Interest of Documenting Design Patterns (cont.)

zImprove documentationyCapture and preserve design informationyArticulate design decisions concisely

zBuild a Pattern LanguageyA cohesive collection of patterns that forms a

vocabulary for understanding and communicating ideas

zNeed of a more or less standard form...

Page 24: Introduction to Design Patterns...Introduction to Design Patterns Pr. Jean-Marc Jézéquel IRISA - Univ. Rennes 1 Campus de Beaulieu F-35042 Rennes Cedex Tel : +33 299 847 192 Fax

Design Patterns (Alexandrian Format)

zNamezProblem & ContextzForce(s) addressedzSolution (structure and collaborations)zExampleszPositive & negative consequences of usezRationalezRelated Patterns & Known Uses

Page 25: Introduction to Design Patterns...Introduction to Design Patterns Pr. Jean-Marc Jézéquel IRISA - Univ. Rennes 1 Campus de Beaulieu F-35042 Rennes Cedex Tel : +33 299 847 192 Fax

Design Pattern Descriptions (GoF Format)

zName (& Aliases)zIntentzMotivation & ApplicabilityzStructurezParticipants & CollaborationszConsequenceszImplementation, Sample Code and UsagezKnown Uses & Related Patterns

Page 26: Introduction to Design Patterns...Introduction to Design Patterns Pr. Jean-Marc Jézéquel IRISA - Univ. Rennes 1 Campus de Beaulieu F-35042 Rennes Cedex Tel : +33 299 847 192 Fax

5. GoF Design Patterns

zCreational patterns yDeal with initializing and configuring classes

and objects

zStructural patternsyDeal with decoupling interface and

implementation of classes and objects

zBehavioral patternsyDeal with dynamic interactions among

societies of classes and objects

Page 27: Introduction to Design Patterns...Introduction to Design Patterns Pr. Jean-Marc Jézéquel IRISA - Univ. Rennes 1 Campus de Beaulieu F-35042 Rennes Cedex Tel : +33 299 847 192 Fax

Creational PatternszAbstract FactoryyInterface for creating families of objects without

specifying their concrete classes

zBuilderyFactory for building complex objects incrementally

zFactory MethodyLets a class defer instantiation to subclasses.

zPrototypeyFactory for cloning new instances from a prototype

zSingletonyAccess to the unique instance of class

Page 28: Introduction to Design Patterns...Introduction to Design Patterns Pr. Jean-Marc Jézéquel IRISA - Univ. Rennes 1 Campus de Beaulieu F-35042 Rennes Cedex Tel : +33 299 847 192 Fax

Structural Patterns (1)

zAdapteryConvert the interface of a class into another interface

clients expect. zBridgeyDecouple an abstraction from its implementations

zCompositeyRecursive aggregations letting clients treat individual

objects and compositions of objects uniformly

zDecoratoryExtends an object functionalities dynamically.

Page 29: Introduction to Design Patterns...Introduction to Design Patterns Pr. Jean-Marc Jézéquel IRISA - Univ. Rennes 1 Campus de Beaulieu F-35042 Rennes Cedex Tel : +33 299 847 192 Fax

Structural Patterns (2)

zFacadeySimple interface for a subsystem

zFlyweightyEfficiently sharing many Fine-Grained Objects

zProxyyProvide a surrogate or placeholder for

another object to control access to it.

Page 30: Introduction to Design Patterns...Introduction to Design Patterns Pr. Jean-Marc Jézéquel IRISA - Univ. Rennes 1 Campus de Beaulieu F-35042 Rennes Cedex Tel : +33 299 847 192 Fax

Behavioral Patterns (1)

zChain of ResponsibilityyUncouple request sender from precise receiver

on a chain.

zCommandyRequest reified as first-class object

zInterpreteryLanguage interpreter for a grammar

zIteratorySequential access to elements of any

aggregate

Page 31: Introduction to Design Patterns...Introduction to Design Patterns Pr. Jean-Marc Jézéquel IRISA - Univ. Rennes 1 Campus de Beaulieu F-35042 Rennes Cedex Tel : +33 299 847 192 Fax

Behavioral Patterns (2)

zMediatoryManages interactions between objects

zMementoyCaptures and restores object states (snapshot)

zObserveryUpdate observers automatically when a subject

changes

zStatey State reified as first-class object

Page 32: Introduction to Design Patterns...Introduction to Design Patterns Pr. Jean-Marc Jézéquel IRISA - Univ. Rennes 1 Campus de Beaulieu F-35042 Rennes Cedex Tel : +33 299 847 192 Fax

Behavioral Patterns (3)

zStrategyyFlexibly choose among interchangeable

algorithms

zTemplate MethodySkeleton algo. with steps supplied in subclass

zVisitoryAdd operations to a set of classes without

modifying them each time.

Page 33: Introduction to Design Patterns...Introduction to Design Patterns Pr. Jean-Marc Jézéquel IRISA - Univ. Rennes 1 Campus de Beaulieu F-35042 Rennes Cedex Tel : +33 299 847 192 Fax

6. More into the details...

With Abstract Factory

Page 34: Introduction to Design Patterns...Introduction to Design Patterns Pr. Jean-Marc Jézéquel IRISA - Univ. Rennes 1 Campus de Beaulieu F-35042 Rennes Cedex Tel : +33 299 847 192 Fax

The Maze Example

Maze

add_roomdescription

Map_Site

Room

make

Dooris_open

enter {Abstract}

* entermake

Wall

enterroom#

1

0..4sides>

2<link

1number

set_side

enter

Game

Page 35: Introduction to Design Patterns...Introduction to Design Patterns Pr. Jean-Marc Jézéquel IRISA - Univ. Rennes 1 Campus de Beaulieu F-35042 Rennes Cedex Tel : +33 299 847 192 Fax

Create a basic gameclass BASEGAMEcreation makefeature -- Creation

make is-- Program entry pointdo

my_maze := create_mazemy_maze.describe

end -- makefeature

create_maze : MAZE is-- 2 rooms connected through a door: [r1|r2]local

r1, r2 : ROOMdoor : DOORwall : WALL

do!!Result.make_empty!!r1.make(1); !!r2.make(2); !!door.make(r1,r2)Result.add_room(r1); Result.add_room(r2)

-- Now set up r1!!wall; r1.set_north_side(wall)r1.set_east_side(door)!!wall; r1.set_south_side(wall)!!wall; r1.set_west_side(wall)-- Now set up r2!!wall; r2.set_north_side(wall)!!wall; r2.set_east_side(wall)!!wall; r2.set_south_side(wall)r2.set_west_side(door)

end -- create_mazefeature {NONE} -- Private

my_maze : MAZEend -- BASEGAME

Page 36: Introduction to Design Patterns...Introduction to Design Patterns Pr. Jean-Marc Jézéquel IRISA - Univ. Rennes 1 Campus de Beaulieu F-35042 Rennes Cedex Tel : +33 299 847 192 Fax

Evolution

zI want the same maze, but with specialized rooms (enchanted)zDo I need to re-write the basic game?ydanger of cut and paste...

zUse an Abstract Factoryyfor creating maze components

Page 37: Introduction to Design Patterns...Introduction to Design Patterns Pr. Jean-Marc Jézéquel IRISA - Univ. Rennes 1 Campus de Beaulieu F-35042 Rennes Cedex Tel : +33 299 847 192 Fax

With Abstract Factory

Maze

add_roomdescription

Map_Site

Room

make

Dooris_open

enter {Abstract}

* entermake

Wall

enterroom#

1

0..4sides>

2<link

1number

set_side

enter

Game

Maze_Factory

new_mazenew_roomnew_doornew_wall

Enchanted_Maze_Factory

new_mazenew_roomnew_doornew_wall

Page 38: Introduction to Design Patterns...Introduction to Design Patterns Pr. Jean-Marc Jézéquel IRISA - Univ. Rennes 1 Campus de Beaulieu F-35042 Rennes Cedex Tel : +33 299 847 192 Fax

Maze Factoryclass MAZE_FACTORYinherit

ABSTRACT_FACTORYrename new_product as new_maze redefine new_maze end;

feature {ANY} -- Publicnew_maze : MAZE is

do!!Result.make_empty

end -- new_mazenew_wall : WALL is

do!!Result

ensure created: Result /= Voidend -- new_wall

new_room (number : INTEGER) : ROOM isdo

!!Result.make(number)ensure created: Result /= Voidend -- new_room

new_door (r1, r2 : ROOM) : DOOR isdo

!!Result.make(r1,r2)ensure created: Result /= Voidend -- new_door

end -- MAZE_FACTORY

class ENCHANTED_MAZE_FACTORYinherit

MAZE_FACTORYredefine new_room, new_door end; -- (co-variant redefinition)

feature {ANY} -- Publicnew_room (number : INTEGER) : ENCHANTED_ROOM is

-- Creates an ENCHANTED_ROOMdo

cast_a_spell!!Result.make(number,last_spell_cast)

end -- new_roomnew_door (r1, r2 : ROOM) : LOCKED_DOOR is

do!!Result.make(r1,r2)

end -- new_doorfeature {NONE} -- Private

last_spell_cast : SPELLcast_a_spell is do !!last_spell_cast end

end -- ENCHANTED_MAZE_FACTORY

Page 39: Introduction to Design Patterns...Introduction to Design Patterns Pr. Jean-Marc Jézéquel IRISA - Univ. Rennes 1 Campus de Beaulieu F-35042 Rennes Cedex Tel : +33 299 847 192 Fax

class GAME_WITH_ABSTRACT_FACTORYcreation makefeature -- Creation

make is-- Program entry pointlocal maze_factory : MAZE_FACTORYdo

!!maze_factory -- A normal MAZEmy_maze := create_maze (maze_factory)my_maze.describe!ENCHANTED_MAZE_FACTORY!maze_factorymy_maze := create_maze (maze_factory)-- A MAZE with enchanted ROOMsmy_maze.describe

end -- make

Game with Abstract Factoryfeature

create_maze (factory : MAZE_FACTORY) : MAZE is-- Create a new mazelocal

r1, r2: ROOMdoor : DOOR

doResult := factory.new_mazer1 := factory.new_room(1); r2 := factory.new_room(2)door := factory.new_door(r1,r2)Result.add_room(r1); Result.add_room(r2)-- Now set up r1r1.set_north_side(factory.new_wall)r1.set_east_side(door)r1.set_south_side(factory.new_wall)r1.set_west_side(factory.new_wall)-- Now set up r2r2.set_north_side(factory.new_wall)r2.set_east_side(factory.new_wall)r2.set_south_side(factory.new_wall)r2.set_west_side(door)

end -- create_mazefeature {NONE} -- Private

my_maze : MAZEend -- GAME_WITH_ABSTRACT_FACTORY

Page 40: Introduction to Design Patterns...Introduction to Design Patterns Pr. Jean-Marc Jézéquel IRISA - Univ. Rennes 1 Campus de Beaulieu F-35042 Rennes Cedex Tel : +33 299 847 192 Fax

Conclusion

zDesign Patterns have raised the level at which most OO designs are done nowzUseful thing in the designer’s toolkityBut no silver bullet…

zOngoing systematic efforts to catalog DPsymaybe towards the software engineering

manual.

Page 41: Introduction to Design Patterns...Introduction to Design Patterns Pr. Jean-Marc Jézéquel IRISA - Univ. Rennes 1 Campus de Beaulieu F-35042 Rennes Cedex Tel : +33 299 847 192 Fax

Further References

z Design Patterns and Contracts ……...yAddison-Wesley 1999. ISBN 0-201-30959-9

z Design Patterns: Elements of ReusableObject-Oriented SoftwareyGamma, Helm, Johnson, Vlissides. Addison Wesley, 1995

z Pattern Oriented Software Architecture, A System of PatternsyBuschmann, Meunier, Sommerland, Stal. Wiley & Sons, 1996

z Pattern Languages of Program DesignyPLoPD 1, 2, 3 & 4. Addison-Wesley

z http://hillside.net/patterns

Page 42: Introduction to Design Patterns...Introduction to Design Patterns Pr. Jean-Marc Jézéquel IRISA - Univ. Rennes 1 Campus de Beaulieu F-35042 Rennes Cedex Tel : +33 299 847 192 Fax

And...

Object-Oriented SoftwareEngineering with Eiffel

by Jean-Marc Jézéquel

Addison-Wesley Eiffel in Practice SeriesISBN 0-201-63381-7 * Paperback368 pages * ©1996http://www.irisa.fr/pampa/EPEE/book.html