DEV Community

Philip Perry
Philip Perry

Posted on • Originally published at on


Refactorings with Design Patterns

I’ve previously written about refactorings I found useful. But at the time I didn’t include refactorings that use design patterns, as I didn’t really understand them yet. Now that I’ve read a book on design patterns, these will hopefully make more sense. Actually there is a book on Refactoring to Patterns, which I haven’t read yet. As a first step I want to just list the refactorings using patterns that are mentioned in the course, so that I can easily find them later:

Duplicate Observed Data (Observer pattern)

If domain data stored in classes is responsible for the GUI then it is a good idea to separate the data into separate classes, ensuring connection and synchronization between the domain class and the GUI.

Replace Type Code with State/Strategy

This refactoring technique is a way out of situations when a field with a coded type changes its value during the object’s lifetime. In this case, replacement of the value is made via replacement of the state object to which the original class refers.

Replace Constructor with Factory Method

Useful if one has a complex constructor that does something more than just setting parameter values in object fields.

Form Template Method

Merge the shared algorithm steps in a superclass and leave just the differences in the subclasses.

Replace Inheritance with Delegation (Strategy pattern)

If inheritance doesn’t make sense, e.g. there is a subclass that uses only a portion of the methods of its superclass one should consider replacing inheritance with composition.

Oldest comments (0)