Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You want to use a bunch of factory objects, stored in a <code>Map</code> under the string keys that you use in the <code>switch</code>.</p> <p><strong>These are the classes for the various fish you should already have.</strong></p> <pre><code>abstract class FishBase {} class Keegan extends FishBase { Keegan(Object _this, int x, int y) { // ... } } class GoldenBarb extends FishBase { GoldenBarb(Object _this, int x, int y) { // ... } } </code></pre> <p><strong>An interface for all the fish factories.</strong> A fish factory represents a way to create some type of fish. You didn't mention what the constructor signature is so I just picked some types.</p> <pre><code>interface IFishFactory { FishBase newFish(Object _this, int x, int y); } </code></pre> <p><strong>Set up one factory for every fish type.</strong> These obviously don't need to be anonymous classes, I'm using them to cut down on clutter.</p> <pre><code>Map&lt;String, IFishFactory&gt; fishFactories = new HashMap&lt;&gt;(); fishFactories.put("Keegan", new IFishFactory() { public FishBase newFish(Object _this, int x, int y) { return new Keegan(_this, x, y); } }); fishFactories.put("GoldenBarb", new IFishFactory() { public FishBase newFish(Object _this, int x, int y) { return new GoldenBarb(_this, x, y); } }); </code></pre> <p><strong>Then just pick the factory from the <code>Map</code> using the string you already have.</strong> You might want to check whether a factory for the given name exists.</p> <pre><code>stock.add(fishFactories.get(s).newFish(this, x, y)); </code></pre> <hr> <p>Now, if all your fish classes have <strong>the exact same</strong> constructor signature, you can create a single factory class that can handle all of them using reflection, and get rid of some boilerplate.</p> <pre><code>class ReflectionFishFactory implements IFishFactory { Constructor&lt;? extends FishBase&gt; fishCtor; public ReflectionFishFactory(Class&lt;? extends FishBase&gt; fishClass) throws NoSuchMethodException { // Find the constructor with the parameters (Object, int, int) fishCtor = fishClass.getConstructor(Object.class, Integer.TYPE, Integer.TYPE); } @Override public FishBase newFish(Object _this, int x, int y) { try { return fishCtor.newInstance(_this, x, y); } catch (InstantiationException | InvocationTargetException | IllegalAccessException e) { // this is terrible error handling throw new RuntimeException(e); } } } </code></pre> <p><strong>Then register it for every applicable subclass.</strong></p> <pre><code>for (Class&lt;? extends FishBase&gt; fishClass : Arrays.asList(Keegan.class,GoldenBarb.class)) { fishFactories.put(fishClass.getSimpleName(), new ReflectionFishFactory(fishClass)); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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