Question

Okay, I hope I don't try to achieve the impossible here.

I have an abstract class abstract class MathFunc that is used to implement the mathematical functions Rastrigin, Griewangk and Rosenbrock in their specific classes final class Rastrigin extends MathFunc, final class Griewangk extends MathFunc and final class Rosenbrock extends MatjFunc.

I need a Generation class that contains an ArrayList that have to contain several objects of types Rastrigin/Griewangk/Rosenbrock. These objects should be instantiated separately from each other because Rastrigin/Griewangk/Rosenbrock generate in their constructor random numbers that are supposed to remain random.

From my previous question here I managed to implement a version that starts out fully initialized but has the same Rastrigin (for example) object repeated through the ArrayList. From the answer given there I understand that I should use the Factory pattern to instantiate every object separately. From the answer I have this chunk of code:

interface MathFuncFactory <T extends MathFunc()>{
       T createFunc();
}

But it doesn't work. How can I implement this design properly and make it work the way I wish? Also would it be possible to instantiate an object of type T inside my Generation class ? I'm a newbie in java so detailed explanations are very appreciated.

p.s. I made this second answer because it is not very related to the initial question which was the usage of generic classes in java.

Was it helpful?

Solution

You can't instantiate type parameters like T directly. Instead you want Generation to accept a factory, so that you can create a new instance in each iteration of the loop:

interface MathFuncFactory<T extends MathFunc> {
    T create();
}

class Generation<T extends MathFunc> {

    Generation(MathFuncFactory<T> factory) {
        for (...) {
            list.add(factory.create());
        }
    }
}

Then you could create a Generation for a particular function as follows:

new Generation(new MathFuncFactory<Rastrigin>() {
    public Rastrigin create() {
        return new Rastrigin();
    }
});

It might be beneficial to have a class that will make these factories:

class MathFuncs {

    private MathFuncs() {}

    static MathFuncFactory<Rastrigin> rastrigin() {
        return new MathFuncFactory<Rastrigin>() {
            public Rastrigin create() {
                return new Rastrigin();
            }
        }
    }

    static Generation<Rastrigin> rastriginGeneration() {
        return new Generation<Rastrigin>(rastrigin());
    }
}

so you can simply call

new Generation(MathFuncs.rastrigin())

or just

MathFuncs.rastriginGeneration()

OTHER TIPS

You have two mistakes in your code:

1) You don't need () after extends MathFunc

2) abstract methods should be also marked with the abstract keyword

abstract class MathFuncFactory <T extends MathFunc> {
    abstract T createFunc();
}

Relating to the answer you've linked, you don't need the abstract keyword for interfaces:

interface MathFuncFuncFactory<T extends MathFunc> {
    T createFunc();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top