Joshua Kerievsky: Refactoring to Patterns Addison Wesley Signature Series, Software development 15

Transcription

Joshua Kerievsky: Refactoring to Patterns Addison Wesley Signature Series, Software development 15
Joshua Kerievsky: Refactoring to Patterns
Addison Wesley Signature Series,
Software development 15 th annual productivity award
1
Creation
1.1 Replace (one class) Constructors with intention-revealing Creation Methods that return
object instances (p. 66)
Constructor on a class make it hard to decide which constructor to call during development.
1.2 Move creation Knowledge into a single Factory class (p. 68)
Data and code used to instantiate a class is sprawled across numerous classes.
1.3 Encapsulate classes with Factory, make their constructors non public and let clients create
instances of them using a Factory (p. 80)
Clients directly instantiate classes that reside in one package and implement a common
interface.
1.4 Introduce Polymorphic Creation with Factory Method (p.88)
Make a single superclass version of the method that calls a Factory Method to handle the
instantiation
Classes in a hierarchy implement a method similarly, except for an object creation step.
1.5 Encapsulate Composite with Builder
Simplify the build by letting a Builder handle the details (p.96)
Building a Composite is repetitive, complicated, or error-prone.
Builder (Gamma et al.: Design Patterns)
1.6 Inline Singletone
Move the Singleton’s features to a class that stores and provides Access to the object.
Delete the Singleton (p.114)
Code needs Access to an object but doesn’t need a global point of Access to it.
(Singletonitis, Singleton abusers)
2
Simplification
2.1 Compose Method
Transform the logic into a small number of intention-revealing steps at the same level of
detail (p. 123)
You can’t rapidly understand a method’s logic.
2.2 Replace Conditional Logic with Strategy
Create Strategy for each variant and make the method delegate the calculation to
a Strategy instance (p.129)
2.3 Move the embellishment code to a Decorator
Place each embellishment in its own class and let that class wrap the type of object it
needs to embellish (pp. 144, 145, 147)
Decorator (Gamma et al.: Design Patterns)
2.4 Replace State-Altering Conditionals with State (p. 166)
Replace the conditionals that handle specific states and transition between them
2.5 Replace your implicit form of a tree structure
(using primitive representation, such as a String) with a Composite (p. 178, p.189?)
2.6 Replace Conditional Dispatcher with Command
Create a Command for each action. Store the Commands in a collection to fetch and
execute them (p. 191)
Conditional logic is used to dispatch requests and execute actions.
3
Generalization
3.1 Form Template Method
Generalize the methods by extracting their steps into methods with identical structures,
then pull up the generalized methods to form a Template Method (p. 205)
Two methods in subclasses perform similar steps in the same order, yet the steps are different.
(Same steps in different order?)
3.2 Extract Composite
Extract the superclass that implements the Composite (p. 214)
Subclasses in a hierarchy implement the same Composite.
3.3 Replace One/Many Distinction with Composite to produce one piece of code
capable of handling single or multiple objects (p. 224)
A class processes single and multiple objects using separate pieces of code.
3.4 Replace Hard-Coded Notification with Observer (p. 236)
3.5 Unify Interfaces with Adapter (p. 247)
Clients interact with two classes, one of which has a preferred interface.
Adapter (Gamma et al.: Design Patterns)
3.6 Extract Adapter (p.258)
3.7 Replace Implicit Language with Interpreter (p. 269)
4
Protection
4.1 Replace Type Code with Class
Constrain the assignments and equality comparisons (p. 286)
4.2 Limit Instantiation with Singleton (p. 296)
Code creates multiple instances of an object, and that uses too much memory or slows system
performance.
4.3 Replace the null logic with a Null Object, an object that provides the appropriate null
behavior (pp. 301, 303)
5
Accumulation
5.1 Move Accumulation (single bulky method that accumulates information to a local
variable) to Collecting Parameter (Kent Beck: Smalltalk Best Practice Patterns) that gets
passed to extracted methods (p. 313)
5.2 Move Accumulation to Visitor that can visit each class to accumulate the information
(pp. 320, 321, 322)
Visitor (Gamma et al.: Design Patterns)
6
Utilities
6.1 Chain the Constructors that contain duplicate code together to obtain the least amount of
duplicated code (p. 340)
6.2 Unify Interfaces
Find all public methods on the subclass that are missing on the superclass/interface. Add
copies of these missing methods to the superclass, altering each one to perform null
behavior (p.343)
6.3 Extract Parameter (p. 346)