From "Refactoring"
🎧 Listen to Summary
Free 10-min PreviewRefactoring by Moving Specific Elements Down the Hierarchy
Key Insight
Refactoring by moving specific elements down the hierarchy enhances clarity and reduces coupling within class structures. When a method or field is only relevant to one or a small subset of subclasses, removing it from the superclass and placing it solely in the relevant subclass(es) makes the design more explicit. This clarifies that a feature is not universally applicable across the entire hierarchy, thus improving comprehension and maintainability.
'Push Down Method' is applied when a method in a superclass is only utilized by specific subclasses. This refactoring is conditional: it can only be performed if the calling code already knows it is working with an instance of a particular subclass. If not, 'Replace Conditional with Polymorphism' should be considered, potentially with a 'placebo' behavior implemented on the superclass to handle the general case.
The mechanics for 'Push Down Method' involve copying the method into every subclass that requires it. The method is then removed from the superclass, followed by comprehensive testing. Subsequently, the method is removed from any subclass that does not genuinely need it, with further testing to ensure functionality. 'Push Down Field' follows a similar principle, moving fields that are specific to one or a few subclasses. Its mechanics include declaring the field in all necessary subclasses, removing it from the superclass, testing, and then removing it from any superfluous subclass declarations, followed by testing.
📚 Continue Your Learning Journey — No Payment Required
Access the complete Refactoring summary with audio narration, key takeaways, and actionable insights from Martin Fowler, Kent Beck.