1 design patterns hausi a. müller university of victoria software architecture course spring 2000

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

Post on 20-Dec-2015

219 views

Category:

Documents


0 download

TRANSCRIPT

11

Design PatternsDesign Patterns

Hausi A. MüllerHausi A. Müller

University of VictoriaUniversity of Victoria

Software Architecture CourseSoftware Architecture Course

Spring 2000Spring 2000

Software Architecture Software Architecture 22

MotivationMotivation

Vehicle for reasoning about design or Vehicle for reasoning about design or architecture at a higher level of abstraction architecture at a higher level of abstraction (design confidence)(design confidence)

Mining or discovering design patterns in Mining or discovering design patterns in legacy systemslegacy systems

Software architectureSoftware architecture• dissemination of good design, design reusedissemination of good design, design reuse

Engineering HandbooksEngineering Handbooks• contain a wealth of experiencecontain a wealth of experience

Software Architecture Software Architecture 33

Software PatternsSoftware Patterns

Design patterns [GoF]Design patterns [GoF] Pattern languages [Coplien]Pattern languages [Coplien] Software idioms [Coplien]Software idioms [Coplien] Analysis patterns [Fowler]Analysis patterns [Fowler] AntiPatterns [Brown]AntiPatterns [Brown] FrameworksFrameworks STL (C++ Template Library)STL (C++ Template Library) Algorithms and data structuresAlgorithms and data structures

Software Architecture Software Architecture 44

Software Patterns ...Software Patterns ...

Conceptual patternsConceptual patterns Architectural patternsArchitectural patterns Design patternsDesign patterns Generative patternsGenerative patterns Programming patterns or idiomsProgramming patterns or idioms Analysis patternsAnalysis patterns AntiPatternsAntiPatterns Organizational patternsOrganizational patterns

Software Architecture Software Architecture 55

Pattern DefinitionsPattern Definitions

A pattern is a named nugget of insight A pattern is a named nugget of insight that conveys the essence of a proven that conveys the essence of a proven solution to a recurring problem within a solution to a recurring problem within a certain context amidst competing certain context amidst competing concerns [Riehle]concerns [Riehle]

A pattern is the abstraction from a A pattern is the abstraction from a concrete form which keeps recurring in concrete form which keeps recurring in specific non-arbitrary contexts.specific non-arbitrary contexts.

Software Architecture Software Architecture 66

Pattern Definitions ...Pattern Definitions ...

A pattern is a named nugget of A pattern is a named nugget of instructive information that captures the instructive information that captures the essential structure and insight of a essential structure and insight of a successful family of proven solutions to successful family of proven solutions to a recurring problem that arises within a a recurring problem that arises within a certain context and system forces.certain context and system forces.

Software Architecture Software Architecture 77

Pattern Definitions ...Pattern Definitions ...

Description of communicating objects Description of communicating objects and classes that are customized to solve and classes that are customized to solve a general design in a particular context a general design in a particular context [GoF].[GoF].

Design patterns capture the static and Design patterns capture the static and dynamic structures of solutions that occur dynamic structures of solutions that occur repeatedly when producing applications repeatedly when producing applications in a particular context [Coplien].in a particular context [Coplien].

Software Architecture Software Architecture 88

Historical Perspective onHistorical Perspective onDesign PatternsDesign Patterns

19791979• Alexander’s Timeless Way of BuildingAlexander’s Timeless Way of Building

19871987• OOPSLA workshop by Beck & WardOOPSLA workshop by Beck & Ward

19941994• First PLoP conferenceFirst PLoP conference

19951995• GoF (Gamma, Helm, Johnson, Vlissides)GoF (Gamma, Helm, Johnson, Vlissides)• Design Patterns; Elements of Reusable Design Patterns; Elements of Reusable

Object-Oriented SoftwareObject-Oriented Software

Software Architecture Software Architecture 99

A Good PatternA Good Pattern

It solves a problemIt solves a problem• Patterns capture solutions, not just abstract Patterns capture solutions, not just abstract

principles or strategiesprinciples or strategies It is a proven conceptIt is a proven concept• Patterns capture solutions with a track record, Patterns capture solutions with a track record,

not theories or speculationnot theories or speculation The solution isn’t obviousThe solution isn’t obvious• The best patterns generate a solution indirectly; The best patterns generate a solution indirectly;

normal for many design problemsnormal for many design problems

Software Architecture Software Architecture 1010

A Good Pattern ...A Good Pattern ...

It describes a relationshipIt describes a relationship• Patterns describe more than black boxes: Patterns describe more than black boxes:

system structures and mechanismssystem structures and mechanisms The pattern has a significant human The pattern has a significant human

componentcomponent• The best patterns explicitly appeal to The best patterns explicitly appeal to

aesthetics and utilityaesthetics and utility

Software Architecture Software Architecture 1111

Patterns FormatsPatterns Formats

GoF formatGoF format Alexandrian form (canonical form)Alexandrian form (canonical form) Essential components of a pattern formatEssential components of a pattern format• Name, problem, context, forcesName, problem, context, forces• Solution, examples, context,Solution, examples, context,• Rationale, related patterns, known usesRationale, related patterns, known uses

Software Architecture Software Architecture 1212

Pattern Format ...Pattern Format ...

NameName• meaningful phrasemeaningful phrase

ProblemProblem• a statement of the problem which describes a statement of the problem which describes

its intent: the goals and objectives it wants to its intent: the goals and objectives it wants to reach within the given context and forcesreach within the given context and forces

Software Architecture Software Architecture 1313

Pattern Format ...Pattern Format ...

ContextContext• preconditions under which the problem and its preconditions under which the problem and its

