Refactoring Class To Be Polymorphic

(...a word in the RefactoringLanguage.)

(...or "Refactoring Concrete Class Into Polymorphic Implementation")

Problem
You're using a ConcreteClass? and find that you wish to provide an alternate implementation of the abstraction.
Solution
Split the class into interface and implementation. This will enable you to provide an alternate implementation of the interface.

Steps:
  >  Change all "ConcreteClass?" references to "NewInterfaceName?"
      ...except for object creation references  ("news")
           Change these to "ImplementationClass?."
  >  Run your UnitTests.

(Don't tell me about Class-FactoryPattern-s! YouArentGonnaNeedIt! OK; if you need it later, you can HaveThisPattern -- the FactoryPattern, if you want. ;-)


This RefactoringPattern breaks a ConcreteClass? into an InterfaceDefinition? (AbstractBaseClass in some languages) and an ImplementationClass?.

You can follow this by adding alternate ImplementationClass?-es, each implementing the common InterfaceDefinition? to achieve Polymorphism.

You can extend with one of the several different types of FactoryPattern, allowing more flexible selection of an appropriate ImplementationClass? in each instance.

This pattern uses the simple RenameClass refactoring move, altering it slightly to factor out the reusable InterfaceDefinition?.

EditText of this page (last edited February 27, 2003) or FindPage with title or text search