Shalloway ch5 Intro
Shalloway ch5 Intro
An Introduction
to Design Patterns
Overview
This chapter introduces the concept of design patterns. In this chapter
In this chapter,
Design patterns are part of the cutting edge of object-oriented tech- Design patterns and
nology. Object-oriented analysis tools, books, and seminars are object-oriented
incorporating design patterns. Study groups on design patterns design reinforce
abound. It is often suggested that people learn design patterns only each other
after they have mastered basic object-oriented skills. I have found
that the opposite is true: learning design patterns early in the learn-
ing of object-oriented skills greatly helps to improve understanding
of object-oriented analysis and design.
Throughout the rest of the book, I will discuss not only design pat-
terns, but also how they reveal and reinforce good object-oriented
principles. I hope to improve both your understanding of these
principles and illustrate why the design patterns being discussed
here represent good designs.
Some of this material may seem abstract or philosophical. But give Give this a chance
it a chance! This chapter lays the foundation for your understand-
ing of design patterns. Understanding this material will enhance
your ability to understand and work with new patterns.
71
72 Part III • Design Patterns
1. Alexander, C., Ishikawa, S., Silverstein, M., The Timeless Way of Building, New
York: Oxford University Press, 1979.
2. The anthropologist Ruth Benedict is a pioneer in pattern-based analysis of cul-
tures. For examples, see Benedict, R., The Chrysanthemum and the Sword, Boston:
Houghton Mifflin, 1946.
Chapter 5 • An Introduction to Design Patterns 73
If you accept the idea that it is possible to recognize and describe a How do you get good
good quality design, then how do you go about creating one? I can quality repeatedly?
imagine Alexander asking himself,
and
Alexander studied this problem by making many observations of Look for the
buildings, towns, streets, and virtually every other aspect of living commonalties
spaces that human beings have built for themselves. He discovered
that, for a particular architectural creation, good constructs had
things in common with each other.
Architectural structures differ from each other, even if they are of . . . especially
the same type. Yet even though they are different, they can still be commonality in
of high quality. the features of the
problem to be solved
For example, two porches may appear structurally different and yet
both may still be considered high quality. They might be solving dif-
ferent problems for different houses. One porch may be a transition
from the walkway to the front door. Another porch might be a
place for shade on a hot day. Or two porches might solve a common
problem (transition) in different ways.
74 Part III • Design Patterns
Figure 5-1 Structures may look different but still solve a common
problem.
This led to the Alexander discovered that by narrowing his focus in this way—by
concept of a pattern looking at structures that solve similar problems—he could discern
similarities between designs that were high quality. He called these
similarities, patterns.
3. Alexander, C., Ishikawa, S., Silverstein, M., A Pattern Language, New York: Oxford
University Press, 1977, p. x.
Chapter 5 • An Introduction to Design Patterns 75
In the same way, a courtyard, which is prop- A pattern always has a name and has a purpose.
erly formed, helps people come to life in it. Here, the pattern’s name is Courtyard and its pur-
pose is to help people to come to life in it.
But there are more subtle forces too. For He points out a difficulty with the simplified solu-
instance, when a courtyard is too tightly tion and then gives us a way to solve the problem
enclosed, has no view out, people feel that he has just pointed out.
uncomfortable, and tend to stay away . . . they
need to see out into some larger and more
distant space.
Or again, people are creatures of habit. If they Familiarity sometimes keeps us from seeing the
pass in and out of the courtyard, every day, in obvious. The value of a pattern is that those with
the course of their normal lives, the courtyard less experience can take advantage of what oth-
becomes familiar, a natural place to go . . . ers have learned before them: both what must be
and it is used. included to have a good design, and what must be
avoided to keep from a poor design.
(continued)
4. Alexander, C., Ishikawa, S., Silverstein, M., The Timeless Way of Building, New
York: Oxford University Press, 1977.
76 Part III • Design Patterns
When a courtyard has a view out to a larger Alexander is telling us how to build a great
space, has crossing paths from different courtyard . . .
rooms, and has a veranda or a porch, these
forces can resolve themselves. The view out . . . and then tells us why it is great.
makes it comfortable, the crossing paths help
generate a sense of habit there, the porch
makes it easier to go out more often . . . and
gradually the courtyard becomes a pleasant
customary place to be.
The four compo- To review, Alexander says that a description of a pattern involves
nents required of four items:
every pattern
description • The name of the pattern
Alexander postulated that patterns can solve virtually every archi- Patterns exist for
tectural problem that one will encounter. He further postulated that almost any design
patterns could be used together to solve complex architectural problem
problems.
How patterns work together will be discussed later in this book. For . . . and may be
now, I want to focus on his claim that patterns are useful to solve combined to solve
specialized problems. complex problems
Well, in the early 1990s some smart developers happened upon Adapting Alexander
Alexander’s work in patterns. They wondered if what was true for for software
architectural patterns would also be true for software design.5
• Were there problems in software that occur over and over again
that could be solved in somewhat the same manner?
The group felt the answer to both of these questions was “unequiv-
ocally yes.” The next step was to identify several patterns and
develop standards for cataloging new ones.
5. The ESPRIT consortium in Europe was doing similar work in the 1980s. ESPRIT’s
Project 1098 and Project 5248 developed a pattern-based design methodology
called Knowledge Analysis and Design Support (KADS) that was focused on pat-
terns for creating expert systems. Karen Gardner extended the KADS analysis
patterns to object orientation. See Gardner, K., Cognitive Patterns: Problem-Solving
Frameworks for Object Technology, New York: Cambridge University Press, 1998.
78 Part III • Design Patterns
The Gang of Four Although many people were working on design patterns in the
did the early work early 1990s, the book that had the greatest influence on this fledging
on Design Patterns community was Design Patterns: Elements of Reusable Object-Oriented
Software6 by Gamma, Helm, Johnson, and Vlissides. In recognition
of their important work, these four authors are commonly and
affectionately known as the Gang of Four.
It is important to realize that the authors did not create the patterns
described in the book. Rather, the authors identified these patterns
as already existing within the software community, patterns that
reflected what had been learned about high-quality designs for spe-
cific problems (note the similarity to Alexander’s work).
Today, there are several different forms for describing design pat-
terns. Since this is not a book about writing design patterns, I will
not offer an opinion on the best structure for describing patterns;
however, the following items listed in Table 5-2 need to be included
in any description.
6. Gamma, E., Helm, R., Johnson, R., Vlissides, J., Design Patterns: Elements of
Reusable Object-Oriented Software, Reading, Mass.: Addison-Wesley, 1995.
Chapter 5 • An Introduction to Design Patterns 79
Item Description
GoF Reference Where to look in the Gang of Four text to get more
information.
Consequences/Forces
Design patterns give However, there is a third reason to study design patterns:
a higher perspective
on analysis and Patterns give you a higher-level perspective on the prob-
design lem and on the process of design and object orientation.
This frees you from the tyranny of dealing with the
details too early.
By the end of this book, I hope you will agree that this is one of the
greatest reasons to study design patterns. It will shift your mindset
and make you a more powerful analyst.
7. This section is inspired by a talk given by Ralph Johnson and is adapted by the
authors.
Chapter 5 • An Introduction to Design Patterns 81
Consider two carpenters discussing how to build the drawers for Example of the
some cabinets. tyranny of details:
carpenters making a
Carpenter 1: How do you think we should build these set of drawers
drawers?
Now, your job is to figure out what they are talking about!
Isn’t that a confusing description? What is Carpenter 2 prescribing? The details may
The details certainly get in the way! Let’s try to draw out his confuse the solution
description.
This sounds like so Doesn’t this sound like code reviews you have heard? The one
many code reviews: where the programmer describes the code in terms such as,
details, details,
details And then, I use a WHILE LOOP here to do . . . followed
by a series of IF statements to do . . . and here I use a
SWITCH to handle . . .
You get a description of the details of the code, but you have no
idea what the program is doing and why it is doing it!
Carpenters do not Of course, no self-respecting carpenter would talk like this. What
really talk at that would really happen is something like:
level of detail
Carpenter 1: Should we use a dovetail joint or a miter
joint?
When the carpenter speaks of a miter joint, he or she has the fol-
lowing characteristics of the solution in mind:
Patterns help us see This is one thing I mean when I say that patterns can help raise the
the forest and the level of your thinking. You will learn later in the book that when
trees you raise your level of thinking like this, new design methods
become available. This is where the real power of patterns lies.
Other Advantages to
Studying Design Patterns
Improve team My experience with development groups working with design
communications and patterns is that design patterns helped both individual learning
individual learning and team development. This occurred because the more junior
team members saw that the senior developers who knew design
patterns had something of value and these junior members
wanted it. This provided motivation for them to learn some of
these powerful concepts.
Chapter 5 • An Introduction to Design Patterns 85
Most design patterns also make software more modifiable. The rea- Improved
son for this is that they are time-tested solutions. Therefore, they modifiability
have evolved into structures that can handle change more readily of code
than what often first comes to mind as a solution.
Design patterns, when they are taught properly, can be used to Design patterns
greatly increase the understanding of basic object-oriented design illustrate basic
principles. I have seen this countless times in the introductory object-oriented
object-oriented courses I teach. In those classes, I start with a brief principles
introduction to the object-oriented paradigm. I then proceed to
teach design patterns, using them to illustrate the basic object-ori-
ented concepts (encapsulation, inheritance, and polymorphism).
By the end of the three-day course, although we’ve been talking
mostly about patterns, these concepts—which were just introduced
to many of the participants—feel like they are old friends.
The Gang of Four suggests a few strategies for creating good object- Adoption of
oriented designs. In particular, they suggest the following: improved strategies—
even when patterns
• Design to interfaces. aren’t present
Another advantage is that design patterns allow you or your team Learn alternatives to
to create designs for complex problems that do not require large large inheritance
inheritance hierarchies. Again, even if design patterns are not used hierarchies
directly, avoiding large inheritance hierarchies will result in
improved designs.
86 Part III • Design Patterns
Summary
In this chapter In this chapter, I described what design patterns are. Christopher
Alexander says “patterns are solutions to a problem in a context.”
They are more than a kind of template to solve one’s problems.
They are a way of describing the motivations by including both
what we want to have happen along with the problems that are
plaguing us.
• Decide whether I have the right design, not just one that works.