Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialize a new object from class in Enum

Tags:

java

enums

I have an Enum called Plugins:

public enum Plugins {

    ROTATING_LINE (plugin.rotatingline.RotatingLine.class),
    SNOW_SYSTEM (plugin.snow.SnowSystem.class);

    private Class<?> c;

    private Plugins (Class<?> c) {
        this.c = c;
    }

    public Class<?> getClassObject() {
        return c;
    }

}

What I would like to do is to loop through all the enums in Plugins and create new objects from those using the variable c like this:

for (Plugins plugins : Plugins.values()) {
    Class<?> c = plugins.getClassObject();
    pluginList.add(new c(400, 400));
}

Is there a way of accomplishing this with a similar method? The reason why I want to do this is to create a list of classes that should be added to the List plugins when I start my application.

like image 838
jnsstnbrg Avatar asked Mar 22 '12 15:03

jnsstnbrg


2 Answers

Since you are iterating over the plugins, I guess all plugins' constructors have the same number & type of parameters. (I do not understand your plugins.add(...) though.)

Anyways, if you want to use Enums, I would do the following, using constant-specific method implementations instead of type tokens/reflection:

public enum PluginTypes {
   ROTATING_LINE { Plugin create(int x, int y){
         return new plugin.rotatingline.RotatingLine(x,y);} },
   SNOW_SYSTEM { Plugin create(int x, int y){
         return new plugin.snow.SnowSystem(x,y);} };

   abstract Plugin create(int x, int y);
}

public interface Plugin {
  //...
}

Then your loop would look something like this:

List<Plugin> plugins = new ArrayList<Plugin>();

for (PluginTypes pt : PluginTypes.values()) {
    plugins.add(pt.create(400, 400));
}

On the other hand, if you know your implementation classes of Plugin anyways, why not use them directly instead of via the PluginTypes Enum?

like image 123
DaveFar Avatar answered Sep 22 '22 02:09

DaveFar


Create an interface for the plugins

public interface Plugin{
     setShapeType();
     setX();
     setY();
     ...
  }

Create a method on the enum that creates an instance of Plugin.

public enum Plugins {

        ROTATING_LINE (plugin.rotatingline.RotatingLine.class),
        SNOW_SYSTEM (plugin.snow.SnowSystem.class);

        private Class<? extends Plugin> c;

        private Plugins (Class<? extends Plugin> c) {
            this.c = c;
        }

        // This acts as a constructor that takes args, I am assuming the args you need 
        public Class<? extends Plugin> createInstance(String shapeType,int x, int y) {
            Plugin plugin = c.newInstance();
            plugin.setShapeType(shapeType);
            plugin.setX(x);
            plugin.setY(y);
            return plugin;
        }

    }

Instantiate in loop

List<Plugin> myPlugins = new ArrayList<Plugin>();

for (Plugins plugins : Plugins.values()) {
    myPlugins.add(plugin.createInstance(Shaped.parent, 400, 400));
}

Please note this is Psuedo Code

like image 37
Kevin Bowersox Avatar answered Sep 21 '22 02:09

Kevin Bowersox