开发者

Minimize code duplication when implementing multiple interfaces

I have one main interface 开发者_StackOverflow中文版and an abstract class implementing all "derivable" methods (that can be written using only abstract methods) of it:

public interface Main {
    public void main1(int x);
    public void main2();
}

public abstract class MainAbstract implements Main {
    public void main2() { main1(42); }
}

This functionality can be extended in different "directions":

public interface SubA extends Main {
     public void subA1(int x);
     public void subA2();
}

public interface SubB extends Main {
     public void subB1(int x);
     public void subB2();
}

Now I could have abstract classes SubAAbstract and SubBAbstract implementing all the "derivable" methods (like main2 in Main). The problem is that I may have concrete implementations who want to implement both SubA and SubB, so I could use only one of the abstract classes. In reality the problem is worse, because I have more than 2 sub-interfaces.

I'm aware that there is no way around single inheritance in Java, but I would like to know if there is a good way to minimize code duplication in that case.

[Edit]

Would it be an abomination to put all functionality in the MainAbstract class (note that it only implements Main), e.g.

public abstract class MainAbstract implements Main {

    public void main2() {
        main1(42);
    }

    public void subA1(int x) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void subA2() {
        subA1(4711);
    }

    public void subB1(int x) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void subB2() {
        subB1(-1);
    }

}

Then a concrete class needs just to implement the needed interfaces, and to implement the needed methods:

public class MainConcrete extends MainAbstract implements Main, SubA {

    public void main1(int x) {
        System.out.println("main " + x);
    }

    public void subA1(int x) {
        System.out.println("subA" + x);
    }

}

Of course this would only make sense if the hierarchy is rather stable (that means, the sub interfaces are known exactly before), but this would be the case for me. Having the superfluous methods visible isn't nice either, but I don't seee a way to change that.


I would like to know if there is a good way to minimize code duplication in that case.

I believe the standard answer would be to use object composition.

Related questions:

  • How can interfaces replace the need for multiple inheritance when have existing classes (Have a look at the first answer for an example of object composition.)
  • Alternative of Multiple inheritance in Java
  • How do I implement multiple inheritence in Java
  • Multiple inheritance design issue in Java


Externalize the work to utility classes, builders etc.


With objweb asm bytecode-library, you could write an utility that reads the code from all methods, and fields of all your implementing classes and puts them into one class.

The util would look something like this public class ClassUtil {

public static Class<?> unifyClass(Class<?>...classes) {
    return ...;
}
}

And then

Class<?> subAllClass = unifyClass(SubA.class, SubB.class, ...);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