Design Patterns: Elements of Reusable Object-Oriented Software 1st Edition - Ebook PDF All Chapters Instant Download
Design Patterns: Elements of Reusable Object-Oriented Software 1st Edition - Ebook PDF All Chapters Instant Download
com
https://ebookluna.com/download/design-patterns-elements-of-
reusable-object-oriented-software-ebook-pdf/
OR CLICK HERE
DOWLOAD NOW
https://ebookluna.com/download/c-programming-an-object-oriented-
approach-1e-ise-ebook-pdf/
ebookluna.com
https://ebookluna.com/product/ebook-pdf-microsoft-visual-c-an-
introduction-to-object-oriented-programming-7th-edition/
ebookluna.com
https://ebookluna.com/product/ebook-pdf-elements-of-visual-design-in-
the-landscape-3rd-edition/
ebookluna.com
https://ebookluna.com/product/ebook-pdf-machine-elements-in-
mechanical-design-6th-edition/
ebookluna.com
(eBook PDF) Elements and Principles of 4D Art and Design
by Ellen Mueller
https://ebookluna.com/product/ebook-pdf-elements-and-principles-
of-4d-art-and-design-by-ellen-mueller/
ebookluna.com
https://ebookluna.com/download/elements-of-structures-and-defects-of-
crystalline-materials-ebook-pdf/
ebookluna.com
https://ebookluna.com/download/elements-of-structures-and-defects-of-
crystalline-materials-ebook-pdf-2/
ebookluna.com
https://ebookluna.com/download/environmentally-oriented-modernization-
of-power-boilers-ebook-pdf/
ebookluna.com
https://ebookluna.com/product/ebook-pdf-computer-organization-and-
design-arm-edition-the-hardware-software-interface/
ebookluna.com
Design Patterns
Elements of Reusable Object-Oriented Software
Produced by KevinZhang
Design Patterns: Elements of Reusable Object-Oriented Software
Contents
Preface to CD ........................................................ 5
Foreword ............................................................. 9
1 Introduction ...................................................... 11
1.1 What Is a Design Pattern? ...................................... 12
1.2 Design Patterns in Smalltalk MVC ............................... 14
1.3 Describing Design Patterns ..................................... 16
1.4 The Catalog of Design Patterns ................................. 18
1.5 Organizing the Catalog ......................................... 21
1.6 How Design Patterns Solve Design Problems ...................... 23
1.7 How to Select a Design Pattern ................................. 42
1.8 How to Use a Design Pattern .................................... 44
2
Design Patterns: Elements of Reusable Object-Oriented Software
3
Design Patterns: Elements of Reusable Object-Oriented Software
4
Design Patterns: Elements of Reusable Object-Oriented Software
Preface to CD
Now, three years after its debut, we find ourselves both grateful andthrilled
by how the book has been received. Lots of people use it.Many tell us the patterns
have helped them design and build bettersystems. Many others have been inspired
to write their own patterns,and the pool of patterns is growing. And many have
commented on whatmight be improved about the book and what they would like to
see inthe future.
A recurring comment in all the feedback has been how well-suited thebook is to
hypertext. There are numerous cross-references, andchasing references is
something a computer can do very well. Sincemuch of the software development
process takes place on computers, itwould be natural to have a book like ours
as an on-line resource.Observations like these got us excited about the potential
of thismedium. So when Mike Hendrickson approached us about turning the bookinto
a CD-ROM, we jumped at the chance.
Two years and several megabytes of e-mail later, we're delighted thatyou can
finally obtain this edition, the Design Patterns CD,and put its unique capabilities
to work. Now you can access a patternfrom your computer even when someone has
borrowed your book. You can search the text for key words and phrases. It's also
considerably easier to incorporate parts of it in your own on-line
documentation.And if you travel with a notebook computer, you can keep the
bookhandy without lugging an extra two pounds of paper.
for their development effort andfor patience despite what must appear to have
been our insatiableappetite for revision. A special acknowledgment is due IBM
Research,which continues to underwrite much of this activity. We also thankthe
reviewers, including Robert Brunner, Sandeep Dani, Bob Koss, ScottMeyers, Stefan
Schulz, and the Patterns Discussion Group at theUniversity of Illinois
Urbana-Champaign. Their advice led to at leastone major redesign and several minor
ones.
Finally, we thank all who have taken time to comment on DesignPatterns. Your
feedback has been invaluable to us as we striveto better our understanding and
presentation of this material.
August 1997
6
Design Patterns: Elements of Reusable Object-Oriented Software
Preface to Book
On the other hand, this isn't an advanced technical treatise either. It’s a book
of design patterns that describes simple and elegant solutions to specific problems
in object-oriented software design. Design patterns capture solutions that have
developed and evolved overtime. Hence they aren't the designs people tend to
generate initially. They reflect untold redesign and recoding as developers have
struggled for greater reuse and flexibility in their software. Design patterns
capture these solutions in a succinct and easily applied form.
The design patterns require neither unusual language features nor amazing
programming tricks with which to astound your friends and managers. All can be
implemented in standard object-oriented languages, though they might take a little
more work than ad hoc solutions. But the extra effort invariably pays dividends
in increased flexibility and reusability.
Once you understand the design patterns and have had an "Aha!" (and not just a
"Huh?") experience with them, you won't ever think about object-oriented design
in the same way. You'll have insights that can make your own designs more flexible,
modular, reusable, and understandable—which is why you're interested in
object-oriented technology in the first place, right?
A word of warning and encouragement: Don't worry if you don’t understand this
book completely on the first reading. We didn’t understand it all on the first
writing! Remember that this isn't a book to read once and put on a shelf. We hope
you'll find yourself referring to it again and again for design insights and for
inspiration.
This book has had a long gestation. It has seen four countries, three of its authors'
marriages, and the birth of two (unrelated) offspring.Many people have had a part
in its development. Special thanks are due Bruce Anderson, Kent Beck, and André
Weinand for their inspiration and advice. We also thank those who reviewed drafts
of the manuscript: Roger Bielefeld, Grady Booch, Tom Cargill, Marshall Cline,
Ralph Hyre, Brian Kernighan, Thomas Laliberty, Mark Lorenz, Arthur Riel, Doug
Schmidt, Clovis Tondo, Steve Vinoski, andRebecca Wirfs-Brock. We are also grateful
to the team at Addison-Wesley for their help and patience: Kate Habib,Tiffany
Moore,Lisa Raffaele,Pradeepa Siva, and John Wait.Special thanks to Carl Kessler,
7
Design Patterns: Elements of Reusable Object-Oriented Software
Danny Sabbah, and Mark Wegman at IBMResearch for their unflagging support of this
work.
Last but certainly not least, we thank everyone on the Internet andpoints beyond
who commented on versions of the patterns, offeredencouraging words, and told
us that what we were doing was worthwhile.These people include but are not limited
toJon Avotins,Steve Berczuk,Julian Berdych,Matthias Bohlen,John Brant,Allan
Clarke,Paul Chisholm,Jens Coldewey,Dave Collins,Jim Coplien,Don
Dwiggins,Gabriele Elia,Doug Felt,Brian Foote,Denis Fortin,Ward Harold,Hermann
Hueni,Nayeem Islam,Bikramjit Kalra,Paul Keefer,Thomas Kofler,Doug Lea,Dan
LaLiberte,James Long,Ann Louise Luu,Pundi Madhavan,Brian Marick,Robert
Martin,Dave McComb,Carl McConnell,Christine Mingins,Hanspeter Mössenböck,Eric
Newton,Marianne Ozkan,Roxsan Payette,Larry Podmolik,George Radin,Sita
Ramakrishnan,Russ Ramirez,Alexander Ran,Dirk Riehle,Bryan Rosenburg,Aamod
Sane,Duri Schmidt,Robert Seidl,Xin Shu,and Bill Walker.
August 1994
8
Design Patterns: Elements of Reusable Object-Oriented Software
Foreword
The impact of this work cannot be overstated. As I travel aboutthe world working
with projects of varying domains andcomplexities, it is uncommon for me to
encounter developers whohave not at least heard of the patterns movement. In the
moresuccessful projects, it is quite common to see many of thesedesign patterns
actually used.
With this book, the Gang of Four have made a seminalcontribution to software
engineering. There is much to learnedfrom them, and much to be actively applied.
Grady Booch
Chief Scientist, Rational Software Corporation
9
Design Patterns: Elements of Reusable Object-Oriented Software
Guide to Readers
This book has two main parts. The first part (Chapters 1 and 2)describes what
design patterns are and how they help you designobject-oriented software. It
includes a design case study thatdemonstrates how design patterns apply in practice.
The second partof the book (Chapters 3, 4, and 5) is a catalog of the actual
designpatterns.
The catalog makes up the majority of the book. Its chapters dividethe design
patterns into three types: creational, structural, andbehavioral. You can use
the catalog in several ways. You can readthe catalog from start to finish, or
you can just browse from patternto pattern. Another approach is to study one of
the chapters. Thatwill help you see how closely related patterns distinguish
themselves.
You can use the references between the patterns as a logicalroute through the
catalog. This approach will give you insightinto how patterns relate to each other,
how they can be combinedwith other patterns, and which patterns work well together.
Figure 1.1(page 23) depicts these references graphically.
It's hard to find an object-oriented system that doesn't use at leasta couple
of these patterns, and large systems use nearly all of them.This subset will help
you understand design patterns in particular andgood object-oriented design in
general.
10
Design Patterns: Elements of Reusable Object-Oriented Software
1. Introduction
One thing expert designers know not to do is solve every problem from first
principles. Rather, they reuse solutions that have worked for them in the past.
When they find a good solution, they use it again and again. Such experience is
part of what makes them experts. Consequently, you'll find recurring patterns
of classes and communicating objects in many object-oriented systems. These
patterns solve specific design problems and make object-oriented designs more
flexible, elegant, and ultimately reusable. They help designers reuse successful
designs by basing new designs on prior experience. A designer who is familiar
with such patterns can apply them immediately to design problems without having
to rediscover them.
An analogy will help illustrate the point. Novelists and playwrights rarely design
their plots from scratch. Instead, they follow patterns like "Tragically Flawed
Hero" (Macbeth, Hamlet, etc.) or "The Romantic Novel" (countless romance novels).
In the same way, object-oriented designers follow patterns like "represent states
with objects" and "decorate objects so you can easily add/remove features." Once
you know the pattern, a lot of design decisions follow automatically.
We all know the value of design experience. How many times have you had design
déjà-vu—that feeling that you've solved a problem before but not knowing exactly
where or how? If you could remember the details of the previous problem and how
you solved it, then you could reuse the experience instead of rediscovering it.
However, we don't do a good job of recording experience in software design for
others to use.
11
Design Patterns: Elements of Reusable Object-Oriented Software
None of the design patterns in this book describes new or unproven designs. We
have included only designs that have been applied more than once in different
systems. Most of these designs have never been documented before. They are either
part of the folklore of the object-oriented community or are elements of some
successful object-oriented systems—neither of which is easy for novice designers
to learn from. So although these designs aren't new, we capture them in a new
and accessible way: as a catalog of design patterns having a consistent format.
Despite the book's size, the design patterns in it capture only a fraction of
what an expert might know. It doesn't have any patterns dealing with concurrency
or distributed programming or real-time programming. It doesn't have any
application domain-specific patterns. It doesn't tell you how to build user
interfaces, how to write device drivers, or how to use an object-oriented database.
Each of these areas has its own patterns, and it would be worthwhile for someone
to catalog those too.
Christopher Alexander says, "Each pattern describes a problem which occurs over
and over again in our environment, and then describes the core of the solution
to that problem, in such a way that you can use this solution a million times
over, without ever doing it the same way twice" [AIS+77]. Even though Alexander
was talking about patterns in buildings and towns, what he says is true about
object-oriented design patterns. Our solutions are expressed in terms of objects
and interfaces instead of walls and doors, but at the core of both kinds of patterns
is a solution to a problem in a context.
1. The pattern name is a handle we can use to describe a design problem, its
solutions, and consequences in a word or two. Naming a pattern immediately
increases our design vocabulary. It lets us design at a higher level of
abstraction. Having a vocabulary for patterns lets us talk about them with
our colleagues, in our documentation, and even to ourselves. It makes it
easier to think about designs and to communicate them and their trade-offs
to others. Finding good names has been one of the hardest parts of developing
our catalog.
2. The problem describes when to apply the pattern. It explains the problem
and its context. It might describe specific design problems such as how
to represent algorithms as objects. It might describe class or object
structures that are symptomatic of an inflexible design. Sometimes the
problem will include a list of conditions that must be met before it makes
sense to apply the pattern.
3. The solution describes the elements that make up the design, their
relationships, responsibilities, and collaborations. The solution doesn't
describe a particular concrete design or implementation, because a pattern
is like a template that can be applied in many different situations. Instead,
the pattern provides an abstract description of a design problem and how
a general arrangement of elements (classes and objects in our case) solves
it.
4. The consequences are the results and trade-offs of applying the pattern.
Though consequences are often unvoiced when we describe design decisions,
they are critical for evaluating design alternatives and for understanding
the costs and benefits of applying the pattern. The consequences for
software often concern space and time trade-offs. They may address language
and implementation issues as well. Since reuse is often a factor in
object-oriented design, the consequences of a pattern include its impact
on a system's flexibility, extensibility, or portability. Listing these
consequences explicitly helps you understand and evaluate them.
Point of view affects one's interpretation of what is and isn't a pattern. One
person's pattern can be another person's primitive building block. For this book
we have concentrated on patterns at a certain level of abstraction. Design patterns
are not about designs such as linked lists and hash tables that can be encoded
in classes and reused as is. Nor are they complex, domain-specific designs for
an entire application or subsystem. The design patterns in this book are
descriptions of communicating objects and classes that are customized to solve
a general design problem in a particular context.
A design pattern names, abstracts, and identifies the key aspects of a common
design structure that make it useful for creating a reusable object-oriented design.
The design pattern identifies the participating classes and instances, their roles
and collaborations, and the distribution of responsibilities. Each design pattern
13
Design Patterns: Elements of Reusable Object-Oriented Software
MVC consists of three kinds of objects. The Model is the application object, the
View is its screen presentation, and the Controller defines the way the user
interface reacts to user input. Before MVC, user interface designs tended to lump
these objects together. MVC decouples them to increase flexibility and reuse.
The following diagram shows a model and three views. (We've left out the controllers
for simplicity.) The model contains some data values, and the views defining a
14
Design Patterns: Elements of Reusable Object-Oriented Software
spreadsheet, histogram, and pie chart display these data in various ways. The
model communicates with its views when its values change, and the views communicate
with the model to access these values.
Taken at face value, this example reflects a design that decouples views from
models. But the design is applicable to a more general problem: decoupling objects
so that changes to one can affect any number of others without requiring the changed
object to know details of the others. This more general design is described by
the Observer (page 326) design pattern.
Another feature of MVC is that views can be nested. For example, a control panel
of buttons might be implemented as a complex view containing nested button views.
The user interface for an object inspector can consist of nested views that may
be reused in a debugger. MVC supports nested views with the CompositeView class,
a subclass of View. CompositeView objects act just like View objects; a composite
view can be used wherever a view can be used, but it also contains and manages
nested views.
Again, we could think of this as a design that lets us treat a composite view
just like we treat one of its components. But the design is applicable to a more
general problem, which occurs whenever we want to group objects and treat the
group like an individual object. This more general design is described by the
15
Exploring the Variety of Random
Documents with Different Content
CH APTER VII
THEATRES
The Londoners might change their religion, but they were not
going to change their sports. They were Protestant instead of
Catholic; but they kept up their bear-baiting, their bull-baiting, their
archery, their wrestlings, their fencing, their quarter-staff play, their
running at the quintain, their feats of tumbling, their Morris dances
and mummings, their plays and interludes. But the Reformation
killed the Miracle Play. The play of modern manners, or the tragedy,
or the farce, took the place of the religious play. And instead of
acting on a stage in a churchyard, the players now began to act in
the broad and ample courtyard of the inn, whose galleries afforded
room for people to look on. The authorities looked on the play from
the beginning with eyes of disfavour: the actor was considered a
masterless man; he had no trade; he was a strolling vagabond; he
lived upon the largesse of those who looked on at his performance;
he was a buffoon who would assume any character at will to make
the people laugh and cry; he must be able to dance and posture like
the tumblers on the road. Again, all the idle people in the City
assembled to see the play; all the vicious people crowded to take
advantage of the throng; in the theatre every day arose disorders
and brawls; young men of sober parentage were seduced into
becoming players. Witness the words of Prynne:—
“Our own experience can sufficiently inform us, that plays and
playhouses are the frequent causes of many murders, duels,
quarrels, debates; occasioned sometimes by reason of some
difference about a box, a seat, a place, upon the stage;
sometimes by intruding too boldly into some female’s company;
sometimes by reason of some amorous, scurrilous, or
disgraceful words, that are uttered of or to some female
spectators; sometimes by reason of some speeches or passages
of the play, particularly applied to some persons present or
absent; sometimes by reason of some husband, or co-rival’s
jealousy, or affront, whose wife, or mistress, being there in
person, is perhaps solicited, abused, or jeared at in his
presence; sometimes by reason of the apprentices who resort to
playhouses, especially on Shrove Tuesday; sometimes by means
of other accidents and occasions. Many have been the murders,
more the quarrels, the duels, that have grown from our stage-
plays, whose large encomiums of rash valour, duels, fortitude,
generosity, impatientcy, homicides, tyranny, and revenge, do so
exasperate men’s raging passions, and make them so impatient
of the very smallest injury, that nothing can satisfy, can expiate,
but the offender’s blood. Hence it is that some players, some
play-haunters, now living, not satisfied with the murder of one,
have embrued their barbarous un-christian hands in the blood
of two, of three, if not of four several men. And so far are they
from ruing the odiousness of these their bloody deeds, that they
glory in the number of their murders as the very trophies of
their valour.”
“Plaies are banished for a time out of London, lest the resort
unto them should ingender a plague, or rather disperse it, being
already begonne. Would to God these comon plaies were exiled
altogether, as seminaries of impiety, and their theatres pulled
downe, as no better then houses of bawdrie. It is an evident
token of a wicked time when plaiers waxe so riche that they can
build suche houses. As moche I wish also to our comon beare-
baitings used oin the sabaothe daies.” (Holinshed’s Chronicles.)
Since the players could act no more in the City, there was nothing
for them but to go outside. In 1574, James Burbage and some of
the Earl of Leicester’s Company obtained the Queen’s license to act
plays in any part of England. After receiving this license Burbage
proceeded to build the first theatre, the house called simply “The
Theatre.” This theatre was built outside the jurisdiction of the City,
close to the remains of the Holywell Priory. After the Dissolution the
church of this House was pulled down with most of the buildings.
Houses were built upon its site, and the ruins themselves gradually
disappeared. At the south-west of these ruins, on a site now marked
by Dean’s Mews, Holywell Lane, Burbage built his theatre at a cost
of £600, the money being advanced by his father-in-law. The theatre
was in shape either circular or oval, probably the former. It was built
for all kind of shows and entertainments. If a large space was
wanted the whole of the area could be taken by the performers;
raised galleries ran round the house; for the performance of a play, a
stage was erected in the middle; from the nature of the case there
could be no question of any scenery. The house was built of wood
and is said to have been handsomely decorated; the central area
was without a roof. There were troubles and quarrels about the
lease of the house, which was taken down in the year 1598–99. The
wood and timber of which the house was built were removed to
Bankside, where they were used for the erection of the Globe
Theatre.
The second theatre of London was that called The Curtain. It is a
fact which illustrates the popularity of Finsbury Fields as a place of
resort that there should have been a second theatre erected so close
to the first. The Curtain Theatre was built on the south side of
Holywell Lane, Shoreditch. In the house, too, feats of arms, sword-
play, quarter-staff, and other games took place.
The third theatre (if we count The Globe as a continuation of The
Theatre) was The Fortune, built near Golden Lane, Cripplegate.
The strongest charge against the theatres was the license allowed
to the clowns or jesters, who between the pieces, or between the
Acts, played “jigs” or “drolls” accompanied by songs and dances, and
impromptu jokes which were topical, and, as may be imagined,
broad and coarse. We may easily imagine that the civic authorities,
the preachers, and the pamphleteers, who were always assailing the
player and driving him from place to place, were not spared when
the Clown had the stage all to himself, with hundreds of grinning
faces in front of him, all of whom were egging him on with laughter
and applause to say or do something more outrageous still, and
loved nothing so much as to see before them acted to the life some
sour Puritan who could see only “filthie and beastlie” stuff in the
noblest play by Shakespeare, or in any sport.
BANKSIDE, SOUTHWARK, IN 1648, WITH A VIEW OF
HOLLAND’S LEAGUER, ONE OF THE ANCIENT STEWS OR
LICENSED BROTHELS SUPPRESSED DURING THE REIGN OF
HENRY VIII.
Another favourite place of resort for the citizens, especially for the
more riotous sort, was Southwark, with its raised river-wall or
Bankside; its numerous inns and taverns; its low-lying fields and its
various amusements. There were amphitheatres for bear- and bull-
baiting; in the High Street itself there was a ring for the bull; in Paris
Gardens, on the east side of Blackfriars Bridge, were kept bears and
dogs for the favourite, almost the national, amusement; there was a
kind of sanctuary in Southwark: here were allowed to reside the
“Flemish Frows” still, in spite of Henry the Seventh’s suppression;
here were held May Day games; here was held every year the
pageant of St. George’s Day; and here, in the time of Henry VIII.,
were collected together idlers, vagabonds, and rogues in great
numbers. In this place, the resort of all the young bloods and the
wild element of London, the players settled down in force. The Rose,
The Hope, The Globe, The Swan, all built about the same time, show
the steady popularity of the Drama, in spite of the Puritanic attacks
upon it, which seem to have done it no manner of harm.
At one end of Bankside stood the ruins of the Monastic House and
the Clink Prison; then followed a single row of houses, at the back of
which were the Bull-Baiting Ground and the Bear Garden; then the
theatres already mentioned; also the Falcon Tavern, and Paris
Gardens. All these places were built on a low-lying and marshy
ground planted thickly with trees, intersected with ponds, ditches,
and running streams—for instance, the Pudding Mill stream ran
round two-thirds of Paris Gardens. For an account of the interior of a
theatre and the presentation of a play I quote an imaginary account,
in my own words:—
In this way the mind of the audience was prepared for what was
to follow. We needed no play-bill. The palace before us could be
no other than Priam’s Palace. If there was a field with tents, it
must be the battle-field and the camp of the Greeks; if there
was a wall, it must be the wall of Troy. And though the scenery
was rough, it was enough. One wants no more than the
unmistakable suggestion; the poet and the actor find the rest.
Therefore, though the intrusive gallants lay on the stage;
though Troilus was dressed in the armour of Tudor time, and
Pandarus wore just such a doublet as old Stow himself, we were
actually at Troy. The boy who played Cressida was a lovely
maiden. The narrow stage was large enough for the Council of
Kings, the wooing of lovers, and the battle-field of heroes.
Women unfaithful and perjured, lovers trustful, warriors fierce,
the alarms of war, fighting and slaying, the sweet whispers of
love were drowned by the blare of trumpets; the loss of lover
forgotten in the loss of a great captain; and among the warriors
and the kings and the lovers, the creeping creatures who live
upon the weaknesses and the sins of their betters, played their
parts upon these narrow boards before a silent and enraptured
house. For three hours we were kept out of our senses. There
was no need, I say, of better scenery: a quick shifting of the
canvas showed a battle-field, and turned the stage into a vast
plain covered with armies of Greeks and Romans. Soldiers
innumerable, as thick as motes in the sun, crossed the stage
fighting, shouting, challenging each other. While they fought,
the trumpets blew and the drums beat, the wounded fell, and
the fight continued over these prostrate bodies till they were
carried off by their friends. The chiefs rushed to the front,
crossed swords, and rushed off again. ‘Come both you cogging
Greeks!’ said Troilus, while our cheeks flushed and our lips
parted. If the stage had been four times as broad, if the number
of men in action had been multiplied by ten, we could not have
felt more vividly the rage, the joy, the madness of the battle.
When the play was finished, the ale, the apples, and the nuts
were passed round, and the noise began again. Then the clown
came in and began to sing, and the music played—but oh, how
poor it seemed after the great emotions of the play! The old
man plucked me by the sleeve and we went out, and with us
most of the better sort.” (London, pp. 237–239.)
From the Panorama of “London, Westminster, and Southwark, in 1543.” By Anthony Van den
Wyngaerde. (Sutherland Collection, Bodleian Library, Oxford.) For continuation see pp. 218 and
235.