chapter five an introduction to design patterns ku-yaw chang [email protected] assistant...

25
Chapter Five Chapter Five An Introduction to An Introduction to Design Patterns Design Patterns Ku-Yaw Chang Ku-Yaw Chang [email protected] [email protected] Assistant Professor, Department of Assistant Professor, Department of Computer Science and Information Engineering Computer Science and Information Engineering Da-Yeh University Da-Yeh University

Upload: randell-anthony

Post on 13-Dec-2015

225 views

Category:

Documents


0 download

TRANSCRIPT

Chapter FiveChapter Five

An Introduction toAn Introduction toDesign PatternsDesign Patterns

Ku-Yaw ChangKu-Yaw [email protected]@mail.dyu.edu.tw

Assistant Professor, Department of Assistant Professor, Department of Computer Science and Information EngineeringComputer Science and Information Engineering

Da-Yeh UniversityDa-Yeh University

22Ku-Yaw ChangKu-Yaw Chang An Introduction to Design PatternsAn Introduction to Design Patterns

OutlineOutline

OverviewOverviewDesign Patterns Arose from Architecture and Design Patterns Arose from Architecture and AnthropologyAnthropologyMoving from Architectural to Software Design PatternsMoving from Architectural to Software Design PatternsWhy Study Design Patterns?Why Study Design Patterns?Other Advantages to Studying Design PatternsOther Advantages to Studying Design PatternsSummarySummary

33Ku-Yaw ChangKu-Yaw Chang An Introduction to Design PatternsAn Introduction to Design Patterns

OverviewOverview

Design patternsDesign patterns Cutting edge of the OO technologyCutting edge of the OO technology

RelationshipsRelationships Master OO skills first, then learn design patternsMaster OO skills first, then learn design patterns The opposite is also trueThe opposite is also true

Learning design patterns early in the learning of object-Learning design patterns early in the learning of object-oriented skills greatly helps to improve understanding of oriented skills greatly helps to improve understanding of object-oriented analysis and design.object-oriented analysis and design.

Design patterns and OO design reinforce each otherDesign patterns and OO design reinforce each other

Many ideas are from Many ideas are from Christopher Alexander’s Christopher Alexander’s Timeless Way of BuildingTimeless Way of Building

44Ku-Yaw ChangKu-Yaw Chang An Introduction to Design PatternsAn Introduction to Design Patterns

OutlineOutline

OverviewOverviewDesign Patterns Arose from Architecture and Design Patterns Arose from Architecture and AnthropologyAnthropologyMoving from Architectural to Software Design PatternsMoving from Architectural to Software Design PatternsWhy Study Design Patterns?Why Study Design Patterns?Other Advantages to Studying Design PatternsOther Advantages to Studying Design PatternsSummarySummary

55Ku-Yaw ChangKu-Yaw Chang An Introduction to Design PatternsAn Introduction to Design Patterns

Design Patterns Arose from Design Patterns Arose from Architecture and AnthropologyArchitecture and Anthropology

Christopher Alexander asked himselfChristopher Alexander asked himself Is quality objective?Is quality objective? What makes us know when an architectural What makes us know when an architectural

design is good?design is good?

Alexander postulates Alexander postulates There is such an objective basis within There is such an objective basis within

architectural systems.architectural systems. We can describe beauty through an objective We can describe beauty through an objective

basis that can be measured.basis that can be measured.

66Ku-Yaw ChangKu-Yaw Chang An Introduction to Design PatternsAn Introduction to Design Patterns

Design Patterns Arose from Design Patterns Arose from Architecture and AnthropologyArchitecture and Anthropology

If quality in design is objectiveIf quality in design is objective Be able to identify what makes designs good and Be able to identify what makes designs good and

what makes designs badwhat makes designs bad

Alexander askedAlexander asked What is present in a good quality design that is not What is present in a good quality design that is not

present in a poor quality design?present in a poor quality design? What is present in a poor quality design that is not What is present in a poor quality design that is not

present in a good quality design?present in a good quality design?

Alexander discoveredAlexander discovered Good constructs had things in common with each Good constructs had things in common with each

otherother

77Ku-Yaw ChangKu-Yaw Chang An Introduction to Design PatternsAn Introduction to Design Patterns

Design Patterns Arose from Design Patterns Arose from Architecture and AnthropologyArchitecture and Anthropology

Two porches mayTwo porches may appear structurally differentappear structurally different solve the same (or similar) problemsolve the same (or similar) problem be considered high qualitybe considered high quality

88Ku-Yaw ChangKu-Yaw Chang An Introduction to Design PatternsAn Introduction to Design Patterns

Design Patterns Arose from Design Patterns Arose from Architecture and AnthropologyArchitecture and Anthropology

Alexander narrowed his focusAlexander narrowed his focus Look at structures that solve similar problemsLook at structures that solve similar problems Discern similarities between designs that were high Discern similarities between designs that were high

qualityquality These similarities are called These similarities are called patternspatterns

