Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Interface-Implementation Pair [closed]

Is there a preferred method or style of creating a default implementation for interface methods? Suppose I had a commonly used interface where in 90% of the cases the functionality I wanted was identical.

My first instinct is to create a concrete class with static methods. I would then delegate the functionality to the static methods when I want 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...
    }
}

Concrete Implementation using default functionality

public class MyClass implements StuffDoer{
    public void  doStuff(){
        ConcreteSuffDoer.doStuff();        
    } 
}

Is there a better approach here?

EDIT

After seeing a few of the proposed solutions I think I should be more clear about my intent. Essentially I am trying to work around Java not allowing multiple inheritance. Also to be clear I am not trying to make a statement about whether or not Java should allow multiple inheritance. I am just looking for the best way to create a default method implementation for classes implementing an interface.

like image 437
N8g Avatar asked Aug 06 '10 19:08

N8g


2 Answers

This is the approach I would take:

public interface MyInterface {

      MyInterface DEFAULT = new MyDefaultImplementation();

      public static class MyDefaultImplemenation implements MyInterface {
      }
 }

Of course, the MyDefaultImplementation may need to be private, or its own top level class, depending on what makes sense.

You can then have the following in your implementations:

 public class MyClass implements MyInterface {
        @Override
        public int someInterfaceMethod(String param) {
             return DEFAULT.someInterfaceMethod(param);
        }
  }

Its a bit more self-documenting, and ultimately more flexible, than a default implementation class that exists elsewhere but is not referenced by the interface. With this you can do things like just pass the default implementation as a method parameter when required (which you cannot do with the static methods).

Of course, the above only works if there is no state involved.

like image 57
Yishai Avatar answered Sep 20 '22 21:09

Yishai


You could turn the interface into an abstract class, and provide default implementation for the methods as appropriate.

Update: I see, the multiple inheritance closes out changing the interface into an abstract class... in this case I would do the same as you. If the default implementation of the method(s) is not state-dependent, the best place for them is indeed in a static utility class. However, if there is state involved, I would consider object composition, which could even end up as something like a Decorator.

like image 28
Péter Török Avatar answered Sep 19 '22 21:09

Péter Török