Design Patterns Tutorial
Volume Number: 13
Issue Number: 4
Column Tag: Programming Techniques
The Tao of Design
By John Schettino
Using Design Patterns to help reuse the structure of your
code
The Tao
I know what you're thinking— not another "Eastern Mysticism as applied to Object
Oriented Programming" article. Don't worry, although I'm going to begin with an
eastern mysticism example, I'll spend most of the article presenting real examples of
design patterns in C++. You're now probably wondering what is a design pattern? To
answer that question we need to consider Tai Chi, Fibonocci, and the Tao.
I used to study Tai Chi, a martial art and a philosophy towards life. As I was learning
the "form" (a series of body positions) my teacher would often tell us stories that
illustrated the principals of Tai Chi. One day he began talking about Fibonocci. This
mathematician and philosopher studied Nature and numbers. What he discovered, and
Mandelbrot later refined, was that nature was really into reuse. There are patterns to
both numbers and organisms. My teacher related Fibonocci sequences to Tai Chi by
pointing out the many patterns within the form: how large movements are built from
similar small movements in a Fibonocci sequence, how positions repeated in slight
variations, and so on. It was all rather enlightening. What of the Tao? The Tao-te Ching
roughly translates into "the way." It is the essential unifying element of all that is. It
is a way of perception and living, as are using design patterns. Once you grasp the
concept of design patterns you will see how and where to apply them. In essence, you'll
be living the Tao of Design.
Design patterns are an attempt to express the sameness of design solutions in object
oriented programs. It is really a way of thinking about a problem and the design of that
problem's solution. Think about your own programming style. A large portion of it is
most likely the way you structure your solutions, rather than the minute details of
your code. If you could reuse the structure from one project to the next, you would
gain all of the benefits of reuse (savings of time, higher quality, etc.) on a larger scale
than simply reusing classes or functions. This is the central premise behind the book
"Design Patterns" by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides.
The book is an attempt to present the concept behind design patterns and describe
several patterns that you can use. It is not intended as an introductory text on object
oriented programming! The authors state up front that you should be proficient in at
least one object oriented language (the examples are in Smalltalk and C++), and that
you have object oriented design experience. To understand the book you must be
familiar with such concepts as types, polymorphism, and interface versus
implementation inheritance. This is not a light book. If you don't know object oriented
design this is not the place to start. If, on the other hand, you have been involved with
object oriented design and/or programming and feel that you could get more reuse or
elegance out of your efforts, this is precisely the place to start.
The book begins with a description of the concept of design patterns and the motivation
for using them. The idea is sound. We all reuse our own personal set of design solutions
that we've built up through years of experience. We should study those solutions to
discover the patterns within them, and then formalize those patterns into reusable
classes.
What the authors propose is that we develop a common way of referring to and
specifying designs. Not only do they propose it, they then give a detailed example of
how to apply design patterns in an application. The example is somewhat academic, as
is the entire book, but don't let that stop you from learning from it. Following the
example are the 23 patterns themselves. They are organized into three categories:
creational, structural, and behavioral. Each category contains several patterns.
Creational patterns abstract the instantiation process. When you are designing
complex object oriented systems you often rely on composite objects along with
class-based inheritance. Creational patterns are used to delegate instantiation,
abstract behavior, and hide instantiation and composition details.
Structural patterns focus on the composition of classes and objects into larger
structures. They deal with run-time compositions that are more dynamic than
traditional multiple inheritance, object sharing and interface adaptation, and dynamic
addition of responsibilities to objects.
Behavioral patterns deal with encapsulating algorithms and managing or delegating
responsibility among objects. They focus more on communication and interaction,
dynamic interfaces, object composition, and object dependency.
Many design problems fall into one or more of these categories. Typically, one must
create several objects to represent the data in a design and decide how those objects
interact and behave. When design patterns are applied to these problems you create
more general, extensible, and reusable solutions than you might otherwise have done.
You're not doing this extra work simply to feel good about yourself. You're helping
yourself in the future when you must maintain this code or solve a similar problem.
Each pattern is described from several perspectives:
• Intent: a description of the purpose the pattern serves.
• Also Known As: common names used for the pattern, such as Wrapper instead of
Adapter.
• Motivation: why you would use the pattern.
• Applicability: when to use the pattern.
• Structure: one or more diagrams describing the classes in the pattern and how
they relate.
• Participants: a description of each of the classes used in the Structure section.
• Collaborations: a description of the run-time interaction between instances of the
classes used in the Structure section.
• Consequences: a description of the trade-offs and issues with using the pattern.
• Implementation: a description of the implementation issues. The authors tell you
what to avoid and what to consider when implementing a pattern.
• Sample Code: implementations of classes illustrating the pattern in Smalltalk and
C++.
• Know Uses: a description of some well-known commercial and academic
applications, toolkits, and languages that use the pattern.
• Related Patterns: a list of the patterns that may be implemented by, use, or
interact with this pattern.
The authors close each section with a discussion of the type of problems the category of
patterns is attempting to solve. This section is most useful to those with limited design
experience. Old hands will find that they can think of many situations where they could
(or perhaps already do) apply the patterns.
You'll gain three new skills from reading the book. First, you'll know more about
design patterns themselves. You'll even know how to identify and classify them. Second,
you'll know the patterns and classes presented. This can act as a standard nomenclature
when you're discussing designs with other programmers. Third, you'll have 23
patterns to use in your own programs. Some of these you will have already discovered
yourself while others will probably be complete revelations to you.
Patterns in Action
Let's put the principals of design patterns into action. Illustrating all 23 patterns
presented in the book is beyond the scope of this article, but I'll present an example
that applies a pattern from each category to help motivate you to explore the topic
further. Since these are design patterns, we need a problem to solve. I'm going to take
the easy road and select a completely made-up problem - tree manipulation. We will
build two tree structures containing file names found in a Macintosh file directory and
perform some operations on it.