Tinus Tate Tinus Tate - 17 days ago 7
Java Question

Implementing interface with many methods = huge class. Patterns come to rescue?

I have an interface with a lot of methods. (which i cannot split into different interfaces)

When i create a class that implements the interface i get a lot of methods in 1 class file.

Things get worst when the body of all these methods get larger -> the class file becomes huge and quite difficult to navigate.

Even with ide's like eclipse because the Outline window containing all classes get a vertical scrollbar because not all methods can fit in the outline.

Is there a pattern that prevents this from happening?

Answer

I do not quite understand why you cannot split the interface into multiple ones...

I surely would try to use inheritance, like:

First interface:

public interface FatherInterface {
    String methodOne(String var);

    String methodTwo(String var);

    String methodThree(String var);
}

Second interface:

public interface SonInterface extends FatherInterface {
    String methodFour(String var);

    String methodFive(String var);
}

Third interface:

public interface SecondSonInterface extends SonInterface {
    String methodSix(String var);

    String methodSeven(String var);
}

And so on... Each interface inheriting from the preceding one.

As for class files becoming large, go with inheritance too.

Father class:

public class Father implements FatherInterface

Son class:

public class Son extends Father implements SonInterface

And so on...


Edit

If you cannot split the interface (as when given by third party), I would do the implementations of the methods by parts. That is, only some of them implemented in each class. Using abstract classes if needed (or leaving blank methods). Each class inheriting from the above and implementing some of the remaining methods.