Software Architecture Design Patterns in Java 1st Edition Partha Kuchana download
Software Architecture Design Patterns in Java 1st Edition Partha Kuchana download
https://ebookname.com/product/software-architecture-design-
patterns-in-java-1st-edition-partha-kuchana/
https://ebookname.com/product/software-architecture-design-
patterns-in-java-1st-edition-partha-kuchana/
https://ebookname.com/product/patterns-in-java-a-catalog-of-
reusable-design-patterns-illustrated-with-uml-2nd-edition-
volume-1-grand/
https://ebookname.com/product/integration-ready-architecture-and-
design-software-engineering-with-xml-java-net-wireless-speech-
and-knowledge-technologies-1st-edition-jeff-zhuk/
https://ebookname.com/product/music-and-sound-in-documentary-
film-1st-edition-holly-rogers/
Principle Advancements in Database Management
Technologies New Applications and Frameworks 1st
Edition Keng Siau
https://ebookname.com/product/principle-advancements-in-database-
management-technologies-new-applications-and-frameworks-1st-
edition-keng-siau/
https://ebookname.com/product/panama-patricia-katzman/
https://ebookname.com/product/nanoparticle-drug-delivery-systems-
for-cancer-treatment-1st-edition-hala-gali-muhtasib/
https://ebookname.com/product/spiritual-assessment-in-social-
work-and-mental-health-practice-david-hodge/
https://ebookname.com/product/user-acceptance-testing-a-step-by-
step-guide-1st-edition-brian-hambling/
The Handbook of Optical Communication Networks 1st
Edition Mark B. Desman
https://ebookname.com/product/the-handbook-of-optical-
communication-networks-1st-edition-mark-b-desman/
Software
Architecture
Design Patterns
in Java
AUERBACH PUBLICATIONS
www.auerbach-publications.com
To Order Call: 1-800-272-7737 • Fax: 1-800-374-3401
E-mail: orders@crcpress.com
© 2004 by CRC Press LLC
Software
Architecture
Design Patterns
in Java
Partha Kuchana
AUERBACH PUBLICATIONS
A CRC Press Company
Boca Raton London New York Washington, D.C.
Kuchana, Partha.
Software architecture design patterns in Java / Partha Kuchana.
p. cm.
Includes bibliographical references and index.
ISBN 0-8493-2142-5 (alk. paper)
1. Java (Computer program language) 2. Computer Software. 3. Computer architecture.
4. Software patterns. I. Title.
QA76.73.J38K83 2004
005.13′3—dc22 2003070897
This book contains information obtained from authentic and highly regarded sources. Reprinted material is quoted
with permission, and sources are indicated. A wide variety of references are listed. Reasonable efforts have been
made to publish reliable data and information, but the author and the publisher cannot assume responsibility for the
validity of all materials or for the consequences of their use.
Neither this book nor any part may be reproduced or transmitted in any form or by any means, electronic or
mechanical, including photocopying, microfilming, and recording, or by any information storage or retrieval system,
without prior permission in writing from the publisher.
The consent of CRC Press LLC does not extend to copying for general distribution, for promotion, for creating new
works, or for resale. Specific permission must be obtained in writing from CRC Press LLC for such copying.
Direct all inquiries to CRC Press LLC, 2000 N.W. Corporate Blvd., Boca Raton, Florida 33431.
Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used only for
identification and explanation, without intent to infringe.
To my family
5 Private Methods
Description
Example
Practice Questions
6 Accessor Methods
Description
Accessor Method Nomenclature
Example
Direct Reference versus Accessor Methods
Practice Questions
8 Immutable Object
Description
Example
Practice Questions
9 Monitor
Description
Example
Practice Questions
11 Singleton
Description
Who Should Be Responsible?
Example
Make the Constructor Private
Static Public Interface to Access an Instance
Practice Questions
13 Prototype
Description
Shallow Copy versus Deep Copy
Shallow Copy Example
Deep Copy Example
Example I
Design Highlights of the HostingPlanKit Class
Example II
Redesign the UserAccount Class
Create a Prototype Factory Class
Practice Questions
14 Builder
Description
Example I
A Side Note
Back to the Example Application
Example II
Example III
Practice Questions
16 Iterator
Description
Iterators in Java
Filtered Iterators
Internal versus External Iterators
Example: Internal Iterator
Client/Container Interaction
Example: External Filtered Iterator
Practice Questions
18 Visitor
Description
Design Idea 1
Design Idea 2
Defining New Operations on the Object Collection
Adding Objects of a New Type to the Collection
Example
Design Approach I
Design Approach II
Design Approach III (Composite Pattern)
Design Approach IV (The Visitor Pattern)
Application Flow
Defining a New Operation on the Order Object Collection
Adding a New Order Type to the Collection
Practice Questions
20 Adapter
Description
Class Adapters versus Object Adapters
Class Adapter
Object Adapter
Example
Address Adapter as an Object Adapter
Practice Questions
21 Chain of Responsibility
Description
Example
Practice Questions
23 Proxy
Description
Proxy versus Other Patterns.
Proxy versus Decorator
Proxy versus Façade
Proxy versus Chain of Responsibility
RMI: A Quick Overview
RMI Components
RMI Communication Mechanism
RMI and Proxy Pattern
Example
Additional Notes
Compilation and Deployment Notes
Practice Questions
24 Bridge
Description
Example
Abstraction Implementation Design
Abstraction Interface Design
Design Highlights of the Abstraction Interface Classes
Bridge Pattern versus Adapter Pattern
Practice Questions
25 Virtual Proxy
Description
Advantage
Disadvantage
Example
Practice Questions
26 Counting Proxy
Description
Example
Practice Questions
27 Aggregate Enforcer
Description
Example
Design Approach I (On-Demand Initialization)
Design Approach II (Early Initialization)
Design Approach III (Final Variables)
Practice Questions
29 Object Cache
Description
Example
Practice Questions
31 Mediator
Description
Mediator versus Façade
Example I
Client Usage of the Mediator
User Interface Objects: Mediator Interaction
Example II
Practice Questions
32 Memento
Description
Example
DataConverter (Originator)
ID
Memento
process
createMemento
setMemento
DCClient (Client)
MementoHandler
Practice Questions
33 Observer
Description
Adding New Observers
Example
Subject–Observer Association
Logical Flow
34 Interpreter
Description
Example
Infix-to-Postfix Conversion (Listing 34.8)
Construction of the Tree Structure (Listing 34.9)
Postorder Traversal of the Tree
Additional Notes
Infix-to-Postfix Conversion
Infix Expression
Postfix Expression
Conversion Algorithm
Example
Binary Tree Traversal Techniques
Preorder (Node-Left-Right)
In-Order (Left-Node-Right)
Postorder (Left-Right-Node)
Level-Order
Practice Questions
35 State
Description
Stateful Object: An Example
Example
Practice Questions
36 Strategy
Description
Strategies versus Other Alternatives
Strategy versus State
Example
SimpleEncryption
CaesarCypher
SubstitutionCypher
CodeBookCypher
Practice Questions
37 Null Object
Description
Example
Practice Questions
38 Template Method
Description
Abstract Class
Concrete Class
Example
Additional Notes
Mod 10 Check Digit Algorithm
Practice Questions
39 Object Authenticator
Description
43 Guarded Suspension
Description
Example
Use of wait() and notify() in the ParkingLot Class Design
Practice Questions
44 Read-Write Lock
Description
Design Highlights of the ReadWriteLock Class
Lock Statistics
Lock Methods
Lock Release
Example
Practice Questions
SECTION X: APPENDICES
Appendix A: List of Design Patterns
Appendix B: References
䡲 7 Basic patterns
䡲 5 Creational patterns
䡲 4 Collectional patterns
䡲 11 Structural patterns
䡲 11 Behavioral patterns
䡲 4 Concurrency patterns
First and foremost, I would like to thank my wife for her patience and support,
for taking some of my workload especially in the ar eas of UML and Java
programming and for her inspirational contributions at the time of frustrating
moments. I would like to thank my parents, my sister, my brother and my dear
friends whose support and encouragement throughout my life have made it
possible for me to build the skill set necessary to succeed.
I would like to thank Venu Kuchana and D.R. Sudhakar for their contributions
in terms of writing different Java programs. I would like to thank BalaLingam
Kuchana for his contributions in the area of UML and for being in charge of
creating the formatted version of my draft.
I would like to thank the entire team at Auerbach publications for their
contributions in this project and for making this a remarkable experience. In
particular, I have a deep sense of gratitude towards my acquisitions editor, John
Wyzalek, for sharing my enthusiasm and providing me with great advice and
help. I also would like to thank the managing editor, Claire Miller, for her
invaluable advice and contribution in arranging the book in a presentable form.
My sincere thanks to Linda Rising for writing the Foreword.
I am truly appreciative and thankful to the following reviewers who have
taken the time to read the draft and provide me with feedback.
During the late 1970s, an architect named Christopher Alexander carried out the
first known work in the area of patterns. In an attempt to identify and describe
the wholeness or aliveness of quality designs, Alexander and his colleagues studied
different structures that were designed to solve the same problem. He identified
similarities among designs that were of high quality. He used the term pattern in
the following books to refer to these similarities.
Software frameworks can be confused with design patterns. They are closely
related. Table 1.1 lists the similarities and differences between the two.
Source Code
The source code for all example applications is available on the following Web
site for this book: http://www.crcpress.com/e_products/download.asp.
Thus far Cator, the attorney's clerk, had proved himself the most
sagacious and quick-witted of our party; though Captain Strogue
would have been amazed and indignant to hear me say so. And
now, when we had rejoined the rest of our little expedition, and all
were recruiting the inner man (or the middle man perhaps he is,
body being first, stomach second, and mind—when found, third
portion), that sprig of the law came up, with a bone between his
teeth, and begged the Captain and myself, who were feeding from
the outside tops of our hats, to go a little further round the elbow of
a crag. There he asked us what opinions we had formed; and when
we had taken our seats, we said: "None at all; except that we are all
bamboozled."
"No doubt about that. But how, and why?" He answered with a
mysterious look, which we were inclined to smile at, not having
known him long enough to be sure of his prophetic gifts. "A lot of
things have occurred to me, which may be very absurd of course,
and it is not likely that all are right; but I am pretty sure that some
are. Shall I tell you, and hear what you think of them?" We lit our
pipes, and nodded to him, and smiled at one another.
"To begin with, then, I suspect most strongly that her Majesty, the
Devil's wife, for so she deserves at least to be, has got her brother
under lock and key somewhere, snug enough, and at her mercy, if
she owns such a quality. Did you see what she touched, when she
went to gag, and at the same time to cram, that poor young fellow,
whose will she has crushed out of him by years and years of
bullying? Perhaps you could not see where you stood; and she did
not think that I could. But I saw the tips of her long fingers playing
with a key which was in her belt—a mere household key of course—
but enough to remind her unlucky son where his poor Uncle was,
without much chance of ever coming forth, but in his coffin. And I
caught a glance of his which proved that he understood her
meaning, and might soon have the same thing for himself. And then
you saw how he broke down; for he is a very tender-hearted youth."
"By Jove, it sounds uncommonly like it; I was so taken aback," said
Strogue, "at seeing another Prince Hafer in the field, and so different
from my Simon Pure, that I could not notice small things much; and
perhaps it was the same with Cranleigh. There is some abominable
villany at work, and we shall be too late to stop it. I would like to
insure friend Imar's life for ten thousand at five thousand premium.
Go on, my son, thou speakest well."
"Another thing, according to my lights. He is not in that queer old
place at all, Gomorrah Castle, or whatever they call it, although
there are plenty of black holes there, enough to starve a regiment
man by man. No, he is away to the North at present, perhaps on the
other side of the mountains. You saw the big window that faced the
North, more like a door than a window it was. Well, every time her
brother was in question, and especially when she was fingering that
key, she gave a quick glance through that window, very likely
without even knowing it. People who gesticulate much often follow it
up in that way. When they speak of a distant thing, they glance in
that direction, if they can see it, or anything anywhere near it; and
there was a great double-peaked mountain covered with snow, like a
white mitre, stuck against the sky in the North. And if her brother
had been in the castle dungeons, she would have made us go down
the front ladder again, instead of getting a wink of back premises."
"Upon my word, this boy is wide-awake, considering how little he
has seen yet of the world. Cator, like Cato, thou reasonest well. Go
ahead, my son, we hearken thee."
"You see, Captain," said the young man, feeling abashed in the
presence of such renown, and doubtful about some chaff in its palm;
"you see, I should never care to offer you advice. It might be in
place with Mr. Cranleigh here, because he is only a young beginner.
But you know what's what, I should say, ever so much better than I
do. But as you tell me to go on, I will. Her Serene Highness intends
to make away with her twin-brother, on Monday next."
"Come now, come now! I can stand a great deal, Cator. And none of
your butter-Scotch—no, you are a Shropshire man, you say.
Whatever you are, it won't make that go down. Why, Old Moore, and
Zadkiel would be nothing to you."
"Captain, I will tell you what I go by, and then you'll be able to judge
for yourself, whether I talk bunkum, or good sense. I have been in
these parts for a twelvemonth now, and I ought to know something
of these blessed natives. There are no two lots of them quite alike,
any more than two mountains or two valleys are. But there is not a
pin to choose among them in the matter of laziness. Poor beggars,
they can scarcely help that, I dare say, frozen as they are for half
the year, and roasted for the other half. Well, about here the manner
is to keep three holidays, Friday, Saturday, and Sunday, just as if
they did anything on the other four days. These Ossets about here
have no religion of any sort worth speaking of. Some call themselves
Christians, some Mahometans, many are simple idolaters; but all are
full of superstition, as such people must be. All they do in the
religious way, is to stick to their fasts and festivals, particularly the
festivals. And their great festival of the year finishes up next
Sunday."
"What the deuce has that got to do with it?" Strogue enquired
impatiently, for the sun was beginning to slope along the valley, and
we had ten miles to go to the next covered place.
"Everything, if you will hear me out. That festival goes on for three
weeks; and during that time it is not lawful to follow up even the
blood-feud. But on Monday, it will be the proper thing to stick and
stab all who are waiting for it. And what makes me think that this
little game is on, according to institution, is that we have not seen a
living soul, except an old woman and a child or two, in the miserable
villages we have come by. Why? For the very simple reason that
every noble savage who can swing a dagger is off for this great act
of faith on Monday; to see the death of the head of the clan
avenged."
"I won't believe a word of it," I exclaimed, meaning no rudeness of
course to Cator, but scouting the possibility of such fiendish
abominations, after all I had heard of the great man's lofty hopes
and pure ideals.
"To me it seems likely enough," said Strogue. "I have been among
fellows who would eat their mothers, and serve up their own babies
for a garnish. We have none of that sort to deal with here; and the
men of these mountains, taking them all round, are an indolent
rather than a cruel lot. Quarrelsome of course, and hot of blood, but
most loyal to their chiefs, and very generous sometimes. It is the
blood-feud that makes devils of them; but how can they help that?
It is their test of honour, ever since they came out of the Ark with
the raven. What we have got to do is to act exactly as if all our
friend Cator suggests were the fact. Thursday to-day; there is little
time to lose, even if we can catch it up at all. We shall want every
son of an emerald of you; and you must fight like sons of the
Emerald Isle. By Jove, what a ripping turn-up it will be! Right about
face, quick march for Kazbek!"
It was all very well for him, and Cator, and the rest to take things
lightly thus. They could not be expected to feel much concern for
the Lesghian Chief, or a Lesghian lady even more adorable. And as
for Strogue his main object was less to rescue Sûr Imar, than to
wreak his own vengeance upon Hafer—that is to say the London
Hafer, the one who had leaped the ivied wall, and shot at me, and
robbed the Captain, by some blackleg's process, of £300.
But I (with my warm affection and deep pity for the father, and
passionate love of the daughter) could see no adventurous joy or
fierce delight in the issue impending. I wanted no revenge, no
compensation for anything done against me. Hafer the genuine, and
Hafer the counterfeit, might settle their claims to the title as they
pleased; even that most malignant and awful woman—if she were as
black as she was painted—the Princess Marva might live her life out,
and give the best account of it when her time came; if only she
could be kept from harming her relatives so innocent. There must be
in her motives something more than we could see. Revenge alone
for the loss of a husband, with whom she had lived on the worst of
terms, and who had wronged her on the tenderest point—that, and
the time-worn grievance about the refusal of her marriage-portion
were not enough to drive her to such a horrible and unnatural deed
as—unless we wronged her most shamefully—she was now in cold
blood designing. There must be some other strong motive too, some
great temptation of self-interest, some of that stern, sour stuff which
drives us out of the hive that should be sweet to us.
No man knows what he does or thinks (unless he can keep himself
separate from the thoughts of all around him, which requires a
wonderful nature) when his legs go along with the legs of other
men, and he has to swing his arms accordingly. There was no sort of
march among us; for we had never been even of the Volunteer
Force (except myself, and that only made me critical, without any
help in it), and if we had wanted to show the Caucasus any sense of
drill, we could never have done it, even if we had known how. By
order of the rocky way, or of rocks without any way among them,
we could never march two abreast, or even three in file with
decency. All we could do was to get along, and admire one another's
clumsiness.
Then we came to a place with a sudden gap in front, and nothing
but the sky beyond it. A cleft in the crown of a rugged ascent, with
spires of black rocks right and left. And there on the saddle-ridge
that we must pass, a gaunt and wondrous figure arose, whether of
man or of beast, and wavered against the grey mist of the distance,
and swayed. Two long arms, like a gallows out of gear, or a cross
that has rotted with its weight, struck up; and having been severely
tried already we were much at a loss what to make of it. There was
good light still, and we were not to be frightened, as we must have
been after sunset; but the Interpreter being always nervous turned
round, and exclaimed: "She has sent the Devil, the Devil himself, to
stop us." While he spoke the long figure fell down on its knees, and
swung its lank arms, like a windmill.
"Hold hard! Don't fire!" Strogue shouted sternly, as some of our men
had brought their guns to bear. "Idiots, it is nothing but a poor lost
man, a fellow without a bit of food inside him. George, let us go and
see what he is up to."
I was ready to go anywhere and do anything in my present state of
mind; and when we came up to him, our poor brother mortal fell
upon his face, and put his hands upon our feet. He muttered some
words which we could not understand, and then he opened his
mouth, which was very large, and pointed down it intelligibly to the
slowest comprehension.
"He may be the Devil, but he wants some grub," Strogue shouted
back to our company, who were still looking towards us doubtfully,
for people become superstitious, without intending it, in these wild
places. Then Cator came up, with a barley-cake in one hand and his
rifle in the other. The unfortunate starver took no heed of the
weapon in his extremity, but stretched his shrivelled arm across the
muzzle, and tore the cake from Cator. In a moment it was gone,
almost without a munch; and then he stared at us, with sun-
scorched eyes projecting from their peel like a boiled potato,—and
groaned for more, crooking his fingers like prongs of a rake. We
shrank from him so that he might not touch us. But for the blood he
was covered with we should have taken him for a skeleton; and but
for his groans and nakedness we should have passed him as a
scarecrow.
"Don't be in such a hurry, old chap, or you'll do yourself more harm
than good," Strogue suggested reasonably. But even if the other had
understood, it would have made no difference. He spread his face
out in such a manner that there was nothing left but mouth; as a
young cuckoo in a sparrow's nest, when his stepmother cannot
satisfy him, squattles his empty body down, and distends himself
into one enormous gape. Then Tommy Williams came up laughing,
with his hat full of broken victuals; and the Captain, who understood
the subject, said: "Not too fast, or he'll fall to pieces. And pour down
a little whiskey to soften it."
When the poor fellow came round a little—and flat enough he had
been before—to our surprise he proved himself an exceedingly brave
and well-intentioned man. In fact, if he had been otherwise we
should never have found him there. A barbarian he appeared at first,
but that was appearance only, and under the stress of misfortune,
although he belonged to a race which is the most barbarous of the
Caucasus. When through our nervous interpreter we began to
understand him, we soon perceived that it was our good luck as well
as his own, which had brought him to us. And much as at first we
grudged the time expended in this humanity, we soon came to see
that it had been well spent, even for our own purposes. After such a
fast, and then such feasting (prolonged in even more than due
redress), it would have been most unfair to expect many words from
him prematurely. We clothed him a little, for he was stark naked,—
and so hairy a person I never beheld,—and then we cut the tight
cord knotted round his waist, from which even famine had not freed
him; and then we made a litter—for he could not walk—and carried
him to our night-quarters. Luckily there was no foe in search of us,
or that miserable sufferer's groans and snores must have told our
whereabout to every echo. He surprised us again by an eager call
for supper, but none would we give him, until he had splashed for a
quarter of an hour in the glacier stream. Then we fed him again, and
clothed him fairly, and a decent and reputable man he looked,
though going down the vale of years. And his tale was interpreted as
follows.
CHAPTER XLVIII
USI, THE SVÂN
"I am Usi, of Ushkul, in the country of the Svâns; Usi the Bear-slayer
was my name, as long as I lived among them. The custom of the
country is that as often as a female child is born, any youth of the
village who looks forward to his need of marriage may come to the
cradle and hang his own bullet around the neck of the infant, and
from that time she is pledged to him, and he must marry her when
she is old enough. When I was a stripling, the wife of our Priest
produced him their fourteenth child, a daughter; and I was the first
to go in at his door, and bespeak the young creature for myself. But
as fortune ordained, the damsel proved deaf and dumb, though in
other ways quite useful; and I very justly refused in the presence of
all the village to marry her. And this I did, when she was ten years
old, allowing her plenty of time for others, who might esteem it to
their pleasure and advantage to possess a wife without a tongue.
But the very next day, when I was watching the maize, a bullet
came through my hat, and lodged in a tree behind me; and when I
dug it out, behold it was my own with the fancy pattern on it, with
which I had betrothed myself ten years before. To that I need not
have paid much attention, but that the Priest had nine well-grown
sons, and it would be the duty of all these nine in succession to lie in
wait for me, and endeavour to shoot me through the head. The
eldest had been too near the mark for me to believe without
rashness that the other eight would fire in vain; so I took my good
mother's advice, which she gave me with many tears, and left my
native place for lifetime. Neither was it safe for me to dwell in any of
the villages for miles and miles around, because we people of the
Svâns had suffered from want of food for the last two years, and
had been obliged to take all the loaves, and corn, and cattle of our
neighbours within three days' journey; and so we were out of favour
with them.
"On this account I was compelled, having borne a strong hand in
those forages, to keep myself away from spots where I would have
settled gladly. At a distance I saw beautiful maidens, over the tops of
the raspberries; but whenever I desired to draw near them, there
was sure to be a father or a brother, whose cow or whose sheep had
been beef or mutton to me. And those people bear such things in
mind, not being generous as we are. And thus I went along the
valleys, feeding on the fruit, wherever the bears had left a tail of it.
Then going further towards the rising sun, which is the strength of
all of us, I came upon a man who carried a kinjal on a gun-mouth.
"In those days, I could jump as high as I could put my hands up;
and being surprised by his pointing at me, I did it to give him time to
think. This made him think more of me than I deserved, and instead
of shooting me, he asked in what land men could jump so. I could
not understand at first, though he did it with all his fingers; because
we had kept ourselves apart from other people, whenever we could
live without our neighbours' goods. But I was always considered the
foremost of the young men for understanding, and I contrived to
make out what he meant, and to do a thing which is much harder—
to make him know what I meant. He was a soldier of the great
Imaum, desiring to shoot Russians; and as soon as we made out
one another, he showed me the notches on his gun, and I counted
forty-two, and he said every one was the good corpse of a Russian.
This made me long to do the like, though the Russians had never
shot at me, but my own friends had; and my soul arose to look
along a gun at any stranger, even as it had been done to me.
"Others came up, and when they found how straight my barrel was,
and what it was famous for doing among the bears, the Captain
said, 'Thou shalt do it, my lad, with the bears that eat our people.'
And so I was put into Shamyl's army, and for many years enjoyed
myself. I have shot three Russian colonels, and small officers by the
dozen; and I could have shot the Commander once; but his
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookname.com