A solution to a problem in a contextA solution to a problem in a context Each pattern describes Each pattern describes a problema problem which occurs over which occurs over

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

99Ku-Yaw ChangKu-Yaw Chang An Introduction to Design PatternsAn Introduction to Design Patterns

Design Patterns Arose from Design Patterns Arose from Architecture and AnthropologyArchitecture and Anthropology

Four essential itemsFour essential items The name of the patternThe name of the pattern The purpose of the pattern, the problem it solvesThe purpose of the pattern, the problem it solves How we could accomplish thisHow we could accomplish this The constraints and forces we have to consider in The constraints and forces we have to consider in

order to accomplish itorder to accomplish it

Alexander postulatedAlexander postulated Patterns can solve virtually every architectural Patterns can solve virtually every architectural

problem that one will encounterproblem that one will encounter Patterns could be used together to solve complex Patterns could be used together to solve complex

architectural problemsarchitectural problems

1010Ku-Yaw ChangKu-Yaw Chang An Introduction to Design PatternsAn Introduction to Design Patterns

OutlineOutline

OverviewOverviewDesign Patterns Arose from Architecture and Design Patterns Arose from Architecture and AnthropologyAnthropologyMoving from Architectural to Software Design PatternsMoving from Architectural to Software Design PatternsWhy Study Design Patterns?Why Study Design Patterns?Other Advantages to Studying Design PatternsOther Advantages to Studying Design PatternsSummarySummary

1111Ku-Yaw ChangKu-Yaw Chang An Introduction to Design PatternsAn Introduction to Design Patterns

Moving from Architectural to Moving from Architectural to Software Design PatternsSoftware Design Patterns

In the early 1990s, some smart developers In the early 1990s, some smart developers Happened upon Alexander’s work in patternsHappened upon Alexander’s work in patterns Wondered if what was true for architectural patterns Wondered if what was true for architectural patterns

would also be true for software design?would also be true for software design?Were there problems in software that occur over and over Were there problems in software that occur over and over again that could be solved somewhat the same manner?again that could be solved somewhat the same manner?

Was it possible to design software in terms of patterns, Was it possible to design software in terms of patterns, creating specific solutions based on these patterns only after creating specific solutions based on these patterns only after the patterns had been identified?the patterns had been identified?

The answer to both of the above questions are The answer to both of the above questions are “unequivocally yes.”“unequivocally yes.”

1212Ku-Yaw ChangKu-Yaw Chang An Introduction to Design PatternsAn Introduction to Design Patterns

Moving from Architectural to Moving from Architectural to Software Design PatternsSoftware Design Patterns

Early work on design patternsEarly work on design patterns Design Patterns: Elements of Reusable Object-Design Patterns: Elements of Reusable Object-

Oriented Software by GoFOriented Software by GoFIdentifiedIdentified the patterns ( the patterns ( not createnot create the patterns ) the patterns )

Served the following purposesServed the following purposesIt applied the idea of design patterns to software design.It applied the idea of design patterns to software design.

It described a structure within which to catalog and describe It described a structure within which to catalog and describe design patterns.design patterns.

It cataloged 23 such patterns.It cataloged 23 such patterns.

It postulated object-oriented strategies and approaches It postulated object-oriented strategies and approaches based on these design patterns.based on these design patterns.

1313Ku-Yaw ChangKu-Yaw Chang An Introduction to Design PatternsAn Introduction to Design Patterns

Key Features of PatternsKey Features of Patterns

1414Ku-Yaw ChangKu-Yaw Chang An Introduction to Design PatternsAn Introduction to Design Patterns

OutlineOutline

OverviewOverviewDesign Patterns Arose from Architecture and Design Patterns Arose from Architecture and AnthropologyAnthropologyMoving from Architectural to Software Design PatternsMoving from Architectural to Software Design PatternsWhy Study Design Patterns?Why Study Design Patterns?Other Advantages to Studying Design PatternsOther Advantages to Studying Design PatternsSummarySummary

1515Ku-Yaw ChangKu-Yaw Chang An Introduction to Design PatternsAn Introduction to Design Patterns

Why Study Design Patterns?Why Study Design Patterns?

Reuse solutionsReuse solutions Not have to reinvent solutions for commonly recurring Not have to reinvent solutions for commonly recurring

problemsproblems

Establish common terminologyEstablish common terminology A common base of vocabularyA common base of vocabulary A common viewpoint of the problemA common viewpoint of the problem

Give a higher-level perspectiveGive a higher-level perspective On the problemOn the problem On the process of design and object orientationOn the process of design and object orientation

1616Ku-Yaw ChangKu-Yaw Chang An Introduction to Design PatternsAn Introduction to Design Patterns

Why Study Design Patterns?Why Study Design Patterns?

A conversation between two carpentersA conversation between two carpenters Carpenter 1: How do you think we should Carpenter 1: How do you think we should

build these drawers?build these drawers? Carpenter 2: Well, I think we should make the Carpenter 2: Well, I think we should make the

