designPatterns10yearsLater Gamma2002
designPatterns10yearsLater Gamma2002
designPatterns10yearsLater Gamma2002
Design Patterns
Erich Gamma
Technical Director.
Object Technology International. Zurich
erich_gamma @oti.com
Doctorate in Informatics.
University of Zunch
Union Bank of Switzerland
Tahgent
Major contribution: design patterns
Current interests:
Java development tools. frameworks.
building large object-oriented systems
Erich Gamma
Design patterns have changed the way software developers design abJect-
oflented systems. Rather than rediscovering solutions to recurring design
problems over and over again, developers can now refer to a body of litera-
ture that captures THE best practices of system design. This article looks
back to where design patterns came from, shows design patterns in action,
and provides an overview of where patterns are today.
Some History
looking back, we can see that patterns came to us via two routes: the
pattern language route and the micro·archltecture route.
Ward Cunningham and Kent Beck had Alexander's books on their shelves
long before they recognized the applicability of hiS Ideas to software. By
1987 they began introducing those ideas to the object-oriented program-
ming community [25) . Ward and Kent were successful in generating in-
terest in Alexander's ideas, but it all sounded rather theoretical to software
developers. What was lacking were concrete examples for software de-
velopment.
We were not shy about showing our catalogue to people. Surprisingly, most
of the feedback we got from experts was along the lines of, "Oh, I've done
that " Such feedback encouraged us to continue, but we also realized that
we should leverage insights from some of these experts to move forward
faster.
As a first step, we Infected John Vlissldes with the idea John was a contri -
butor to InterViews (20], a C++ library that was popular at the time.
InterViews and ET++ were competitors, giving nse to debates over which
system's way of solving problems was best. A lesson of these diSCUSSions
was the Importance of documentmg not just one solution but multiple
alternatives, along with their trade-offs.
Around the same time, another contributor was making patterns more con-
crete. Jim Copl ien collected and published a catalogue of C++ "idioms" -
small-scale patterns applicable to a particular programming language [9] .
As useful as idioms are, though, one of our goals was language neutrality.
We did not want to limit ourselves to one language, no matter how popu -
lar; we wanted to capture problems and solutions that applied across
obJect-oriented languages.
A summary of the group's initial work was presented at ECOOP 114]. That
effort convinced us that we cou ldn't do justice to the to pic with anythmg
less than a book-length work. We also quickly learned that you can't get a
pattern right the first time. It took us more than two years and several
rewrites, requiring over 2000 sometimes-heated e-mail exchanges, to ar-
rive at the catalog of 23 patterns we finally published 115].
While we GoF worked on our patterns, Kent Beck began forming the
Hillside Group, which was instrumental in the pattern community's forma-
tion . In particular, Hillside was the catalyst for the various Partern lan-
guages of Programs (PloP) conferences. The PloP charter was to create
a new literature of what is explicitly not new, but works in practice. The
centerpiece of a PloP conference is its Writers' Workshops. Instead of one
or more authors presentmg their work to a large audience, they listen
silently as a small group discusses each pattern and debates its strengths
and weaknesses. The products of PloPs are carefully reviewed and revised
patterns [10, 16, 21 , 30] from many different domainS.
Several other pattern books appeared around the time of the first few
PlOPs, and they did much to popularize patterns. Examples include Pattern-
Oriented Software Architecture 17], Smalltalk Best Practice Patterns [5], or
the semmal work on analysis patterns (11)
692 Erich Comma
This short history would be Incomplete without mention of the GoFs 1999
indictment by the International Tribunal for the Prosecution of Crtmes
Against Computer Science for crimes too numerous to Itst here [28]. A
show trial held at OQPSLA that yea r had a predictable outcome, though
only one of the defendants received a Jail sentence. He subsequently es-
caped and remains at large.
Frameworks
Design patterns have their roots In object-oriented frameworks A frame -
work is a set of customlzable, cooperating classes that Implements a reusa -
ble solution to a given problem. To make a framework reusable, It'S Impera-
tive to design -in flexibility and extensibility. Frameworks are therefore
design-dense, making them a rich source of design solutions. Many pattern
authors have experience In framework development.
DeSign patterns are interesting 10 this context because they let you app ly
inSights garned 10 framework deSign to day-to-day application develop-
ment. Design patterns are more abstract than frameworks; they are mainly
prose descnptlons with few opportunities for code reuse. As a consequence,
patterns are highly "portable" and can be adapted and tweaked when
they are applied. Patterns are also smaller architectural building blocks
than frameworks. You can comb1Oe them 10 myriad ways.
Mature frameworks use many patterns. The same Isn't typically true
of applications, because they do not require the flexibility of a framework.
An application's pattern density is generally lower than a framework's.
694 Erich Gamma
Modeling Tests
Let's examine the design of modeling tests using patterns as a guide. First
we need an abstraction for the tests themselves. JUnit should be able to
run a test without knowing any details about it. Scanning through our
patterns' intents, we encounter this one: "Encapsulate a request as an
object, thereby letting you parameterize clients with different requests,
queue or log requests, and support undoable operations." That's the Intent
of the Command pattern. While we're not particularly Interested In the
undo aspect, the idea of representing a "run test" request as an object
will effectively hide details of the test from JUnit - and make the tests
extensible as well.
TestCase
run()
The Template Method pattern can do just that. Its intent is to, "Define the
skeleton of an algorithm 10 an operation, deferring some steps to subclas-
ses. Template Method lets subclasses redefine certain steps of an algorithm
without changing the algorithm's structure." The pattern shows us how to
turn ru n into a template method made up of primitive methods setUp,
runTest, and t earDown. Figure 2 shows the result
Design Patterns 695
TestCase
The next design problem has to do with how JUnit supports suites of tests.
Tests should be composable with other tests for two reasons. First. you
might want to test scattered modifications all at once. Second. you might
want to combine your tests with those of other developers.
The power of the Composite pattern stems from the conformance of the
TestSuite and TestCase interfaces - a TestSuite is a TestCase. How you
express this conformance depends on your programming language. In Java,
you introduce a common interface; in Smalltalk, you simply define the
same method selectors.
This conformance lets you assemble TestSUIte objects recu rsively to form
larger suites. In other words, clients no longer have to distinguish between
a single test and many tests. Figure 3 illustrates the Introduction of
Composite.
run(TestResult)
,j '.
r-------·--------,
I
I
I
I I
TestCas e TestSuite
Composite: Leaf
run(TeslResull) run(TesIResult) fTests
addTest(Test)
At the end of the day, you run tests to see whether they succeed or fall.
xecuting tests. Smal/talk Best
To do that, we need to collect the results o f e
Practice Patterns [5] has a pattern called »Collectlng Parameter"
for t his purpose. When you need to collect results over several methods, the
pattern suggests adding a parameter to each method and passing In an
object In which to hold the results. In our design, then, we create a new
object, TestResult, that holds the results of running tests (Figure 4) .
Test.Case
TestResult run(TestResult)
run Test()
Collecting Parameter setUp()
tearDown()
Our design is coming along nicely, but the framewo rk remains harder
to use than It could be. Developers must create a new subclass of TestCase
for every test. Loads of classes, each im plementing a lone method, are a
maintenance burden .
We can always put multiple tests into the class to reduce subclassing. But
we still have a problem: JUnit offers just one method, runTest, for carrying
out a test.
The Adapter pattern comes in handy here. Its intent is to "convert the
interface of a class into another interface." Of particular interest IS an
implementation variation called pluggable adapters. A pluggable adapter
builds interface adaptation into a class. The aspect we want to make plug-
gable IS the test method that TestCase executes.
Test Composite:Component
run(TestResuJI)
I'
j-
I
r- - -----~--------,
I I
TestCase TestSuite
Accordingly, JUnit lets you specify a test method name when creating
a TestCase. The runTest method can then Invoke that method uSing Java
reflectIOn Figure 5 Illustrates the final design of the JUnit framework.
Postmortem
Brrdye AblMilc:lIolI
Observor SubJec.t
· ..L
Bridge Implf!'mfl' nlo1l1on
"'!b@i'AM4 ~
~
It takes time, but design patterns eventually emerge from challenging new
problem domains where design expertise is especially appreciated. For
example, concurrent programming has many degrees of freedom beyond
conventional sequential programming. This relatively new and complicat-
ed problem domain is already covered by patterns for concurrent Java pro-
gramming [19] and for programming networked objects (24). Another
recent example is the work on J2EE patterns for Java enterprise applica-
tion development [1] . You can expect this trend to continue and new
pattern catalogs to be developed as new technologies emerge.
Patterns are now used to teach software design and programming at many
universities. They have found their way into the APls of today's software
platforms, most notably Java and .NET. (Figure 6 depicts an example from
the Java platform .)
Meanwhile. design notations and case tools have incorporated pattern sup-
port. UML [8] can document the structural aspects of design patterns with
so-called parameterized collaborations. Whenever a pattern is applied,
actual classes are substituted for parameters in a corresponding pattern
definition. But such diagrams cover on ly the structural aspects of a pattern:
its most important parts - the trade-ofts. implementation hints. and exam -
ples - have to be documented in text as before.
Conclusion
Design patterns have been successful and influential because they tackle
design problems In a way many developers can appreciate. Here are my
nominees for the four biggest benefits of deSign patterns
Design patterns are tools, not rules Applying a pattern Involves tweaking
and adapting it to your needs. You can't turn off your brain when you
use patterns. You have to choose them Wisely and then decide how to use
them or - perhaps more importantly - how not to.
The most powerful patterns are the ones you've Internalized, and they're
not easy to come by. You can't absorb patterns solely by reading a book
or by attending a course. Unless you have experienced a deSign problem
firsthand, you cannot appreciate the solution a pattern deSCrIbes.
Acknowledgements
Thanks to John Vlissi des for carefully reading and many suggesting
corrections to improve the readability of this article.
References
(II Oeepak. A. J Crupi. 0 Malks. Core J2EE Patterns: Best Pracllces and DeSign Strategies
Prentice Hall. 2001
121 Alexander, C.. S Ishikawa, M.SllverstelO. et al. A Pattern language. Oxford University
Press. New York 1977
[3] Alexander, C The Timeless Way of Building Oxford University Press. New York, 1979
[41 Beck, K , J 0 Coplten. R. Crocker. et al. industrial expellence With deSign patterns
Proceed lOgs of the 1Bth International Conference on Software Engineering (pp 103· 114).
Berltn. Germany, March 1996.
[5J Beck, K Smalltalk Best Practice Patterns PrentICe Hall. 1996
16) Beck, K. Extreme ProgrammlOg ExplalOed' Embrace Change. Addlson·Wesley. ReadlOg.
MA, 1999
[71 Buschmann, F. R, Menunter, H, Rohnert, M.Stal Pattern·Orlented Software Architecture ·
A System of Patterns Wiley, ChIChester. England. 1996
181 Booch. G, I Jacobson. J, Rumbaugh The Untfled Modeling Language User GUide
Addlson ·We~ley,ReadlOg, MA, 1998,
[91 Cophen, J Advanced CH Programming Styles and Idioms. AddlsonWesley. ReadlOg. MA.
1992
[IOJ Cophen. )O . D.C. Schmidt (Eds.) Pattern Languages of Program DeSign.
Add lsonWesley (Software Patterns Selles), ReadlOg, MA. 1995
700 Erich Gamma
[111 Fowler, M,Analy~i~ Patterns Reu~ble Object Models AddlsonWesley, Readmg. MA,
1997,
[12) Fowler, M .Refactormg ; Improvmg the DeSign of EXlsllng Code. Addlson .Wesley, Reading .
MA, 1999
[13[ Gamma E ObJec\·Ollenled Software Development Based on ET ++' DeSign Patterns, Clas~
library, Tools (In German) Doctoral thesIs, University of ZUllch, Insmut fur Informalik,
1991
[14] Gamma E, R.Helm, R Helm, R Johnson. and J Vllssldes. DeSign Patterns' Abstraction
and Reuse of ObJect-Onented DeSig n ECOOP '93, lecture Notes In Computer SCience 707
Springer, Berlin Heidelberg New York, p.406 If.
[IS) Gamma, E, RHelm, R Johnson, J.Vlissldes. DeSign Patterns. Elements of Reu~ble
ObJect·Onented Software. Addlson .Wesley, Reading, MA, 1995
[16] Harrison, N., Bfoote, H Rohnert (Eds.) Pattern Languages of Program DeSign 4 Addison -
Wesley (Software Patterns Serres), Reading. MA, 2000
117) JavaSolt, In t. Java Development Kit Mountam View. CA, 1997
[18[ Johnson, R.Documenting Frameworks USing Patterns In Proc. OOPSLA '92, pages 63 ·76.
Vancouver BC, Oct 1992.
[19[ Lea, D.Concurrent Programmmg in Java, DeSign Prmciples and Patterns, 2nd edition.
Addlson ·Wesley, Reading , MA, 1999
[201 linton, M ..J Vhssldes, and P Calder. Composmg User Interlaces With InterViews.
Computer 22(2)8-22, 1989
(21 [ Martin. R. 0 Riehle, and F. Buschmann (Eds). Pattern Languages of Program DeSign 3.
AddlsonWesley, Reading, MA, 1998.
[22) Prechelt. L An experiment on the usefulness of deSign patterns: Detailed deSCription and
evaluation. Technical Report 9/ 1997, University of Karlsruhe. Germany, June 1997
123) Rising. L The Pattern Almanac 2000 Addison -Wesley, Reading, MA, 2000.
[24) Schmidt. D.• M. Stal, H Rohnert. F Buschmann . Pattern-Onented Software Architecture ·
Pattern~ for Concurrent and Networked Objects Wiley and Sons Ltd., Chichester. England.
2000
[25) http;//c2 .com/dot./oopsla87 html
[261 http.!lhlllslde.net/patterns
[27) http.!/www.Junrtorg
(28) http.!Iwww.acm.org/sigplan/oopsla/oopsla99/2_ap/tech/2dla_gang.htm.
(29) Cotter, S, M. Potel. InSide Tallgent Technology
Add,sonWesley, Readmg, MA, 1995.
(30) Vhssldes, J, J Cophen, and N. Kerth (Eds.) Pattern Languages of Program Design 2.
Addison-We5Iey, Readmg, MA, 1996.
[31] Wemand. A., E Gamma. and R. Marty. ET ++- An object-oriented application framework 10
C++. In Object-Oriented Progra mmmg Systems, Language5, and Applications Conferenr;e
Proceed lOgS, pages 46-57, San Diego, CA. ACM Press. 1988