Unify Interfaces

Prev don't be afraid of buying books Next

You need a superclass and/or interface to have the same interface as a subclass.

Find all public methods on the subclass that are missing on the superclass/interface. Add copies of these missing methods to the superclass, altering each one to perform null behavior.


To process objects polymorphically, the classes of the objects need to share a common interface, whether a superclass or an actual interface. This refactoring addresses the case when a superclass or interface needs to have the same interface as a subclass.

I came across the need for this refactoring on two separate occasions. Once when I was applying Move Embellishment to Decorator (144), an emerging Decorator [DP] needed the same interface as a subclass. The easiest way to make that happen was to apply Unify Interfaces. Similarly, during an application of the refactoring Move Accumulation to Visitor (320), duplicate code could be removed if certain objects shared the same interface, which Unify Interfaces made possible.

After applying this refactoring on a superclass and subclass, I sometimes apply Extract Interface [F] on the superclass to produce a stand-alone interface. I usually do this when an abstract base class contains state fields and I don't want implementors of the common base class, such as a Decorator, to inherit those fields. See Move Embellishment to Decorator (144) for an example.

Unify Interfaces is often a temporary step on the way to somewhere else. For example, after you perform this refactoring, you may perform a sequence of refactorings that allows you to remove methods you added while unifying your interfaces. Other times, a default implementation of a method on an abstract base class may no longer be needed after applying Extract Interface [F].


Find a missing method, a public method on the subclass that isn't declared on the superclass and/or interface.

1. Add a copy of the missing method to the superclass/interface. If you're adding the missing method to a superclass, modify its body to perform null behavior.

  • Compile.

Repeat until the superclass/interface and subclass share the same interface.

  • Test that all code related to the superclass works as expected.


I need to unify the interfaces of a subclass called StringNode and its superclass, AbstractNode. StringNode inherits most of its public methods from AbstractNode, with the exception of one method:

 public class StringNode extends AbstractNode...     public void accept(textExtractor: TextExtractor) {        // implementation details…     } } 

  1. I add a copy of the accept(…) method to AbstractNode, modifying its body to provide null behavior:

     public abstract class AbstractNode...     public void accept(textExtractor: TextExtractor) {     } 

    At this point, the interfaces of AbstractNode and StringNode have been unified. I compile and test to ensure that everything works fine. It does.


Refactoring to Patterns (The Addison-Wesley Signature Series)
Refactoring to Patterns
ISBN: 0321213351
EAN: 2147483647
Year: 2003
Pages: 103

Similar book on Amazon

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