Contents Previous Next
Inside Front Cover
List of Refactorings
Refactoring Directions
Contents Previous Next
Inside Front Cover
1
Contents Previous Next
Refactoring to Patterns
By Joshua Kerievsky
Publisher: Addison Wesley
Pub Date: August 05, 2004
ISBN: 0-321-21335-1
Pages: 400
•
Table of
Contents
In 1994, Design Patterns changed the landscape of object-oriented
development by introducing classic solutions to recurring design problems. In
1999, Refactoring revolutionized design by introducing an effective process
for improving code. With the highly anticipated Refactoring to Patterns,
Joshua Kerievsky has changed our approach to design by forever uniting
patterns with the evolutionary process of refactoring.
This book introduces the theory and practice of pattern-directed refactorings:
sequences of low-level refactorings that allow designers to safely move
designs to, towards, or away from pattern implementations. Using code from
real-world projects, Kerievsky documents the thinking and steps underlying
over two dozen pattern-based design transformations. Along the way he offers
insights into pattern differences and how to implement patterns in the simplest
possible ways.
Coverage includes:
•
•
•
•
•
•
A catalog of twenty-seven pattern-directed refactorings, featuring
real-world code examples
Descriptions of twelve design smells that indicate the need for this
book's refactorings
General information and new insights about patterns and refactoring
Detailed implementation mechanics: how low-level refactorings are
combined to implement high-level patterns
Multiple ways to implement the same pattern•and when to use each
Practical ways to get started even if you have little experience with
patterns or refactoring
Refactoring to Patterns reflects three years of refinement and the insights of
more than sixty software engineering thought leaders in the global patterns,
refactoring, and agile development communities. Whether you're focused on
legacy or "greenfield"development, this book will make you a better software
designer by helping you learn how to make important design changes safely
and effectively.
Inside Front Cover
2
Contents Previous Next
Inside Front Cover
3
Contents Previous Next
Copyright
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 Addison-Wesley was aware of a
trademark claim, the designations have been printed with initial capital letters or in all capitals.
The author 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.
Figures on pages 26 and 28 (top figure) of this book were previously published on pages 108 and 164
of Design Patterns: Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm,
Ralph Johnson, and John M. Vlissides. Copyright 1995 by Pearson Education, Inc. Republished by
permission of Pearson Education, Inc., Publishing as Pearson Addison-Wesley.
Excerpted text on pages 3 and 4 was previously published in Pattern Languages of Program Design 3 by
Robert Martin, Dirk Riehle, and Frank Buschmann (Addison-Wesley, 1998). Republished by
permission. All rights reserved.
The publisher offers discounts on this book when ordered in quantity for bulk purchases and special
sales. For more information, please contact:
U.S. Corporate and Government Sales
(800) 382-3419
corpsales@pearsontechgroup.com
For sales outside of the U.S., please contact:
International Sales
(317) 581-3793
international@pearsontechgroup.com
Visit Addison-Wesley on the Web: www.awprofessional.com
Library of Congress Cataloging-in-Publication Data
Kerievsky, Joshua.
Refactoring to patterns / Joshua Kerievsky.
p. cm. • (Addison-Wesley signature series)
ISBN 0-321-21335-1
1. Software refactoring. 2. Software patterns. I. Title. II. Series.
QA76.76.R42K47 2004
005.1'6•dc22 2004012166
Copyright
4
Copyright © 2005 by Pearson Education, Inc.
All rights reserved. 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, or
otherwise, without the prior consent of the publisher. Printed in the United States of America. Published
simultaneously in Canada.
For information on obtaining permission for use of material from this work, please submit a written
request to:
Pearson Education, Inc.
Rights and Contracts Department
75 Arlington Street, Suite 300
Boston, MA 02116
Fax: (617) 848-7047
Text printed on recycled paper
1 2 3 4 5 6 7 8 9 10•CRW•0807060504
First printing, July 2004
Dedication
For Tracy, Sasha, and Sophia
Contents Previous Next
Dedication
5
Table of Contents
List of Refactorings
Refactoring Directions
Code Smells
A Study Sequence
Afterword
Inside Back Cover
References
Chapter 1. Why I Wrote This Book
•
•
•
•
•
•
•
•
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
Over-Engineering
The Patterns Panacea
Under-Engineering
Test-Driven Development and Continuous Refactoring
Refactoring and Patterns
Evolutionary Design
Chapter 2. Refactoring
The Benefits of Composite Refactorings
Refactoring Tools
What Is Refactoring?
What Motivates Us to Refactor?
Many Eyes
Human-Readable Code
Keeping It Clean
Small Steps
Design Debt
Evolving a New Architecture
Composite and Test-Driven Refactorings
Chapter 3. Patterns
What Is a Pattern?
Patterns Happy
There Are Many Ways to Implement a Pattern
Refactoring to, towards, and away from Patterns
Do Patterns Make Code More Complex?
Pattern Knowledge
Up-Front Design with Patterns
Chapter 4. Code Smells
Table 4.1.
Switch Statements
Combinatorial Explosion
Oddball Solution
Duplicated Code
Long Method
Conditional Complexity
Primitive Obsession
Indecent Exposure
Solution Sprawl
Alternative Classes with Different Interfaces
Lazy Class
Large Class
Table of Contents
6
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
Chapter 5. A Catalog of Refactorings to Patterns
Format of the Refactorings
Benefits and Liabilities
Projects Referenced in This Catalog
A Starting Point
A Study Sequence
Chapter 6. Creation
Replace Constructors with Creation Methods
Creation Methods and Factory Methods
Benefits and Liabilities
Mechanics
Move Creation Knowledge to Factory
What Is a Factory?
Benefits and Liabilities
Mechanics
Encapsulate Classes with Factory
Benefits and Liabilities
Mechanics
Introduce Polymorphic Creation with Factory Method
Benefits and Liabilities
Mechanics
Encapsulate Composite with Builder
Benefits and Liabilities
Mechanics
Inline Singleton
Ward Cunningham on Singletons
Kent Beck on Singletons
Benefits and Liabilities
Mechanics
Chapter 7. Simplification
Compose Method
¤
Benefits and Liabilities
Mechanics
Replace Conditional Logic with Strategy
Benefits and Liabilities
Mechanics
Move Embellishment to Decorator
Decorator versus Strategy
Benefits and Liabilities
Mechanics
Replace State-Altering Conditionals with State
Benefits and Liabilities
Mechanics
Replace Implicit Tree with Composite
Benefits and Liabilities
Mechanics
Replace Conditional Dispatcher with Command
Benefits and Liabilities
Mechanics
Chapter 8. Generalization
Form Template Method
Table of Contents
7
Benefits and Liabilities
Mechanics
Extract Composite
Benefits and Liabilities
Mechanics
Replace One/Many Distinctions with Composite
Benefits and Liabilities
Mechanics
Replace Hard-Coded Notifications with Observer
Benefits and Liabilities
Mechanics
Unify Interfaces with Adapter
Benefits and Liabilities
Mechanics
Extract Adapter
Adapter and Facade
Benefits and Liabilities
Benefits and Liabilities
Mechanics
Chapter 9. Protection
Mechanics
Replace Implicit Language with Interpreter
Replace Type Code with Class
Benefits and Liabilities
Mechanics
Limit Instantiation with Singleton
Benefits and Liabilities
Mechanics
Introduce Null Object
Benefits and Liabilities
Mechanics
Background for the Example: Mouse Events in Java 1.0
Chapter 10. Accumulation
Move Accumulation to Collecting Parameter
Benefits and Liabilities
Mechanics
Move Accumulation to Visitor
Benefits and Liabilities
Mechanics
Chapter 11. Utilities
Chain Constructors
Unify Interfaces
Extract Parameter
Copyright
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
¤
Dedication
The Addison-Wesley Signature Series Signers: Kent Beck and Martin Fowler
What Is This Book About?
What Are the Goals of This Book?
Who Should Read This Book?
What Background Do You Need?
How to Use This Book
Table of Contents
8