joint by cutting straight down into the wood, joint by cutting straight down into the wood, and then cut back up 45 degrees, and the and then cut back up 45 degrees, and the going straight back down, and then back up going straight back down, and then back up the other way 45 degree, and the going the other way 45 degree, and the going straight back down, and then …straight back down, and then …

The details may confuse the solution.The details may confuse the solution.

1717Ku-Yaw ChangKu-Yaw Chang An Introduction to Design PatternsAn Introduction to Design Patterns

Why Study Design Patterns?Why Study Design Patterns?

1818Ku-Yaw ChangKu-Yaw Chang An Introduction to Design PatternsAn Introduction to Design Patterns

Why Study Design Patterns?Why Study Design Patterns?

Carpenter 1: Should we use a dovetail joint or Carpenter 1: Should we use a dovetail joint or a miter joint?a miter joint? A miter jointA miter joint

It is a simpler solution.It is a simpler solution.

It is lightweight.It is lightweight.

It is inconspicuous.It is inconspicuous. A dovetail jointA dovetail joint

It is a more complex solution.It is a more complex solution.

It is impervious to temperature and humidity.It is impervious to temperature and humidity.

It is independent of the fastening system.It is independent of the fastening system.

It is a more aesthetically pleasing joint.It is a more aesthetically pleasing joint.

1919Ku-Yaw ChangKu-Yaw Chang An Introduction to Design PatternsAn Introduction to Design Patterns

Why Study Design Patterns?Why Study Design Patterns?

Carpenter 1Carpenter 1 Should we use a dovetail joint or a miter joint?Should we use a dovetail joint or a miter joint?

The real question isThe real question is Should we use a joint that is expensive to make Should we use a joint that is expensive to make

but is both beautiful and durable, or should we just but is both beautiful and durable, or should we just make a quick and dirty joint that will last at least as make a quick and dirty joint that will last at least as long until the check clears?long until the check clears?

Which is more efficient?Which is more efficient?Who would you rather work with?Who would you rather work with?Patterns help raise the level of your thinkingPatterns help raise the level of your thinking See the forest and the treesSee the forest and the trees

2020Ku-Yaw ChangKu-Yaw Chang An Introduction to Design PatternsAn Introduction to Design Patterns

OutlineOutline

OverviewOverviewDesign Patterns Arose from Architecture and Design Patterns Arose from Architecture and AnthropologyAnthropologyMoving from Architectural to Software Design PatternsMoving from Architectural to Software Design PatternsWhy Study Design Patterns?Why Study Design Patterns?Other Advantages to Studying Design PatternsOther Advantages to Studying Design PatternsSummarySummary

2121Ku-Yaw ChangKu-Yaw Chang An Introduction to Design PatternsAn Introduction to Design Patterns

Other Advantages toOther Advantages toStudying Design PatternsStudying Design Patterns

Experiences from the AuthorExperiences from the Author Help both individual learning and team Help both individual learning and team

developmentdevelopment Improve modifiability of codeImprove modifiability of code Illustrate basic object-oriented principlesIllustrate basic object-oriented principles

Strategies from GoFStrategies from GoF Design to InterfaceDesign to Interface Favor composition over inheritanceFavor composition over inheritance Find what varies and encapsulate itFind what varies and encapsulate it

2222Ku-Yaw ChangKu-Yaw Chang An Introduction to Design PatternsAn Introduction to Design Patterns

OutlineOutline

OverviewOverviewDesign Patterns Arose from Architecture and Design Patterns Arose from Architecture and AnthropologyAnthropologyMoving from Architectural to Software Design PatternsMoving from Architectural to Software Design PatternsWhy Study Design Patterns?Why Study Design Patterns?Other Advantages to Studying Design PatternsOther Advantages to Studying Design PatternsSummarySummary

2323Ku-Yaw ChangKu-Yaw Chang An Introduction to Design PatternsAn Introduction to Design Patterns

SummarySummary

Reasons for studying design patternsReasons for studying design patterns Reuse existing, high-quality solutions to Reuse existing, high-quality solutions to

commonly recurring problems.commonly recurring problems. Establish common terminology to improve Establish common terminology to improve

communications within teams.communications within teams. Shift the level of thinking to a higher Shift the level of thinking to a higher

perspective.perspective. Decide whether I have the right design, not Decide whether I have the right design, not

just one that works.just one that works.

2424Ku-Yaw ChangKu-Yaw Chang An Introduction to Design PatternsAn Introduction to Design Patterns

SummarySummary

(cont.) (cont.) Improve individual learning and team learning.Improve individual learning and team learning. Improve the modifiability of code.Improve the modifiability of code. Facilitate adoption of improved design Facilitate adoption of improved design

alternatives, even when patterns are not used alternatives, even when patterns are not used explicitly.explicitly.

Discover alternatives to large inheritance Discover alternatives to large inheritance hierarchies.hierarchies.

The EndThe End