What Is Refactoring To Patterns?

Refactoring to Patterns is 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 refactoring

Learn why using patterns to improve existing code is better than using patterns early in a new design

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

A suggested study sequence for the refactorings

Who Is This Book For?

This book is for object-oriented programmers engaged in or interested in improving the design of existing code. Such programmers either

Use patterns and/or practice refactoring, but have never implemented patterns by refactoring

Know little about refactoring and patterns and would like to learn more

The types of projects for which this book is useful are:

Greenfield development, in which a new system or feature is being written from scratch

Legacy development, in which you are mostly maintaining a legacy system.

Whether you are doing greenfield or legacy development, the refactorings and design ideas in this book will help you with your work.

What Background Do You Need?

This book assumes you are familiar with design concepts like tight- or loose-coupling and 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#, VB.NET, Python, Ruby, Smalltalk or some other object-oriented language, you ought 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 [F]

Extract Interface [F]

Extract Superclass [F]

Extract Subclass [F]

Pull Up Method [F]

Move Method [F]

Rename Method [F]

It also contains references to more sophisticated refactorings, such as

Replace Inheritance with Delegation [F]

Replace Conditional with Polymorphism [F]

Replace Type Code with Subclasses [F]

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 above refactorings are implemented. However, if you want to get the most out of this book, I do recommend that you have Refactoring [F] 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 or towards 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'd 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. Whie writing this book, I kept the third edition of Fowler's UML Distilled close by my side and referred to it often.

Advance Praise

“For refactoring to be valuable it must be going somewhere, not just an abstract intellectual exercise. Patterns document program structures with known good properties. Put the two together and you have Refactoring to Patterns. If you want your refactorings to go somewhere, I suggest you read and apply Refactoring to Patterns.” —Kent Beck, Director, Three Rivers Institute

“In the GoF book we claimed that design patterns are targets for refactorings. This book finally shows that we didn’t lie. By doing so, Joshua’s book will deepen your understanding of both refactoring and design patterns.” —Erich Gamma, Eclipse Java Development Tools lead, IBM

“Now the connection between software patterns and agile development is finally told.” —Ward Cunningham

“Refactoring to patterns is a revolutionary approach to applying patterns that combines the top-down utility of design patterns with the bottom-up discovery of iterative development and continuous refactoring. Any serious software developer should be using this approach to discover new opportunities to use patterns to improve their code.” —Bobby Woolf, Consulting I/T Specialist, IBM Software Services for Web- Sphere, and coauthor of Enterprise Integration Patterns (Addison-Wesley) and The Design Patterns Smalltalk Companion (Addison-Wesley).

“With this unique catalog of design-level refactorings Joshua Kerievsky has given refactoring an entirely new dimension. Refactoring to Patterns shows developers how to make design-level improvements that simplify everyday work. This book is an invaluable reference for the refactoring practitioner.” —Sven Gorts

“This book refactors and restructures GoF, and much more. Refactoring to Patterns takes a subject that has been presented as static and rigid and makes it dynamic and flexible, converting it into a human process with experiments, mistakes, and corrections so you understand that good designs do not occur by turning some series of cranks—they evolve through struggle and reflection. Kerievsky has also restructured the presentation to make it far clearer and easier to assimilate. Indeed, he has solved a number of the organization problems that I have struggled with in Thinking in Patterns. This book is a clear introduction and combination of the disciplines of testing, refactoring, and patterns, and it is filled with easy reading, good sense, and great insights.” —Bruce Eckel, President of Mindview, Inc., and author of Thinking in Java/Thinking in C++ (Prentice Hall)

“The first time I met Joshua, I was struck by the depth of his passion for understanding, applying, and teaching design patterns. Great teachers care deeply about their subject and how to share it. I think Joshua is a great teacher—and developer—and that we can all benefit from his insight.” —Craig Larman, Chief Scientist, Valtech, and author of Applying UML and Patterns, Second Edition (Prentice Hall) and Agile and Iterative Development (Addison-Wesley)

“Refactoring to Patterns is important not only because it provides step-by-step instructions on how to improve your code through the methodical introduction of appropriate patterns, but more so because it teaches the principles that underlie the design patterns implemented. This book should be useful for novice and expert designers alike. This is a great book.” —Kyle Brown, IBM Software Services for WebSphere, and author of Enterprise Java™ Programming with IBM® WebSphere®, Second Edition (Addison-Wesley)

“Mastering a trade means more than just having the right tools—you also need to use them effectively. Refactoring to Patterns explains how to wield industrial-strength design tools with the skills of an artist.” —Russ Rufer, Silicon Valley Patterns Group

“Josh uses patterns to guide the small steps of refactoring toward larger goals and uses refactoring to introduce patterns into your code as it evolves. You’ll learn how to make large improvements to existing code incrementally, rather than trying to force-fit a prefabricated solution. As the code changes you’ll go beyond seeing better designs—you’ll experience them.” —Phil Goodwin, Silicon Valley Patterns Group