Refactoring Class To Be Polymorphic

(...a word in the RefactoringLanguage.)

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

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

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

View edit of February 27, 2003 or FindPage with title or text search