Reputation: 2939
The story goes like this:
I have an abstract class called Algorithms and a lot of classes that extend it. Some of them have parameters to tune. Some have none, and some have up to 5.
I would like to have a method in Algorithms that can tune an arbitrary parameter. e.g.:
public static void tune (String paramName, double minValue, double MaxValue)
{ ... }
So that I can call it like this on 'class SoftRankBoots extends Algorithm':
Algorithm srb = new SoftRankBoost();
srb.tune("delta", 0, 1);
Note that SoftRankBoost has an instance variable 'double delta';
How can I achieve this?
Thank you.
Upvotes: 0
Views: 392
Reputation: 839
I think the sub-classes of 'Algorithm' class are differ from each other, so the Template Method pattern or the Strategy pattern may give you some useful concept to solve your problem.
Upvotes: 0
Reputation: 188034
Take a look at the Reflections API:
Last link contains example source code.
Note: If you need reflection in Java, then your design may be not as clean, as it could be.
Upvotes: 2
Reputation: 14212
Instead of implementing the method tune in the Abstract class leave it an abstract method, that implementing classes must implement.
So
public abstract void tune (String paramName, double minValue, double MaxValue);
With out seeing the whole picture of what you are trying to do it is hard to give you better advice. But what you are suggesting just seems like a bad idea in general.
Upvotes: 3