Overview
A few months back I reviewed a great book about design patterns: Design Patterns In C# (Addison-Wesley). It was a nice introduction to the OO world where design patterns play a big role and I learned what I can get if I use the patterns. However, it also raised a big bunch of questions. I understood the objective, but I had no idea what I needed to do to start making my design better.
The main questions were whether I should write my code to target the patterns from the beginning or is there some other process involved and if yes then what? And if I do, how should I see the patterns being part of my design? When should I use them and when not? It was clear that patterns are the tool to help me, but there was no road map to see how to use that tool. I simply didn’t know how to start adapting the patterns to my current way of working. It seemed difficult to see a clean pattern design when the workings of the application itself weren’t clear.
Of course, Design Patterns In C# provides good references to further information and I was tempted to look for material about refactoring. So I picked up Refactoring To Patterns , a book written by Joshua Kerievsky and part of Martin Fowler Signature Series. The book seemed to be exactly what I was looking for. It promised to explain the connection between design patterns and refactoring. It would demonstrate how to apply design patterns in practice and when to use patterns and when not. To borrow a sentence from the book “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”. And actually being quite rare in current software books, Refactoring To Patterns contained real-world code examples written in Java.
Basic Information
The book has 367 pages in 11 chapters. The book is a hard cover.
Foreword by Ralph Johnson
Foreword by Martin Fowler
Preface
Chapters
1. Why I wrote this book
In this chapter author explains the reasons for the book. He tells how he started to use patterns and adopted extreme programming practices. He also explains the background how developers usually either over-engineer or under-engineer applications when targeting to patterns and he also mentions what test-driven development is and how it came onto his path.
2. Refactoring
In this chapter the author explains in detail what refactoring means. He covers the motivation to use refactoring and what it means when you have clean, human-readable code and how to evolve to refactoring with small steps.
3. Patterns
Similarly to the previous chapter, this one covers background of design patterns. Patterns are defined in detail and the author explains the theory behind refactoring to, towards and away from patterns as well as what it means when you apply patterns to your code.
4. Code Smells
In this chapter the most common design problems are introduced and the relationship of patterns and refactoring to solve these problems is explained.
5. A Catalog of Refactorings to Patterns
This chapter lists the standard refactorings used in the book.
6. Creation
This chapter covers patterns and refactorings related to object creation. Following refactorings are covered:
- Replace Constructors with Creation Methods
- Move Creation Knowledge to Factory
- Encapsulate Classes with Factory
- Introduce Polymorphic Creation with Factory Method
- Encapsulate Composite with Builder
- Inline Singleton
7. Simplification
This chapter covers patterns and refactorings oriented to simplify methods, state transitions and tree structures. Following refactorings are covered:
- Compose Method
- Replace Conditional Logic with Strategy
- Move Embellishment to Decorator
- Replace State-Altering Conditionals with State
- Replace Implicit Tree with Composite
- Conditional Dispatcher with Command
8. Generalization
This chapter covers patterns and refactorings oriented to remove duplicated code. Following refactorings are covered:
- Form Template Method
- Extract Composite
- Replace One/Many Distinctions with Composite
- Replace Hard-Coded Notifications with Observer
- Unify Interfaces with Adapter
- Extract Adapter
- Replace Implicit Language with Interpreter
9. Protection
This chapter covers patterns and refactorings related to improving the protection of existing code. Following refactorings are covered:
- Replace Type Code with Class
- Limit Instantiation with Singleton
- Introduce Null Object
10. Accumulation
This chapter covers patterns and refactorings oriented to improve code that accumulates information within an object or across several objects. Following refactorings are covered:
- Move Accumulation to Collecting Parameter
- Move Accumulators to Visitor
11. Utilities
This chapter covers refactorings which are low-level transformations used by higher-level refactorings in the book. Following refactorings are covered:
- Chain Constructors
- Unify Interfaces
- Extract Parameter
Afterword by John Brant and Don Roberts
References
Index
The publisher, Addison-Wesley has a web site for the book at:
http://www.awprofessional.com/title/0321213351
Pros
Yes, this is the book. It did answer all of my questions. The book explains refactoring in a thorough and detailed manner. The examples are good and the way they are used to demonstrate refactorings is unusually clear. I could praise the book for a few pages but I will say that you really should read it yourself if you are interested in the subject. Even though the book used Java and my background is in .NET, it didn’t matter at all. If you know C# or VB.NET, it is easy to understand Java. Things are covered in a universal, platform-independent way and all that matters is that you understand the principle behind OO and patterns.
Cons
Only downside was that on some examples some code was cut off and taken off from the context to keep the example concise and having it all would actually helped to keep things in context and to understand the refactoring even better.
Conclusion
Refactoring To Patterns is a first class masterpiece written with such passion and knowledge that you seldom see. If you have trouble understanding the practical side of design patterns, this book is for you.