Is there a preferred method or style for creating a default implementation for interface methods? Suppose I had a widely used interface, where in 90% of cases the functionality that I wanted was identical.
My first instinct is to create a concrete class with static methods. Then I would delegate functions to static methods when I need the default functionality.
Here is a simple example:
Interface
public interface StuffDoer{ public abstract void doStuff(); }
Concrete implementation of methods
public class ConcreteStuffDoer{ public static void doStuff(){ dosomestuff... } }
Specific implementation using standard features
public class MyClass implements StuffDoer{ public void doStuff(){ ConcreteSuffDoer.doStuff(); } }
Is there a better approach here?
EDIT
Having seen several proposed solutions, I think that I should more clearly tell about my intentions. Essentially, I'm trying to work on Java without allowing multiple inheritance. Also, to be clear, I'm not trying to make an expression about whether Java should allow multiple inheritance. I'm just looking for the best way to create a default implementation of a method for classes that implement an interface.
java design design-patterns
N8g Aug 6 '10 at 19:27 2010-08-06 19:27
source share