1

I have a very large and bloated class and I want to split it into separate files, but it should be completely transparent to the user and compatible with existing projects that use the class.

In particular, I have my own ImageMatrix class and it defines a ton of unary functions, a ton of binary functions with a scalar, a ton of binary functions with another image, etc. To keep the class clean and maintainable, I wish to put each class of operators in a separate file.

Is there a way to just cut/paste these methods into a file and include them in the source?

So I wish that I can still do this, but the methods actually reside in different files:

    ImageMatrix img = new ImageMatrix(800, 600, 3);
    img.clear(0.5f, 0.0f, 0,0f);
    img.addSelf(anotherImg);
    img.normalize();
    img.abs();
    img.addSelf(0.5);

Each method is around 15-30 lines of code because the implementations are extremely optimized for performance, the like of which is impossible to achieve with Bufferedimage or even a plain int[]. There is a lot of code duplication, which is the main reason to group similar methods together. If I add a function or if I change global behavior (like error checking), I can easily keep things coherent.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mark Jeronimus
  • 9,278
  • 3
  • 37
  • 50

4 Answers4

2

Unfortunately it is not possible to split a Java class definition into multiple files.

Try to restructure your code so that a huge class definition isn't necessary. Often this means exposing some state through getter and setter methods, and writing supporting classes which use these methods to add functionality to your main class.

In your case, you might consider adding supporting classes like ImageFilters, or even something more narrow like ImageNormalizer if you like very small classes.

Daniel Lubarov
  • 7,796
  • 1
  • 37
  • 56
1

You can't split a Java class across files. If each of your methods is self-contained, you could create classes with static methods. So to a certain extent, you would only need to cut and paste code.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
npinti
  • 51,780
  • 5
  • 72
  • 96
  • I thought about that, but it isn't transparent to the user. However, in accordance with phlogratos' answer I did this anyway but also made delegate methods in the main class, so it becomes transparent. – Mark Jeronimus Apr 05 '12 at 11:37
  • @Zom-B: Yes that is why I mentioned `to a certain extent`. You then had to implement one large wrapper class for everything. – npinti Apr 05 '12 at 11:52
1

Well, you could create a more generic class and put more generic methods there and then have ImageMatrix extends that one. Also, if you have a lot of matrix manipulation functions you will probably end up with a lot of code duplication, i.e. repeating the same code in different methods instead of moving the common code into an aux method and calling it from different places, etc.

scibuff
  • 13,377
  • 2
  • 27
  • 30
1

You can also use delegate methods. This means the definitions of your methods are still in ImageMatrix, but they are implemented in another class. Depending on the complexity of your methods this could reduce the amount of code in ImageMatrix.

class ImageMatrix {

    MatrixHelperOne helperOne = new MatrixHelperOne();

    ...

    public void complexMethod1(Arg arg) {
        helperOne.complexMethod1(arg);
    }

    ...
}
phlogratos
  • 13,234
  • 1
  • 32
  • 37