The Software Patterns Series
Series Editor: John M. Vlissides
The Software Patterns Series (SPS) comprises pattern literature of lasting significance to software developers. Software patterns document general solutions to recurring problems in all software-related spheres, from the technology itself, to the organizations that develop and distribute it, to the people who use it. Books in the series distill experience from one or more of these areas into a form that software professionals can apply immediately.
Relevance and impact are the tenets of the SPS. Relevance means each book presents patterns that solve real problems. Patterns worthy of the name are intrinsically relevant; they are borne of practitioners experiences, not theory or speculation. Patterns have impact when they change how people work for the better. A book becomes a part of the series not just because it embraces these tenets, but because it has demonstrated it fulfills them for its audience.
Titles in the series:
Data Access Patterns: Database Interactions in Object-Oriented Applications;Clifton Nock
Design Patterns Explained, Second Edition: A New Perspective on Object-Oriented Design; Alan Shalloway and James Trott
Design Patterns in C#; Steven John Metsker
Design Patterns in Java;Steven John Metsker and William C. Wake
Design Patterns Java Workbook;Steven John Metsker
.NET Patterns: Architecture, Design, and Process;Christian Thilmany
Pattern Hatching: Design Patterns Applied;John M. Vlissides
Pattern Languages of Program Design;James O. Coplien and Douglas C. Schmidt
Pattern Languages of Program Design 2;John M. Vlissides, James O. Coplien, and Norman L. Kerth
Pattern Languages of Program Design 3;Robert C. Martin, Dirk Riehle, and Frank Buschmann
Pattern Languages of Program Design 5;Dragos Manolescu, Markus Voelter, and James Noble
Patterns for Parallel Programming;Timothy G. Mattson, Beverly A. Sanders, and Berna L. Massingill
Software Configuration Management Patterns: Effective Teamwork, Practical Integration;Stephen P. Berczuk and Brad Appleton
The Design Patterns Smalltalk Companion;Sherman Alpert, Kyle Brown, and Bobby Woolf
Use Cases: Patterns and Blueprints;Gunnar vergaard and Karin Palmkvist
For more information, check out the series web site at www.awprofessional.com/series/swpatterns
Design Patterns in Java
Steven John Metsker
William C. Wake
Upper Saddle River, NJ Boston Indianapolis San Francisco
New York Toronto Montreal London Munich Paris Madrid
Capetown Sydney Tokyo Singapore Mexico City
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals.
The authors and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein.
The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include electronic versions and/or custom covers and content particular to your business, training goals, marketing focus, and branding interests. For more information, please contact:
U.S. Corporate and Government Sales
(800) 382-3419
For sales outside the United States, please contact:
International Sales
Visit us on the Web: www.awprofessional.com
Library of Congress Cataloging-in-Publication Data
Metsker, Steven John.
Design patterns in Java / Steven John Metsker, William C. Wake.
p. cm.
Includes bibliographical references and index.
ISBN 0-321-33302-0 (hardback : alk. paper)
1. Java (Computer program language) 2. Software patterns. I. Wake, William C., 1960II. Title.
QA76.73.J38M482 2006
005.133dc22 2006003471
Copyright 2006 Pearson Education, Inc.
All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permissions, write to:
Pearson Education, Inc.
Rights and Contracts Department
75 Arlington Street, Suite 300
Boston, MA 02116
Fax: (617) 848-7047
ISBN 0-321-33302-0
Text printed in the United States on recycled paper at Courier in Westford, Massachusetts.
First printing, April 2006
To Alison, Emma-Kate, and Sarah-Jane
Steve
To May Lyn, Tyler, and Fiona
Bill
Preface
DESIGN PATTERNS ARE class- and method-level solutions to common problems in object-oriented design. If youre an intermediate-level Java programmer who wants to become advanced or an advanced-level Java programmer who hasnt yet studied design patterns, this book is for you.
Design Patterns in Java takes a workbook approach. Each chapter focuses on a particular pattern. In addition to explaining the pattern, the chapter includes a number of challenges, each asking you to explain something or to develop code that solves a problem.
We strongly urge you to stop and work through the challenges rather than try to read this book straight through. Youll learn more by putting in the work to do the challenges, even if its only a chapter or two a week.
An Update
This book merges and updates two previous books: Design Patterns Java Workbook and Design Patterns in C#. This book combines the Java orientation of the former with the more stand-alone approach of the latter. If youve already worked through the previous books, you wont need this one.
Coding Conventions
The code for this book is available online. See for details on how to obtain it.
Weve used a style generally consistent with Suns coding conventions. Braces are omitted where possible. We have had to make a couple of compromises to fit the book format. To fit the narrow columns, variable names are sometimes shorter than wed really use. And to avoid the complications of source control, we name multiple versions of a file with a digit appended to the name (e.g., ShowBallistics2
). In real life, youd use source control and work only with the latest version of a class.
Acknowledgments
A book is a challenging undertaking. Along the way, a number of reviewers have provided us with valuable advice: Daryl Richter, Adewale Oshineye, Steven M. Luplow, Tom Kubit, Rex Jaeschke, Jim Fox, and David E. DeLano. Each one made suggestions that improved the end result. Readers and reviewers of the earlier books have contributed as well.
Thanks also to the editorial staff at Addison-Wesley, especially Chris Guzikowski, Jessica DAmico, and Tyrrell Albaugh. Other editors helped along the way, including Mary OBrien and John Wait.