开发者

Specify what kind of object will be created at runtime of a program

开发者 https://www.devze.com 2023-04-09 12:00 出处:网络
I have a set of java subclasses which all extend the same superclass. They contain exactly the same methods and make the same computations on input data of different type. That means that one is respo

I have a set of java subclasses which all extend the same superclass. They contain exactly the same methods and make the same computations on input data of different type. That means that one is responsible to deal with int, another with double, another with int Array, List etc. Objects of each type should be created at runtime of the program according to the input data each time.

What is the best way to specify what kind of object (of the referred subcl开发者_高级运维asses) will be created each time while the program is running? It seems not possible to use something like type checking since the data could be of different type (class).

Do I need a separate class that is responsible exclusively for this task, something that serves as an object generator? If so, is there a hint available about what would be the form and functionality of such a class?

Thank you.


I'm not sure I understood your question correctly as it's not very clear, some examples of your class and of what you trying to accomplish would help.

Anyway, what I got from it is that you should check the Visitor pattern. http://en.wikipedia.org/wiki/Visitor_pattern

In simpler words, I would separate the actions you want to make with each type, and have one class for each one of these (instead of having all these as methods in all your subclasses).

And you would do something like that:

yourSubclass.accept(yourAction)

A visit method would be defined on the Visitor interface, once for each type of Subclass you have. And each of your action class would have to implement what to do for each one of these. And the accept method, in your superclass, would just call visit from the action.

If this is not what you asked, sorry :)


Here's an example with "sum". The sum of a single number is just that number. The sum of a collection of them is the sum of all numbers in that collection.

interface Summable {
    double sum();
}

class IntegerSum implements Summable {
    int i;
    IntegerSum(int i) { this.i = i; }
    public double sum() { return (double)i; }
}

class DoubleSum implements Summable {
    double d;
    DoubleSum(double d) { this.d = d; }
    public double sum() { return d; }
}

class ListSum<? extends Number> implements Summable {
    List<? extends Number> list;
    ListSum(List<? extends Number> list) { this.list = list; }
    public double sum() {
        double d = 0;
        for(Number n : list) d += n;
        return d;
    }
}

Now you just need a FACTORY method:

class SummableFactory {

    public static Summable summableFor(double d) { return new DoubleSum(d); }
    public static Summable summableFor(int i) { return new IntegerSum(i); }
    public static Summable summableFor(List<? extends Number> list) { return new ListSum(list); }

    private SummableFactory() { } 
}


maybe a factory like:

abstract class Super {
    abstract void foo();
}
class I extends Super {
    void foo() {
        System.out.println(this);
    }
}
class D extends Super {
    void foo() {
        System.out.println(this);
    }
}
class Factory {
    static Super create(Class clazz) {
        if(clazz.equals(Integer.class))
            return new I();
        else if(clazz.equals(Double.class))
            return new D();
        else return null;
    }
}
public class Main {
    public static void main(String[] args) {
        Factory.create(Integer.class).foo();
        Factory.create(Double.class).foo();
    }
}
0

精彩评论

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

关注公众号