Software Architecture Design Patterns in Java 1st Edition Partha Kuchana download pdf
Software Architecture Design Patterns in Java 1st Edition Partha Kuchana download pdf
com
https://ebookname.com/product/software-architecture-design-
patterns-in-java-1st-edition-partha-kuchana-2/
OR CLICK BUTTON
DOWNLOAD EBOOK
https://ebookname.com/product/software-architecture-design-patterns-
in-java-1st-edition-partha-kuchana/
ebookname.com
https://ebookname.com/product/patterns-in-java-a-catalog-of-reusable-
design-patterns-illustrated-with-uml-2nd-edition-volume-1-grand/
ebookname.com
https://ebookname.com/product/global-trade-in-services-fear-facts-and-
offshoring-1st-edition-j-bradford-jensen/
ebookname.com
Sauces 3rd Edition James Peterson
https://ebookname.com/product/sauces-3rd-edition-james-peterson/
ebookname.com
https://ebookname.com/product/micromechanics-of-fracture-and-damage-
volume-1-1st-edition-luc-dormieux/
ebookname.com
https://ebookname.com/product/new-history-of-anthropology-1st-edition-
henrika-kuklick/
ebookname.com
https://ebookname.com/product/1948-a-history-of-the-first-arab-
israeli-war-first-edition-benny-morris/
ebookname.com
Ecology 4th Edition William D. Bowman
https://ebookname.com/product/ecology-4th-edition-william-d-bowman/
ebookname.com
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: [email protected]
© 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.
There are two concertos and a concert piece for piano and
orchestra. The latter owes its form and style very clearly to Weber’s
concert piece in F minor. Both the concertos are fluent and plausible
enough; the orchestra is handled with Mendelssohn’s customary
good taste and sensitiveness; but the writing for the pianoforte is
wholly commonplace and the themes themselves of little or no
distinction.
II
Meanwhile Robert Schumann was composing sets of pieces which
have been and long will be regarded as one of the most precious
contributions of the Romantic movement to pianoforte literature.
Schumann was an enthusiast and an innovator. He was a poet and a
warm-hearted critic. He was the champion of the new and the fresh,
of self-expression and noble sentiment. In his early manhood a
strained finger resulted from over-enthusiastic and unwise efforts to
make his hand limber, and cut short his career as a concert pianist,
for which he had given up his study of the law, not without some
opposition. He turned, therefore, with all fervor to composing music
for the pianoforte, and before his long-delayed marriage with Clara
Wieck, daughter of his teacher, had published the sets of pieces on
which a great part of his fame now rests.
Perhaps the most distinctive feature of his style is his frequent use of
syncopated rhythms. This becomes at times an obsession with him;
and there are many passages in his music so continuously off the
beat, that the original measure is quite lost, and the syncopation is to
all practical purpose without effect. In such passages it seems hardly
possible that Schumann intended the original beat to be kept in mind
by the accentuation of notes that are of secondary importance;
unless, of course, the interest of the music is chiefly rhythmical. Yet
in some passages of purely melodic significance this may be done
without awkwardness, producing an effect of dissociation of melody
and harmony which may be what Schumann heard in his mind.
These are problems for the pianist, but a few of them may be
suggested here. The last movement of the very beautiful concerto is
in 3/4 time. There is no change of time signature for the second
theme. This, as first announced by the orchestra in E major and later
taken up by the piano solo in B major, is none the less in 3/2 time.
Such must be the effect of it, because the passage is long and
distinct enough to force the 3/4 beat out of the mind, since no note
falls in such a way as to accent it. But when the orchestra takes up
this theme, again in E major, the piano contributes a steadily flowing
stream of counterpoint. In this it is possible to bring out the original
measure beat, throwing the whole piano part into a rhythm counter to
the rhythm of the orchestra. Such an accentuation is likewise out of
line with the natural flow of the counterpoint; yet it may be what
Schumann desired here, as well as in the following section, where,
though the orchestra is playing in 3/2 time, the pianist may go
against the natural line of his own part and bring out a measure of
three-quarter notes.
Besides these cycles there are sets of short pieces which are
independent of each other. Such are the ‘Fantasy Pieces,’ the
Novelettes, the ‘Romances,’ and the Bunte Blätter, among others.
These may be fairly compared with the ‘Songs without Words’ of
Mendelssohn. How utterly different they prove to be, how virile and
how genuinely romantic! They are not only the work of a creative
genius of the highest order, they show an ever venturesome spirit at
work on the keyboard. Take, for example, the ‘Fantasy Pieces.’ The
first, called Des Abends, is as properly a song as any of
Mendelssohn’s short pieces which are so designated. The very
melody is inspired and new, rising and falling in the long smooth
phrases which are the gift of the great artist, not the mere music-
maker. The accompaniment appears simple enough; but the wide
spacing, the interlocking of the hands, above all, its rhythm, which is
not the rhythm of the melody, these are all signs of fresh life in
music. The interweaving of answering phrases of the melody in the
accompaniment figures, the contrast of registers, the exquisite points
of harmonic color which the accompaniment touches in the short
coda, these are signs of the great artist. It is remarkable how little
Mendelssohn’s skill prompted him to such beautiful involutions; how,
master as he was of the technique of sound, he could amble for ever
in the commonplace. And Schumann, with far less grasp of the
science, could venture far, far beyond him.
The second of the ‘Fantasy Pieces,’ Aufschwung, calls imperiously
upon the great resources of the pianoforte. There is power and
breadth of style, passion and fancy at work. It is a wholly different
and greater art than Mendelssohn’s. It is effective, it speaks, it
proclaims with the voice of genius. And in the little Warum? which
follows it, skill is used for expression. There is perhaps more
appreciation of the pianoforte in this piece, which by nature is not
pianistic, than there is in all the ‘Songs without Words,’ an
appreciation of the contrasting qualities of high and low sounds, of
the entwining of two melodies, of the suggestive possibilities of
harmony.
Take them piece by piece, the Grillen with its brusque rhythms, its
syncopations, its rapidly changing moods; the In der Nacht, with its
agitated accompaniment, its broken melodies, and the soaring
melody of the middle section, not to mention the brief canonic
passages which lead from this section back to the wild first mood;
the delicate Fabel, the Traumes Wirren with its fantastic, restless,
vaporish figures and the strange, hushed, shadows of the middle
section; and the Ende vom Lied, so full for the most part of good
humor and at the end so soft and mysteriously sad; these are all
visions, all prophecies, all treasure brought back from strange and
distant beautiful lands in which a fervid imagination has been
wandering. Into such a land as this Mendelssohn never ventured,
never even glanced. For Schumann it was all but more real than the
earth upon which he trod, such was the force of his imagination.
The imagination is nowhere more finely used than in the short pieces
called the Kinderscenen. Each of these pieces gives proof of
Schumann’s power to become a part, as it were, of the essence of
things, to make himself the thing he thought or even the thing he
saw. They are not picture music, nor wholly program music. They are
more a music of the imagination than of fact. Schumann has himself
become a child in spirit and has expressed in music something of the
unbound rapture of the child’s mind. So, even in a little piece like the
‘Rocking Horse,’ we have less the picture of the ‘galumphing’
wooden beast, than the ecstasy of the child astride it. In the Curiose
Geschichte there is less of a story than of the reaction of the child
who hears it. In the Bittendes Kind and the Fürchtenmachen this
quality of imagination shows itself with almost unparalleled intensity.
The latter is not the agency of fear, it is the fear itself, suspense,
breathless agitation. The former does not beg a piece of cake; it is
the anguished mood of desire. Only in the last two pieces does
Schumann dissociate himself from the moods which he has been
expressing. The former, if it is not the picture of the child falling
asleep, is the process itself; the latter is, as it were, the poet’s
benediction, tender and heartfelt.
The most imaginative and the most fantastic of the works as a whole
is the series of twenty short pieces which make up the Carnaval,
opus 9. Here there is a kaleidoscopic mixture of pictures, characters,
moods, ideas, and personalities; the blazonry of spectacle, the noise
and tumult, the quiet absorption that may come over one in the midst
of such animation, the cool shadows beyond the edge of it wherein
lovers may wander and converse; strange flashes of thought,
sudden darting figures, apparitions and reminiscences. All is
presented with unrelaxing intensity. One cannot pick out a piece from
the twenty which does not show Schumann’s imagination at fever
heat. There is a wealth of symbolism; the Sphinxes, mysterious
sequences of notes that are common to all the pieces, and dancing
letters which spell the birthplace of one of Schumann’s early loves.
The next two numbers in the scene are pictures of two figures
common to nearly every fair, the Pierrot and the Harlequin. The
distinction between them is exquisite. In Pierrot we have the clown,
now mock-mournful and pathetic, only to change in a second and
startle with some abrupt antic. Harlequin, on the other hand, is
nimble and quick, full of hops and leaps. At the end of the Pierrot, by
the way, there is the chance to experiment with the pedal in
overtones. The sharp fortissimo dominant seventh, just before the
end, will set the notes of the following chord, all but the fundamental
E-flat, in vibration if the pedal is pressed down; so that the keys of
this second chord need hardly to be struck but only to be pressed.
And when the pedal is lifted, this second chord will be left still
sounding, by reason of the sympathetic vibration which was set
about in its strings by the loud chord preceding.
The love of his whole life follows—Chiarina, his beloved Clara; and,
as if with her were associated the loveliest and most poetic of
pianoforte music, he calls Chopin to mind. Chopin at this fair! It is a
fantastic touch. More than when Eusebius speaks, the background
of gay dancers and masqueraders fades from sight. For a moment
Chopin is in our midst. Then he has vanished. And at once another
thought of Clara, this time as Estrella; then an acquaintance in the
throng. He has seen a face he knew, it is a friend. It is the Sphinx of
Chiarina in the music. Is it she he recognized? Are the lovely
interchanges in the middle section conversations with her? If so,
their mood is light. They have met at a fair. They are in the merry-
making.
III
The other cycles of Schumann comparable to it are the Papillons,
opus 2, the Davidsbündler Tänze, opus 6, and the
Faschingsschwank aus Wien, opus 26. The first of these is short and
slight, but of singularly faultless workmanship and rare charm. The
last must be cherished for the Romanza, the Scherzo, and the
splendid Intermezzo; but the first movement is rather out of
proportion, and parts of the last are perfunctory and uninteresting.
Most of the Dances of the Davidsbündler are beautiful. The series is,
however, much too long and too loose to be regarded as a whole.
There are passages of unsuccessful workmanship, notably in the
third; some of the dances are rambling, some rather commonplace.
On the other hand, many may be ranked among the best of
Schumann’s compositions. The second, seventh, and fourteenth
have been mentioned as among the beautiful utterances of
Eusebius; the fifth is less distinguished but is delightful pianoforte
music. Florestan does not make quite such a good impression,
except possibly in the fourth and the twelfth. The fifteenth speaks for
both Florestan and Eusebius; and the E-flat major section is
splendidly rich and full-throated music. The last dance of all is like a
happy, wayward elf waltzing along in the wake of more substantial
dancers. The series may properly end with the seventeenth; but, as
Schumann said, though Eusebius knew well that the eighteenth was
quite superfluous, yet one could see by his eyes that he was blissful
over it.[32]
Both the ‘Symphonic Studies’ and the Kreisleriana stand apart from
the works previously discussed. The former, opus 13, was written in
1834, the latter, opus 16, in 1838. A brief glance at opus 1, the
‘Abegg’ variations, written in 1830, will serve to make clear the
immense progress Schumann made in the art of composition in the
brief space of four years. The early work is by no means lacking in
interest. Schumann reveals himself in nearly every page. The theme
itself is made up of the notes a, b, e, g, g, spelling the name of the
honorable lady to whom the variations were dedicated. In the middle
of the last movement he experiments with a new style of
diminuendo, allowing a chord to die away by separate notes, till only
one note of it is left sounding. He tried the same effect again at the
end of the Papillons. But the workmanship, though clever, is for the
most part conventional. The statement of the theme is laughably
simple, particularly the ‘echoes,’ pianissimo, in broken octaves. Such
a device recalls the ‘Maiden’s Prayer’ and fountain curls. The
variations show a fine ear for pianoforte effects. The first especially is
in virtuoso style and makes more use of the upper registers of the
keyboard than is common in the later works. But the harmonies,
though richly altered, are conventional, and so are the figures. The
third, fourth, and fifth might have been written by Hummel.
It is, on the other hand, superb. The opening movement alone, with
its figures like short waves in a windy sea, its sharp cross-accents,
its filmy, elusive trio, is a masterpiece. The second movement is
unbalanced, yet at times most wondrously beautiful. The opening
theme in itself is inspired, though it is perhaps overworked. But what
is the meaning of the harsh chords which interrupt it and shatter the
mood which it might else instill? The style is polyphonic in places;
there are inner melodies that slide long distances up and down the
keyboard, oftenest in tenths. The two intermezzi furnish a welcome
contrast to the intense subjectivity of most of this second movement.
After the second there comes one of the loveliest pages in all
Schumann’s pianoforte music.
The third movement is built on a restless, jerky figure, in ceaseless
movement. There are strong accents and unusual harmonies. A
middle section offers yet another happy instance of Schumann’s skill
in dialogue between two melodies, such as we have already noticed
in Warum? and the eleventh of the ‘Symphonic Études.’ The
movement is somewhat slower than the main body of the piece, but
a strange sort of half-accompaniment does not allow the
restlessness to subside altogether.
The fourth and sixth movements are slow. In both there is some
thickness of scoring, a sinking too deep into the lower registers. Both
are about the same length and both are constructed on the same
plan; consisting of an incompleted, or broken, melody of the most
intimately expressive character, a few measures of recitative, the
melodious phrases again—in the one wandering down alone into the
bass, disappearing rather than ending, in the other not completing
itself, but developing into a contrasting section. In both there are
these contrasting sections of more articulate and more animated
music; and in both there is a return of the opening melody. There is
wonderful music in these two short movements; but it is mysterious,
fragmentary and incomplete, visionary, as it were, and without
definite line.
IV
The only worthy successor to Schumann in the realm of German
pianoforte music is Johannes Brahms. Into the hands of Brahms
Schumann may be said to have given over the standard which he