0

How can i represent an object type with a variable such as a string. For example I have DrawPanel1, DrawPanel2, DrawPanel3, all the way through DrawPanel12. and in a separate class I have a method to create each one with "DrawPanel1 panel = new DrawPanel1();" but I want to have a method.

public void TestPanel(int panelNum){} 

where it so it creates DrawPanel(panelNum), so if 2 is passed in it creates a new DrawPanel2.

I thought of doing this with [ String Panel = ("DrawingPanel"+panelNum); ] but when I used Panel rather than the name of the object it wouldn't work.

8
  • 2
    What is the functionality of DrawPanel1 such that it is divergent and completely different from DrawPanel12? Commented Jan 14, 2019 at 20:23
  • 6
    Your problem is more likely that you have 12 different classes, you should probably have 12 instances (objects) of one class. Commented Jan 14, 2019 at 20:25
  • its for a school project, so we need to have all of them be separate classes, but having one class to create any one of them is just for my sanity, and also I've wondered in the past if it is possible to somehow represent an object type with a string. Commented Jan 14, 2019 at 20:28
  • 1
    Please add some more code. For example, is there a DrawPanel interface, with DrawPanel1, DrawPanel2, ... implementations? If yes, see the Factory pattern. Commented Jan 14, 2019 at 20:30
  • If all you want to do is get a string representing a variable you can do DrawPanel1.getClass().getSimpleName(); Commented Jan 14, 2019 at 20:32

3 Answers 3

1

I think I understand what you are asking, and I'm going to provide an answer to your immediate question. What you are attempting sounds a little over-complicated, however. The following may need a bit of tweaking...

public void testPanel(Class<?> clazz) {
    Object instance = Class.forName(class.getName());
    ...
}

At this point, instance won't do you much good. You could also create an interface DrawPanelI with the methods used by your different DrawPanel's and have each of them implement that interface. Then, change Object instance to DrawPanelI instance. Now, you can invoke the common methods through instance.

Sign up to request clarification or add additional context in comments.

Comments

0

It is a very time consuming task to have variables DrawPanel1 - DrawPanel12. Instead, it would be much easier to have a list of them.

//Making the list
List<DrawPanel> drawPanels = new ArrayList<DrawPanel>();

//Adding to the list
drawPanels.add(new DrawPanel());

//Retrieving from the list
DrawPanel panel = drawPanels.get(0);//"DrawPanel1"

//Processing the list
for (DrawPanel panel: drawPanels){
   panel.doStuff();
}

Comments

0

Assuming there is a DrawPanel interface then it would be possible to use a factory pattern:

public class DrawPanelFactory() {
    public DrawPanel create(int whichTypeOfPanel) {
        if (whichTypeOfPanel == 1) {
            return new DrawPanel1();
        }
        ...
        if (whichTypeOfPanel == 12) {
            return new DrawPanel12();
        }

        throw new IllegalArgumentException("Unsupported panel type:" + whichTypeOfPanel);
   }
}

That ends up being a lot of if statements, but still easily testable. To avoid the if statements use a static Map<Integer, DrawPanelFactoryDelegate> to associate a specific integer value with a specific factory that knows how to create that specific type of DrawPanel:

public class DrawPanelFactory() {
    private static Map<Integer, DrawPanelFactoryDelegate> drawPanelFactories = new ...;
    static {
        drawPanelFactories.put(1, new DrawPanelFactory1());
        ...
        drawPanelFactories.put(12, new DrawPanelFactory12());
    }

    public DrawPanel create(int whichTypeOfPanel) {
        DrawPanelFactoryDelegate delegateFactory = drawPanelFactories.get(whichTypeOfPanel);
        if (delegateFactory != null) {
            return delegateFactory .create();
        }
        throw new IllegalArgumentException("Unsupported panel type:" + whichTypeOfPanel);
   }
}

interface DrawPanelFactoryDelegate {
    public DrawPanel create();
}

DrawPanel1Factory implements DrawPanelFactoryDelegate {
    public DrawPanel create() {
        return DrawPanel1();
    }
}

...

DrawPanel12Factory implements DrawPanelFactoryDelegate {
    public DrawPanel create() {
        return DrawPanel12();
    }
}

then in use:

DrawPanelFactory drawPanelFactory = new DrawPanelFactory();
DrawPanel aDrawPanel = drawPanelFactory.create(1);
...
DrawPanel yetAnotherDrawPanel = drawPanelFactory.create(12);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.