What Is This Book About?
This book is about the marriage of refactoring—the process of improving the design of existing code—with patterns, the classic solutions to recurring design problems. Refactoring to Patterns suggests that using patterns to improve an existing design is better than using patterns early in a new design. This is true whether code is years old or minutes old. We improve designs with patterns by applying sequences of low-level design transformations, known as refactorings.
What Are the Goals of This Book?
This book was written to help you:
• Understand how to combine refactoring and patterns
• Improve the design of existing code with pattern-directed refactorings
• Identify areas of code in need of pattern-directed refactorings
• Learn why using patterns to improve existing code is better than using patterns early in a new design
To achieve these goals, this book includes the following features:
• A catalog of 27 refactorings
• Examples based on real-world code, not the toy stuff
• Pattern descriptions, including real-world pattern examples
• A collection of smells (i.e., problems) that indicate the need for pattern-directed refactorings
• Examples of different ways to implement the same pattern
• Advice for when to refactor to, towards, or away from patterns
To help individuals or groups learn the 27 refactorings in the book, you’ll find a suggested study sequence on the inside back cover of the book.
Who Should Read This Book?
This book is for object-oriented programmers engaged in or interested in improving the design of existing code. Many of these programmers use patterns and/or practice refactoring but have never implemented patterns by refactoring; others know little about refactoring and patterns and would like to learn more.
This book is useful for both greenfield development, in which you are writing a new system or feature from scratch, and legacy development, in which you are mostly maintaining a legacy system.
What Background Do You Need?
This book assumes you are familiar with design concepts like tight coupling and loose coupling as well as object-oriented concepts like inheritance, polymorphism, encapsulation, composition, interfaces, abstract and concrete classes, abstract and static methods, and so forth.
I use Java examples in this book. I find that Java tends to be easy for most object-oriented programmers to read. I’ve gone out of my way to not use fancy Java features, so whether you code in C++, C#, Visual Basic .NET, Python, Ruby, Smalltalk, or some other object-oriented language, you ought to be able to understand the Java code in this book.
This book is closely tied to Martin Fowler’s classic book Refactoring [ F ]. It contains references to low-level refactorings, such as:
• Extract Method
• Extract Interface
• Extract Superclass
• Extract Subclass
• Pull Up Method
• Move Method
• Rename Method
Refactoring also contains references to more sophisticated refactorings, such as:
• Replace Inheritance with Delegation
• Replace Conditional with Polymorphism
• Replace Type Code with Subclasses
To understand the pattern-directed refactorings in this book, you don’t need to know every refactoring listed above. Instead, you can follow the example code that illustrates how the listed refactorings are implemented. However, if you want to get the most out of this book, I do recommend that you have Refactoring close by your side. It’s an invaluable refactoring resource, as well as a useful aid for understanding this book.
The patterns I write about come from the classic book Design Patterns [ DP ], as well as from authors such as Kent Beck, Bobby Woolf, and myself. These are patterns that my colleagues and I have refactored to, towards, or away from on real-world projects. By learning the art of pattern-directed refactorings, you’ll understand how to refactor to, towards, or away from patterns not mentioned in this book.
You don’t need expert knowledge of these patterns to read this book, though some knowledge of patterns is useful. To help you understand the patterns I’ve written about, this book includes brief pattern summaries, UML sketches of patterns, and many example implementations of patterns. To get a more detailed understanding of the patterns, I recommend that you study this book in conjunction with the patterns literature I reference.
This book uses UML 2.0 diagrams. If you don’t know UML very well, you’re in good company. I know the basics. While writing this book, I kept the third edition of Fowler’s UML Distilled [ Fowler, UD ] close by my side and referred to it often.
How to Use This Book
To get a high-level understanding of the refactorings in this book, you can begin by studying each refactoring’s summary (see Format of the Refactorings , 47), as well as its Benefits and Liabilities box, which appears at the end of each refactoring’s Motivation section.
To get a deeper understanding of the refactorings, you’ll want to study every part of a refactoring, with the exception of the Mechanics section. The Mechanics section is special. It’s intended to help you implement a refactoring by suggesting what low-level refactorings to follow. To understand a refactoring in this book, you don’t have to read the Mechanics section. You’re more likely to use the Mechanics section as a reference when you’re actually refactoring.
The coding smells described in this book and in Refactoring [ F ] provide a useful way to identify a design problem and find associated refactorings to help fix the problem. You can also scan the alphabetized listing of refactorings (on the inside covers of this book and Refactoring ) to find a refactoring that can help improve a design.
This book documents the refactorings that take a design either to, towards, or away from a pattern. To help you figure out what direction to go in, you’ll find a section on this subject (called Refactoring to, towards, and away from Patterns, 29) as well as a table (listed on the inside front cover) that shows each pattern name and the refactorings you can apply to take a design to, towards, or away from the pattern.
The History of This Book
I began writing this book sometime in 1999. At the time, there were several forces driving me to write about patterns, refactoring, and extreme programming ( XP ) [ Beck, XP ]. First, I was surprised that patterns had not been mentioned in the XP literature. This led me to write a paper called “Patterns & XP ” [ Kerievsky, PXP ] in which I publicly discussed the subject and offered some suggestions on how to integrate these two great contributions to our field.
Second, I knew that Martin Fowler had included only a few refactorings to patterns in Refactoring [ F ], and he clearly stated that he hoped someone would write more. That seemed like a worthwhile goal.
Finally, I noticed that people in The Design Patterns Workshop, a class that my colleagues and I teach, needed more help in figuring out when to actually apply a pattern to a design. It’s one thing to learn what a pattern is and an altogether different thing to really understand when and how to apply the pattern. I thought that these students needed to study real-world examples of cases where applying a pattern to a design makes sense, and thus I began compiling a catalog of such examples.
As soon as I began writing this book, I followed Bruce Eckel’s lead and placed my rather rough contents on the Web to obtain feedback. The Web is indeed a beautiful thing. Many folks responded with suggestions, encouragement, and appreciation.
As my writings and ideas matured, I began presenting the subject of Refactoring to Patterns in conference tutorials and during Industrial Logic’s intensive patterns and refactoring workshops. This led to more suggestions for improvement and many ideas on what programmers needed to understand this subject.
Gradually I came to see that patterns are best viewed in the light of refactoring and that they are destinations best reached by applying sequences of low-level refactorings.
When my writings began to resemble a book, rather than a long paper, I was fortunate enough to have many experienced practitioners review my work and offer suggestions for improvement. You can read more about these folks in the Acknowledgments section.
Standing on the Shoulders of Giants
In the summer of 1995, I walked into a bookstore, encountered the book Design Patterns [ DP ] for the first time, and fell in love with patterns. I wish to thank the authors, Erich Gamma, Richard Helm (whom I still haven’t met), Ralph Johnson, and John Vlissides for writing such an excellent piece of literature. The wisdom you shared in your book has helped me become a much better software designer.
Somewhere around 1996, before he became famous, I met Martin Fowler at a patterns conference. It was to be the beginning of a long friendship. I doubt whether I would have written this book if Martin (and his colleagues, Kent Beck, William Opdyke, John Brant, and Don Roberts) had not written the classic book Refactoring [ F ]. Like Design Patterns , Refactoring utterly changed the way I approach software design.
My writings in this book could only have happened because of the hard work of the authors of Design Patterns and Refactoring . I can’t thank you all enough for your great books.
Acknowledgments
I am most fortunate to have a wife who has lovingly supported me during the writing of this book. Tracy, you are the greatest. I’m looking forward to spending many more decades together.
Our two daughters, Sasha and Sophia, were born during the time I wrote this book. I thank both of them for their patience while their dad wrote and wrote and wrote.
In the 1970s, Bruce Kerievsky, my father, brought my brother and me to work so we could draw pictures of the enormous computers in the refrigerated rooms. He also brought us huge green and white computer listings with our names printed in giant letters. His work inspired me to get into this great field—thank you.
Now that I’m past the gushy thanks to my family, I can focus on the technical acknowledgments. And there are many.
John Brant made the largest contribution to this book. He and his colleague Don Roberts are two of the world’s most knowledgeable refactoring people. John, who reviewed four versions of the evolving manuscript, contributed many great ideas and encouraged me to remove many not-so-great ideas. His insights pervade the mechanics for nearly every refactoring in the catalog. Don, who was busy on other projects, could not contribute as much, yet he reviewed John’s feedback, and I thank him for that. I also thank both of them for writing this book’s Afterword.
Martin Fowler helped with numerous reviews and suggestions, including how to simplify sketches and clarify technical discussions. He helped fix some of my broken UML diagrams and update them to reflect UML 2.0. I’m honored that Martin chose this book for his signature series and grateful for the Foreword he wrote for the book.
Sven Gorts downloaded many versions of the evolving manuscript and sent me a flood of thoughtful feedback. He contributed many useful ideas that have improved this book’s writings, diagrams, and code.
Somik Raha helped a great deal in improving the content of this book. His open source htmlparser project, which he started before he had thoroughly learned patterns, turned out to be a gold mine of code that needed refactorings to patterns. Somik and I paired on many of these refactorings, and I can’t thank him enough for his support, encouragement, and suggestions.
Eric Evans, the author of Domain-Driven Design , provided feedback on early versions of this book. While each of us was writing our books, we would meet at coffee shops in and around San Francisco. Then we would write, exchange laptops, and comment on each other’s writings. Thanks for the feedback and companionship, Eric.
Chris Lopez, who is a member of the Silicon Valley Patterns Group ( SVPG ), provided a great deal of highly detailed and useful feedback on my writings, diagrams, and code. While other members of the SVPG also deserve thanks, Chris went above and beyond the call of duty in his reviews of this book.
Russ Rufer, Tracy Bialik, and the rest of the programmers in the SVPG (including Ted Young, Phil Goodwin, Alan Harriman, Charlie Toland, Bob Evans, John Brewer, Jeff Miller, David Vydra, David W. Smith, Patrick Manion, John Wu, Debbie Utley, Carol Thistlethwaite, Ken Scott-Hlebek, Summer Misherghi, and Siqing Zhang) spent numerous sessions reviewing early and more mature versions of this book. They contributed a good deal of wisdom, which helped me realize what needed to be clarified, extended, or removed. Particular thanks goes to Russ for scheduling so many meetings on the book and to Jeff for recording the group’s thoughts.
Ralph Johnson and his Patterns Reading Group at the University of Illinois at Urbana-Champaign provided incredibly useful feedback on the early version of the manuscript. This group sent me feedback on MP3 files. I spent lots of time listening to their recordings and acting on their advice. I’m particularly thankful to Ralph, Brian Foote, Joseph Yoder, and Brian Marick for their thoughts and suggestions. I know there are others in the group—yet I don’t have your names; I apologize and offer my thanks. Thanks also to Ralph for writing a Foreword to this book.
John Vlissides provided extremely useful feedback on several occasions, including many detailed notes on the first draft of the manuscript. He was enormously encouraging of my work, which helped a great deal.
Erich Gamma made some excellent suggestions for the introductory material in this book, as well as the refactorings.
Kent Beck reviewed several refactorings in this book and contributed a sidebar for Inline Singleton (114) . I thank him for pair-programming with me on the State pattern refactoring during the XP2002 conference in Alghero, Italy.
Ward Cunningham contributed a sidebar for Inline Singleton (114) and provided some helpful, last-minute advice on where to locate a chapter in the introductory material.
Dirk Baumer, the lead programmer for producing automated refactorings in Eclipse, and Dmitry Lomov, the lead programmer for producing automated refactorings in IntelliJ, both contributed valuable insights and suggestions for many of the refactorings in this book.
Kyle Brown reviewed an early first draft of the manuscript and provided some excellent insights.
Ken Shirriff and John Tangney provided extensive, thoughtful feedback on versions of the manuscript.
Ken Thomases pointed out a critical error in an older version of the mechanics for Replace Type Code with Class ( 286 ) .
Robert Hirshfeld helped clarify mechanics in a very early version of Move Embellishment to Decorator (144) .
Ron Jeffries helped me articulate this book’s message by engaging in countless arguments on [email protected] . He also helped me refactor my language in a tricky section of the introductory material for this book.
Dmitri Kerievsky helped me clarify the language in this Preface.
The following folks also provided useful feedback along the way: Gunjan Doshi, Jeff Grigg, Kaoru Hosokawa, Don Hinton, Andrew Swan, Erik Meade, Craig Demyanovich, Dave Hoover, Rob Mee, Benny Sadeh, and Alex Chaffee.
I’m also grateful to the feedback of folks who discussed refactorings in this book on the [email protected] list.
I’d like to thank the students of Industrial Logic’s classes, The Design Patterns Workshop and The Testing & Refactoring Workshop, who provided feedback on the refactorings in this book. Many of you helped me learn what needed to be clarified or added to this book.
A special thanks goes to my editor, Paul Petralia, and his team (Lisa Iarkowski, Faye Gemmellaro, John Fuller, Kim Arney Mulcahy, Chrysta Meadowbrooke, Rebecca Rider, and Richard Evans). When other publishers were competing to publish this book, Paul fought hard to make sure Addison-Wesley won the publishing rights. I’m very grateful that happened. I’ve spent years reading great Addison-Wesley books, and it’s now an honor to be part of the family. Paul has become a friend over the course of writing this book. When he wasn’t haranguing me to hurry up and finish the book, we talked about our kids, hit tennis balls, had a few laughs and some drinks. Thanks, Paul—I’m fortunate to have you as my editor.