Refactoring Workbook

Symptoms

  • A class accesses internal (should-be-private) parts of its parent. (There is a related form of Inappropriate Intimacy between separate classes; see "Inappropriate Intimacy [General Form]," Chapter 10.)

Causes

It's natural that parent and child classes be more coupled together than two strangers. Sometimes this just goes too far.

What to Do

  • If the subclass is accessing the parent's fields in an uncontrolled way, use Self Encapsulate Field.

  • If the parent can define a general algorithm that the children can plug into, then use Form Template Method.

  • If the parent and child need to be even more decoupled, then Replace Inheritance with Delegation .

Payoff

Reduces duplication. Often improves communication. May reduce size.

Contraindications

None identified.

Категории