2

I'm writing a code in Java, one for creating matrices specifically, and I've many different methods I use. I'm trying to separate like methods into separate files, so they're easier to modify, but I can't come up with a way to do it without horrendous inheritance. Is what I'm trying to do possible, or should I bite the bullet and put them all on one file? For example, I have 4 separate files. The only way I can think of where I don't have to import many different classes is:

MatrixBase --> MatrixSort --> MatrixMethods --> Matrix

I get the feeling that this inheritance for one class is unnecessary, when all I want to do is store similar method in the same file. What is my best solution?

2
  • What is easier if methods are in different files? From my point of view methods should be defined in the logic domain they belong to (methods belonging to a matrix should be defined there). If you are certain that you do not break a logical object into pieces, define the methods in another class as static methods. Commented Mar 11, 2015 at 23:11
  • It's mostly to reduce massive amounts of code in one file. Commented Mar 11, 2015 at 23:20

4 Answers 4

2

Strategy is one of the OOP design patterns that allows to separate methods from objects. Although it's primary purpose is to provide a mechanism of supplying alternative algorithm implementations at runtime, you may want to check if it suits your case.

Sign up to request clarification or add additional context in comments.

Comments

0

You are likely not using the best Object Oriented Design Approach if you have too much code in a single class. Also, importing many classes is fine in Java. However, I will give you a reasonable option within the bounds of your question. hope it helps.

Try using composition in order to make the functions available in a single location while still breaking them logically into separate classes. ... So lets assume you have MatrixMath as your root class to access all of your matrix manipulation. Lets suppose you want to have a collection of algebra and a collection of sorting.

// container that has all of your matrix math objects
public class MatrixMath {
    MatrixMath(){};
    public final MatrixAlgebra algebra = new MatrixAlgebra();
    public final MatrixSort sort = new MatrixSort();
}

// put all of the algebra type functions here
public class MatrixAlgebra {
    MatrixMultiply(){};
    public static Matrix multiply(final Matrix a, final Matrix b) {
        Matrix result;
         // do something
        return result;
    }
}

// and your sorting functions here...
public class MatrixSort {
    ...
}

now to access matrix multiply you can use this:

// create a reference to your MatrixMath class so that you 
// can use your function.   
public class Matrix {
   final MatrixMath math = new MatrixMath();

   Matrix times(Matrix other) {
       return math.algebra.multiply(this,other);
   }
}

If you are truly just making functions and you make everything static you can use static references everywhere.

1 Comment

I did have something like that, but I think I've ultimately decided to just use the minimize method use, since trying to separate the class into different files is proving almost as difficult as actually figuring out the logic.
0

One approach is to make your functions static and move them into static utility classes.

So instead of

Matrix m = ...;
Matrix mt = m.transpose();
float det = mt.determinant();
Vector x = ..;
Vector b = ..;
Vector y = m.solve(x,b);

You end up with

Matrix m = ...
Matrix mt = MatrixGeometry.transpose(m);
float det = MatrixMath.determinant(m);
Vector x = ..;
Vector b = ..;
Vector y = LinearAlgebra.solve(m,x,b);

The fun part is working out which functions should stay member functions, and which should be static.

There is a down-side if you're using inheritance in your Matrix type though. If your heirachy looks like this,

static interface Matrix { ... }
class DenseMatrix implements Matrix { ... }
class SparseMatrix implements Matrix { ... }
class BandedMatrix implements Matrix { ... }

Then you will probably end up with implementations containing type based dispatch:

class LinearAlgebra {
   public static Vector solve(Matrix m, Vector x, Vector b) {
     if(m instanceof DenseMatrix)
       return solveDense((DenseMatrix)m, x, b);
     if(m instanceof SparseMatrix)
       return solveSparse((SparseMatrix)m, x, b);
     ...
   }

   private solveSparse(SparseMatrix m, Vector x, Vector b) {
     ...
   }
}

Which many would consider a "code smell". You can get around this by using a double-dispatch / visitor style in your Matrix class, but that is a bit more pain to set up - and in many cases is not really much more readable/maintainable.

1 Comment

That is a good idea, I might just do that. I'll see how that looks.
0

You should not create such a big class--it's a really bad idea.

You need to start evaluating how you can extract portions of your functionality into other small classes. The other classes need not be exposed to users, they can be package private if you like (this is what you get if you don't supply an access level)

You can easily have a single class that is a "Matrix", but that can just be a facade over a large number of other classes.

I'm not sure if it is the same kind of thing you are doing but I spent a while developing a matrix class with a replaceable number implementation that would work with special types like rational and complex types the matrix could manipulate.

Java is miserable at this kind of work (and I'm probably the strongest java proponent you'll run into, so that's saying an awful lot!) The main problem is that number types don't have a useful base type, but to do this kind of work it really helps to be able to overload operators (Something I generally don't consider to be anything more than an attractive nuisance)

If this is the kind of thing you are working on, I highly recommend looking into groovy for the topmost level (using the classes). If that's not the kind of thing you're working on then sorry about the rant but you probably stopped reading before here anyway :)

1 Comment

Yeah I agree. It's not really that big of a class, I just like simplification. It was originally in python, but I'm working on improving my java so I'm putting some of my old programs into java. Lack of operator overloading is one thing I did notice and was surprised java did not have, although that really just means it won't look as pretty as it might in a language that allows that. My matrix doesn't do complex or irrational, just int, float, double, and long. I've already mastered the logic, it's just structure and syntax. Thanks for the advice! (btw, I read everything people say to me :) )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.