You are on page 1of 15

9525_Grand_fm_v.

qxd 8/26/02 9:23 AM Page iii

Patterns in Java ,

Volume 1
A Catalog of Reusable
Design Patterns
Illustrated with UML
Second Edition

MARK GRAND

Wiley Publishing, Inc.


9525_Grand_fm_v.qxd 8/26/02 9:23 AM Page ii
9525_Grand_fm_v.qxd 8/26/02 9:23 AM Page i


Patterns in Java , Volume 1
Second Edition
9525_Grand_fm_v.qxd 8/26/02 9:23 AM Page ii
9525_Grand_fm_v.qxd 8/26/02 9:23 AM Page iii

Patterns in Java ,

Volume 1
A Catalog of Reusable
Design Patterns
Illustrated with UML
Second Edition

MARK GRAND

Wiley Publishing, Inc.


9525_Grand_fm_v.qxd 8/26/02 9:23 AM Page iv

Publisher: Robert Ipsen


Editor: Theresa Hudson
Developmental Editor: Kathryn Malin
Managing Editor: Angela Smith
New Media Editor: Brian Snapp
Text Design & Composition: North Market Street Graphics

Designations used by companies to distinguish their products are often claimed as


trademarks. In all instances where Wiley Publishing, Inc. is aware of a claim, the
product names appear in initial capital or all capital letters. Readers, however,
should contact the appropriate companies for more complete information
regarding trademarks and registration.


This book is printed on acid-free paper.

Copyright 2002 by Mark Grand. All rights reserved.

Published by Wiley Publishing, Inc., Indianapolis, Indiana

Published simultaneously in Canada.

No part of this publication may be reproduced, stored in a retrieval system or


transmitted in any form or by any means, electronic, mechanical, photocopying,
recording, scanning or otherwise, except as permitted under Sections 107 or 108 of
the 1976 United States Copyright Act, without either the prior written permission
of the Publisher, or authorization through payment of the appropriate per-copy fee
to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978)
750-8400, fax (978) 750-4744. Requests to the Publisher for permission should be
addressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspointe
Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4447, E-mail:
permcoordinator@wiley.com

This publication is designed to provide accurate and authoritative information in


regard to the subject matter covered. It is sold with the understanding that the
publisher is not engaged in professional services. If professional advice or other
expert assistance is required, the services of a competent professional person
should be sought.

Library of Congress Cataloging-in-Publication Data:


ISBN: 0-471-22729-3

Wiley also publishes its books in a variety of electronic formats. Some content that
appears in print may not be available in electronic versions.
For more information about Wiley products, visit our web site at www.wiley.com.

Printed in the United States of America.

10 9 8 7 6 5 4 3 2 1
9525_Grand_fm_v.qxd 8/26/02 9:23 AM Page v

C O N T E N T S

Acknowledgments ix

About the Author xi

Chapter 1 Introduction to Software Patterns 1


Patterns Description 2
A Brief History of Patterns 4
Organization of This Book 5

Chapter 2 Overview of UML 7


Class Diagram 8
Collaboration Diagram 18
Statechart Diagram 27
Deployment Diagram 29

v
9525_Grand_fm_v.qxd 8/26/02 9:23 AM Page vi

vi Contents

Chapter 3 The Software Life Cycle 31


Case Study 34
Business Case 34
Define Requirements Specification 35
Develop High-Level Essential Use Cases 37
Object-Oriented Analysis 39
Object-Oriented Design 40

Chapter 4 Fundamental Design Patterns 51


Delegation (When Not to Use Inheritance) 53
Interface 61
Abstract Superclass 67
Interface and Abstract Class 73
Immutable 79
Marker Interface 85
Proxy 91

Chapter 5 Creational Patterns 101


Factory Method 103
Abstract Factory 119
Builder 129
Prototype 139
Singleton 149
Object Pool 159

Chapter 6 Partitioning Patterns 175


Filter 177
Composite 187
Read-Only Interface 199

Chapter 7 Structural Patterns 207


Adapter 209
Iterator 217
Bridge 223
Faade 235
Flyweight 243
Dynamic Linkage 255
Virtual Proxy 265
Decorator 275
Cache Management 283
9525_Grand_fm_v.qxd 8/26/02 9:23 AM Page vii

Contents vii
Chapter 8 Behavioral Patterns 303
Chain of Responsibility 305
Command 317
Little Language 329
Mediator 355
Snapshot 369
Observer 387
State 397
Null Object 407
Strategy 413
Template Method 419
Visitor 427

Chapter 9 Concurrency Patterns 439


Single Threaded Execution 443
Lock Object 453
Guarded Suspension 461
Balking 469
Scheduler 475
Read/Write Lock 485
Producer-Consumer 495
Two-Phase Termination 501
Double Buffering 507
Asynchronous Processing 523
Future 535

Appendix Overview of Patterns in Java, Volumes 1 through 3 547

Bibliography 567

Index 569
9525_Grand_fm_v.qxd 8/26/02 9:23 AM Page viii
9525_Grand_fm_v.qxd 8/26/02 9:23 AM Page ix

A C K N O W L E D G M E N T S

I would first like to acknowledge the patience of my wife, Nicole. Without