solutions seem to occursolutions seem to occur• the pattern’s applicabilitythe pattern’s applicability• may change over timemay change over time

ForcesForces• relevant forces and constraints and their relevant forces and constraints and their

interactions and conflictsinteractions and conflicts• motivational scenario for the patternmotivational scenario for the pattern

Software Architecture Software Architecture 1414

Pattern Format ...Pattern Format ...

SolutionSolution• Static and dynamic relationships describing Static and dynamic relationships describing

how to realize the patternhow to realize the pattern• instructions on how to construct the work instructions on how to construct the work

productsproducts• pictures, diagrams, prose which highlight the pictures, diagrams, prose which highlight the

pattern’s structure, participants, and pattern’s structure, participants, and collaborationscollaborations

Software Architecture Software Architecture 1515

Pattern Format ...Pattern Format ...

ExamplesExamples• one or more sample applications to illustrate one or more sample applications to illustrate

– a specific contexta specific context– how the pattern is appliedhow the pattern is applied

Resulting contextResulting context• the state or configuration after the pattern has the state or configuration after the pattern has

been appliedbeen applied• consequences (good and bad) of applying the consequences (good and bad) of applying the

patternpattern

Software Architecture Software Architecture 1616

Pattern Format ...Pattern Format ...

RationaleRationale• justification of the steps or rules in the patternjustification of the steps or rules in the pattern• how and why it resolves the forces to achieve how and why it resolves the forces to achieve

the desired goals, principles, and philosophiesthe desired goals, principles, and philosophies• how are the forces orchestrated to achieve how are the forces orchestrated to achieve

harmonyharmony• how does the pattern actually workhow does the pattern actually work

Software Architecture Software Architecture 1717

Pattern Format ...Pattern Format ...

Related patternsRelated patterns• the static and dynamic relationships the static and dynamic relationships

between this pattern and other patternsbetween this pattern and other patterns Known usesKnown uses• to demonstrate that this is a proven solution to demonstrate that this is a proven solution

to a recurring problemto a recurring problem

Software Architecture Software Architecture 1818

Qualities of a PatternQualities of a Pattern

Encapsulation and abstractionEncapsulation and abstraction• encapsulates a well-defined problem and its encapsulates a well-defined problem and its

solution in a particular domainsolution in a particular domain• provides crisp, clear boundaries to provides crisp, clear boundaries to

crystallize the problem and solution spacescrystallize the problem and solution spaces• serves as an abstraction which embodies serves as an abstraction which embodies

domain knowledge and experiencedomain knowledge and experience• may occur at different levels of abstractionmay occur at different levels of abstraction

Software Architecture Software Architecture 1919

Qualities of a Pattern ...Qualities of a Pattern ...

Openness and variabilityOpenness and variability• is open for extension and parameterization is open for extension and parameterization

by other patternsby other patterns• is able to solve larger problems in concert is able to solve larger problems in concert

with other patternswith other patterns• can be realized by a variety of can be realized by a variety of

implementations (variants)implementations (variants)

Software Architecture Software Architecture 2020

Qualities of a Pattern ...Qualities of a Pattern ...

Generativity and composabilityGenerativity and composability• applying a pattern once provides a context applying a pattern once provides a context

for further applicationsfor further applications• patterns are easier to apply in another patterns are easier to apply in another

context than C++ codecontext than C++ code• can evolve into Golden Hammer AntiPatterncan evolve into Golden Hammer AntiPattern

Software Architecture Software Architecture 2121

Qualities of a Pattern ...Qualities of a Pattern ...

EquilibriumEquilibrium• realizes a balance among its forces and realizes a balance among its forces and

constraintsconstraints• realizes an invariant, heuristics, or a policy realizes an invariant, heuristics, or a policy

which minimizes conflict within the solution which minimizes conflict within the solution spacespace

• an invariant characterizes the problem an invariant characterizes the problem solving philosophysolving philosophy

Software Architecture Software Architecture 2222

GoF Catalog ofGoF Catalog of23 Design Patters23 Design Patters

Creational patternsCreational patterns• Abstract the instantiation processAbstract the instantiation process• Make the system independent on how the Make the system independent on how the

objects are created, composed, and objects are created, composed, and representedrepresented– Abstract FactoryAbstract Factory– BuilderBuilder– Factory MethodFactory Method– PrototypePrototype– SingletonSingleton

Software Architecture Software Architecture 2323

GoF Catalog ofGoF Catalog of23 Design Patters ...23 Design Patters ...

Structural patternsStructural patterns• Composition of classes and objects to form Composition of classes and objects to form

larger structureslarger structures• Compose classes to form new interfacesCompose classes to form new interfaces• Compose objects to provide new functionalityCompose objects to provide new functionality

– AdaptorAdaptor– BridgeBridge– CompositeComposite– DecoratorDecorator– FaçadeFaçade– FlyweightFlyweight– ProxyProxy

Software Architecture Software Architecture 2424

GoF Catalog ofGoF Catalog of23 Design Patters ...23 Design Patters ...

Behavioral patternsBehavioral patterns• concerned with algorithms and the concerned with algorithms and the

assignment of responsibilities among objectsassignment of responsibilities among objects– Chain of ResponsibilityChain of Responsibility– CommandCommand– InterpreterInterpreter– IteratorIterator–MediatorMediator–MementoMemento– ObserverObserver– StateState– StrategyStrategy– Template MethodTemplate Method– VisitorVisitor

Software Architecture Software Architecture 2525

SummarySummary

Vehicle for reasoning about design, Vehicle for reasoning about design, architecture, component technologyarchitecture, component technology

GoF book is great but there are many GoF book is great but there are many other software patternsother software patterns