Note that there are some explanatory texts on larger screens.

plurals
  1. POFactory pattern generic interface with type that extends an abstract class
    text
    copied!<p>Okay, I hope I don't try to achieve the impossible here.</p> <p>I have an abstract class <code>abstract class MathFunc</code> that is used to implement the mathematical functions Rastrigin, Griewangk and Rosenbrock in their specific classes <code>final class Rastrigin extends MathFunc</code>, <code>final class Griewangk extends MathFunc</code> and <code>final class Rosenbrock extends MatjFunc</code>.</p> <p>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. </p> <p>From my previous question <a href="https://stackoverflow.com/questions/20515026/how-to-instantiate-t-type-inside-a-template-class">here</a> 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: </p> <pre><code>interface MathFuncFactory &lt;T extends MathFunc()&gt;{ T createFunc(); } </code></pre> <p>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> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload