Mark Jeronimus
Mark Jeronimus

Reputation: 9541

$Include #include equivalent in Java

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.

Upvotes: 1

Views: 779

Answers (4)

npinti
npinti

Reputation: 52185

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.

Upvotes: 1

phlogratos
phlogratos

Reputation: 13924

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);
    }

    ...
}

Upvotes: 1

Daniel Lubarov
Daniel Lubarov

Reputation: 7924

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.

Upvotes: 2

scibuff
scibuff

Reputation: 13755

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.

Upvotes: 1

Related Questions