Cover of Refactoring by Martin Fowler, Kent Beck - Business and Economics Book

From "Refactoring"

Author: Martin Fowler, Kent Beck
Publisher: Addison-Wesley Professional
Year: 1999
Category: Computers

🎧 Free Preview Complete

You've listened to your free 10-minute preview.
Sign up free to continue listening to the full summary.

🎧 Listen to Summary

Free 10-min Preview
0:00
Speed:
10:00 free remaining
Chapter 12: Dealing with Inheritance
Key Insight 2 from this chapter

Refactoring 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.