Generics is the way to go. But interpreting this in your question:
However, because I don't know what parameters such function would require, I defined it like this:
and your first comment to answer from Gregor Koukkoullis I think that your problem is that you just need to (and should) declare every method that takes different amount of parameters. There just is no other way around but it is anyway more clear this way.
So you should have something like this:
public interface WeightFunction<T> {
double calculate(T... arguments);
double calculate(Long seed, T... arguments);
double calculate(Long seed, Integer somethingElse, T... arguments);
}
Why varags parameters have to be the last? See this. The accepted answer propably is not the clearest one but few others will clarify the problem.
Now, when you implemented your calculate
in your example somehow you knew what are the parameters?
@Override
public double calculate(Object ... arguments) {
// you know here that the 2 first are pixels, dont you?
return calculate((Pixel)arguments[0], (Pixel)arguments[1]);
}
So with the same knowledge you could just create a declaration of needed atributes in your interface. Maybe even:
double calculate(Long seed, T t1, T t2);
if there is more likely only two T
s.
And the answer to your question:
Is this considered good form?
IMO it is never a good habit to make functions that take an array of Object
s and then you implement a method that interprets the params as it wants and does what it wants. I think it is strongly against the whole idea of interface.
It is always a better idea to declare methods that "tell" what they are doing and then just add a new method declaration or refactor your interface and already implemented methods when there is a need for that.
If you choose to pass "a list of objects" anytime you need some flexibility, you are soon a knee-deep in it.
Now this may raise a question that do I have to implement all the methods n this interface? Yes, but if you do not want to you can either define separate interfaces and make your class implement 1 or more of them or/and use extends
to make interface hierarchy.