esign Patterns in Java (2nd Edition) Metsk
ibooki.com/download/design-patterns-in-j
his Book is Available on YakiBooki.com
Design Patterns in Java (2nd Edition) Metsker
https://www.yakibooki.com/download/design-patterns-in-java-2nd-edition/
DESIGN PATTERNS IN JAVA™
This Book is Available on YakiBooki.com
Design Patterns in Java (2nd Edition) Metsker
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
https://www.yakibooki.com/download/design-patterns-in-java-2nd-edition/
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
This Book is Available on YakiBooki.com
Design Patterns in Java (2nd Edition) Metsker
https://www.yakibooki.com/download/design-patterns-in-java-2nd-edition/
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
This Book is Available on YakiBooki.com
Design Patterns in Java (2nd Edition) Metsker
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
https://www.yakibooki.com/download/design-patterns-in-java-2nd-edition/
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 pur-
chases or special sales, which may include electronic versions and/or custom covers and con-
tent 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
[email protected] For sales outside the United States, please contact:
International Sales
[email protected]
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., 1960–
II. Title.
QA76.73.J38M482 2006
005.13'3—dc22 2006003471
Copyright © 2006 Pearson Education, Inc.
All rights reserved. Printed in the United States of America. This publication is protected by copy-
right, 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, mechani-
cal, 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 Westford in Westford, Massachusetts.
3rd Printing August 2008
This Book is Available on YakiBooki.com
Design Patterns in Java (2nd Edition) Metsker
https://www.yakibooki.com/download/design-patterns-in-java-2nd-edition/
To Alison, Emma-Kate, and Sarah-Jane
—Steve
To May Lyn, Tyler, and Fiona
—Bill
This Book is Available on YakiBooki.com
Design Patterns in Java (2nd Edition) Metsker
https://www.yakibooki.com/download/design-patterns-in-java-2nd-edition/
This page intentionally left blank
This Book is Available on YakiBooki.com
Design Patterns in Java (2nd Edition) Metsker
https://www.yakibooki.com/download/design-patterns-in-java-2nd-edition/
CONTENTS
Preface xiii
CHAPTER 1 INTRODUCTION 1
Why Patterns? 1
Why Design Patterns? 2
Why Java? 3
UML 4
Challenges 4
The Organization of This Book 5
Welcome to Oozinoz! 6
Summary 7
PART I INTERFACE PATTERNS 9
CHAPTER 2 INTRODUCING INTERFACES 11
Interfaces and Abstract Classes 11
Interfaces and Obligations 13
Summary 15
Beyond Ordinary Interfaces 16
CHAPTER 3 ADAPTER 17
Adapting to an Interface 17
Class and Object Adapters 21
Adapting Data for a JTable 25
Identifying Adapters 30
Summary 31
CHAPTER 4 FACADE 33
Facades, Utilities, and Demos 33
Refactoring to FACADE 35
Summary 46
This Book is Available on YakiBooki.com vii
viii Design Patterns in Java (2nd Edition) MetskerContents
CHAPTER 5 COMPOSITE 47
An Ordinary Composite 47
Recursive Behavior in Composites 48
Composites, Trees, and Cycles 50
https://www.yakibooki.com/download/design-patterns-in-java-2nd-edition/
Composites with Cycles 56
Consequences of Cycles 60
Summary 60
CHAPTER 6 BRIDGE 63
An Ordinary Abstraction: On the Way to BRIDGE 63
From Abstraction to BRIDGE 66
Drivers as BRIDGEs 68
Database Drivers 69
Summary 71
PART II RESPONSIBILITY PATTERNS 73
CHAPTER 7 INTRODUCING RESPONSIBILITY 75
Ordinary Responsibility 75
Controlling Responsibility with Visibility 77
Summary 79
Beyond Ordinary Responsibility 79
CHAPTER 8 SINGLETON 81
SINGLETON Mechanics 81
Singletons and Threads 83
Recognizing SINGLETON 84
Summary 86
CHAPTER 9 OBSERVER 87
A Classic Example: OBSERVER in GUIs 87
Model/View/Controller 92
Maintaining an Observable Object 99
Summary 101
CHAPTER 10 MEDIATOR 103
A Classic Example: GUI Mediators 103
Mediators of Relational Integrity 108
Summary 116
This Book is Available on YakiBooki.com
ContentsDesign Patterns in Java (2nd Edition) Metsker ix
CHAPTER 11 PROXY 117
A Classic Example: Image Proxies 117
Image Proxies Reconsidered 122
Remote Proxies 125
https://www.yakibooki.com/download/design-patterns-in-java-2nd-edition/
Dynamic Proxies 131
Summary 136
CHAPTER 12 CHAIN OF RESPONSIBILITY 137
An Ordinary Chain of Responsibility 137
Refactoring to CHAIN OF RESPONSIBILITY 139
Anchoring a Chain 142
CHAIN OF RESPONSIBILITY without COMPOSITE 144
Summary 144
CHAPTER 13 FLYWEIGHT 145
Immutability 145
Extracting the Immutable Part of a Flyweight 146
Sharing Flyweights 148
Summary 152
PART III CONSTRUCTION PATTERNS 153
CHAPTER 14 INTRODUCING CONSTRUCTION 155
A Few Construction Challenges 155
Summary 157
Beyond Ordinary Construction 157
CHAPTER 15 BUILDER 159
An Ordinary Builder 159
Building under Constraints 162
A Forgiving Builder 164
Summary 165
CHAPTER 16 FACTORY METHOD 167
A Classic Example: Iterators 167
Recognizing FACTORY METHOD 168
Taking Control of Which Class to Instantiate 169
FACTORY METHOD in Parallel Hierarchies 171
Summary 173
This Book is Available on YakiBooki.com