her support, this second edition of Patterns in Java, Volume 1 would not
have been possible.
I would also like to acknowledge the feedback that I have received
from many readers whose emails to me suggested improvements and cor-
rections that have gone into this second edition. I would like to single out
Ed Remmell, who organized a discussion group around my book and took
the time to send me summaries of the discussions. Other readers who took
the time and trouble to send me useful questions and comments are (in
alphabetical order): Nader Afshar, Derrick Ampy, Barry W. Anderson, Paul
E. Andrighetti, Kaushik Barot, Ted Beckett, Pablo Bellver, Davide De
Benedictis, Dave Busse, Mike Carlin, Max Chandler, John Clarke, Charlie
Crook, Christian Cryder, Carlos Devoto, Cyrus Doomasia, Rodrigo
Figueiredo, Jeff French, Brent Fry, Srinivas Ganti, Daniel L. Gleneck,
Satish Gupta, Barton W. Hammond, Shannon Harvey, Judy Housman,
Hong Hsu, Rob Hulsebos, Gary Janes, Brian Krahmer, Erik Bak
Kristensen, Robert Laganire, Laurene O. Laidlaw, Allen Lee, Roger Lee,
Tim Lethbridge, Charles Magid, Kurt Matthys, Phil McGlauchlin, Barry

ix
9525_Grand_fm_v.qxd 8/26/02 9:23 AM Page x

x Acknowledgments

Middlebrook, Sadiq Mohammed, Thomas Moore, Raymond Naseef, Jaime


Nino, Jeff Oakes, Sudesh Palekar, Greg Pearman, Jim Phipps, John Pinto,
Jon Poploskie, Raj Prathinidhi, Andy Pryke, Tulsi Rai, Damith C.
Rajapakse, Steven Ranieri, John Sargeant, Robert Allan Schwartz, Greg
Scott, Joe Sharp, Mike Shivas, Eduardo Silva, Daniel Stainhauser, Steve
Stephens, Christian Svenstrup, Oyvind Teig, Alan Thompson, Richard
Tomlinson, Bruce Wallace, Dieter Wankmueller, Mark Waschkowski,
Richard Williams, Joe Wissmann, Robert Wragg, Wayne Wylupski, and
Hong Yan.

Acknowledgments from the First Edition


I want to thank Craig Larman, my most conscientious reviewer. He con-
vinced me of the importance of patterns. Craig also provided me with
much invaluable feedback on the use of UML and the presentation of pat-
terns. His valuable suggestions greatly improved the way that I present the
patterns in this book.
Jack Harich was another manuscript reviewer who made many useful
suggestions about the organization of this book. He convinced me to
expand my coverage of fundamental patterns. He also supplied the example
that I use for the State pattern.
Brad Appleton provided me with the most voluminous and detailed
comments.
The UIUC patterns group provided some insightful discussions for
the patterns in this book. The participants in those discussions included
Brian Foote, Ed Peters, Dragos Malonescu, Peter Hatch, Don Roberts,
Joseph W. Yoder, Ralph Johnson, John Brant, James Overturf, Jean Pierre
Briot, Eiji Nabika, Hiro Nakamura, and Ian Chai.
I also want to acknowledge some of my other reviewers, in particular
Micheal Wheaton and Micheal Pair.
9525_Grand_fm_v.qxd 8/26/02 9:23 AM Page xi

A B O U T T H E A U T H O R

Mark Grand is an Atlanta-based consultant with over 23 years of experi-


ence specializing in distributed systems, object-oriented design, and
Java. He was the architect for the first commercial business-to-business
(B2B) e-commerce product for the Internet. He is currently working on
an open source framework for gluing components and programs into an
application.
Mark Grand is most widely known for his best-selling Patterns in Java
books. In addition to teaching Java for Sun and other training organizations,
Mark has been involved in several large-scale commercial Java projects.
Prior to his involvement with Java, Mark spent over 11 years as a
designer and implementer of fourth-generation languages (4GLs). His
most recent role in that vein was as the architect and project manager for
an electronic data interchange product. Mark has worked with a number
of information technology (IT) organizations in capacities such as soft-
ware architect, database architect, network designer and administrator,
and Sun System administrator. He has been involved with object-oriented
programming and design since 1982.
9525_Grand_fm_v.qxd 8/26/02 9:23 AM Page xii
9525_Grand_01_v.qxd 8/26/02 9:07 AM Page 1

C H A P T E R 1
Introduction to
Software Patterns

Software patterns are reusable solutions to recurring problems that we


encounter during software development. Because this book is all about
software patterns, they are simply referred to as patterns throughout.
What makes a bright, experienced programmer much more produc-
tive than a bright, but inexperienced programmer? Experience. Experience
gives programmers a wealth of wisdom. As programmers gain experience,
they recognize the similarity of new problems to those they have solved in
the past. With even more experience, they recognize that solutions to simi-
lar problems follow recurring patterns. With knowledge of these patterns,
experienced programmers can recognize a problem to which specific pat-
terns apply and immediately determine the solution without having to stop
to analyze the problem first.
When a programmer discovers a pattern, its just an insight. In most
cases, to go from an unverbalized insight to a well-thought-out idea that
the programmer can clearly articulate is surprisingly difficult. Its also an
extremely valuable step. When we understand a pattern well enough to put
it into words, we are able to intelligently combine it with other patterns.
More important, once put into words, a pattern can be used in discussions
among programmers who know the pattern. Such discussions allow pro-
grammers to more effectively collaborate and combine their wisdom. They

You might also like