Alternative Classes with Different Interfaces


Symptoms

  • Two classes seem to be doing the same thing but are using different method names .

Causes

People create similar code to handle a similar situation, but don't realize the other code exists.

What to Do

Harmonize classes so that you can eliminate one of them.

  1. Rename Method to make method names similar.

  2. Move Method, Add Parameter, and Parameterize Method to make protocols (method signatures and approach) similar.

  3. If the two classes are similar but not identical, use Extract Superclass once you have them reasonably well harmonized.

  4. Remove the extra class if possible.

Payoff

Reduces duplication. May also reduce size. May improve communication (by removing ambiguity about which approach to use).

Contraindications

Sometimes the two classes can't be changed (e.g., if both are in different libraries). Each library may have its own vision for the same concept, but you may be left with no good way to unify them.



Refactoring Workbook
Refactoring Workbook
ISBN: 0321109295
EAN: 2147483647
Year: 2003
Pages: 146

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net