开发者

How to turn methods in another class as a method in a generic class

开发者 https://www.devze.com 2023-02-12 11:02 出处:网络
I\'m trying to create an Object called a \'measurement\'. This measurement object has only one method, calculate(). (Could be an interface or class - not sure which is best).

I'm trying to create an Object called a 'measurement'. This measurement object has only one method, calculate(). (Could be an interface or class - not sure which is best).

I have many different measurements (hundreds, possibly more), and the way I'm trying to implement them is to have a class that contains all these measurements as static methods.

So I'm essentially trying to load a static method from a class into a Measurement object, where I can call measurement.calculate().

The way I'm starting to implement it, and the only way I can think of, is to use the Method class in Reflection, where each Measurement is开发者_C百科 instantiated with a Method (the static measurement method in a different class). The Measurement class' calculate() method would then invoke the method the respective object was instantiated with.

What's the best way to implement this?

Thank you!


This seems like you have decided on the way you want to do the task and have created more complicated issues along the way. You started out fine by suggesting that you would create an interface that all of your objects would implement. But grouping them all in one static class is where things start to fall apart.

A better organization would be to let each measurement class implement its calculate method. This way, when you are dealing with Foos, you don't have to go to one big class that has code that relates to Foos, Bars, Bizzes and Bazzes. You said you could have at least 100 classes, this will make your static class hard to work with in the first place. It is likely that you may not need that many implementations of calculate. You will be able to achieve this by building up a inheritance hierarchy which will further sub-divide and organize your code.

public interface Measurable<T> {
  T calculate();
}

public class Foo implements Measurable<Integer> {
  private int a;
  private int b;
  @Override
  Integer calculate() {
    return a + b;
  }
}

public class Foo implements Measurable<Double> {
  private double a;
  private double b;
  @Override
  Integer calculate() {
    return a / b;
  }
}

You have more information about what you are actually looking for, but that is a basic template that should provide a good example and show how you can make things flexible.


As you stated your problem, you should implement the "measurement object" as an interface. It has only one method and this method should be implemented by all the single specific "measurement-objects". Your approach looks more like a God class augmented with introspective code – just to imitate the behavior which is achieved more easily by using polymorphism.

So to be a bit more concrete:

public interface Measurable {
    public void calculate();
}

And the single measurement strategies would then be implemented as follows:

public class MeasurementA implements Measurable {

    @Override
    public void calculate() {
        System.out.println("I am doing a very cool measurement.");
    }

}

This could then be used in the following manner:

public class Controller {

    public static void main(String[] args) {

        ArrayList<Measurable> measurables = new ArrayList<Measurable>();
        measurables.add(new MeasurementA());
        measurables.add(new MeasurementB());

        for (Measurable measurement : measurables) {
            measurement.calculate();
        }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号