Refactoring Push Down Field
ProblemIs a field used only in a few subclasses?
SolutionMove the field to these subclasses.
Although it was planned to use a field universally for all classes, in reality the field is used only in some subclasses. This situation can occur when planned features fail to pan out, for example.
This can also occur due to extraction (or removal) of part of the functionality of class hierarchies.
Improves internal class coherency. A field is located where it is actually used.
When moving to several subclasses simultaneously, you can develop the fields independently of each other. This does create code duplication, yes, so push down fields only when you really do intend to use the fields in different ways.
How to Refactor
Declare a field in all the necessary subclasses.
Remove the field from the superclass.
Tired of reading?
No wonder, there are 7 hours worth of the text on this website.
Try out something different. We've just launched the interactive learning course on refactoring. It has more content and much more fun than a boring text.Learn more...