0

I'd like to create new classes dynamically, using Java. I have 3 buttons with labels: 1, 2 and 3. Code is like:

switch (button.getActionCommand()) { case 1: return new Listener1(); break; case 2: return new Listener2(); break; case 3; return new Listener3(); break; } 

And it works but I'd like to make it shorter. Every new class will be different from the previous with last number, only. So is it possible to create classes dynamically like:

return new Listener()+button.getActionListener(); 

I'm sure its possible, but how? Should I use one of Proxy classes or is there an easier way to achieve this?

9
  • What programming language are you using? Commented Jan 15, 2013 at 15:27
  • I guess this is java? You should add the language as a tag. Commented Jan 15, 2013 at 15:27
  • Obligatory: sourcemaking.com/refactoring/… (among many others, that was just the first Google result) Commented Jan 15, 2013 at 15:29
  • Ayayay I forgot sorry. Yes its Java Commented Jan 15, 2013 at 15:29
  • Not sure if you can avoid the switch statement in this case. But I'd definitely hide the switch in a ListenerFactory. Commented Jan 15, 2013 at 15:29

4 Answers 4

2

You didn't specify any language, so here is one way:

const_get("Listener#{button.action_command}").new 

However, I don't see why you would need to create classes dynamically for this. From what I understand, the ListenerN classes already exist and you just want to dynamically instantiate them?

If you must create classes dynamically, that's not a problem, either:

const_set("Listener#{button.action_command}", Class.new) 
Sign up to request clarification or add additional context in comments.

1 Comment

I have abstract class where all methods are. Then I make sub-classes which differ little. When program is running user can make various actions and various types of Listener class are needed. So this SWITCH statement's work is to return correct class (switch statemnt is in class btw)
2

If it's Java, you can do something like this:

String className = "package.Listener" + button.getActionCommand(); Class theClass = Class.forName(className); Object theListener = the.newInstance(); return (Listener) theListener; 

Comments

2

Provided this is Java, you could use reflection if you really wanted that...

return Class.forName("packageName.Listener"+theNumber).newInstance(); 

Of course, doing so might have adverse effects too - performance hit, ugly code, debugging and readability issues, having to deal with handling multiple kinds of exceptions...

Recommended reading:

Comments

0

Even when you use Class.forName(...) somewhere in your classpath this Listener1, Listener2, Listener3 classes must exist so this will not work for a random number but only for the numbers wherefor such a corresponding Class is defined. I assume you expect all these different classes respect some "contract" (=interface) so you can define an interface which must be implemented by these concrete classes:

public interface Listener() { public void someExpectedMethod(); } public class Listener1 implements Listener { public void someExpectedMethod() { // some implementation logic } } 

So what you can do is create one a static map of these classes:

static Map<Integer,Class<? extends Listener>> listenerMap=new HashMap<>(); listenerMap.put(1,Listener1.class); listenerMap.put(2,Listener2.class); 

In your button you can:

Listener inst=listenerMap.get(button.getActionCommand()).newInstance(); inst.someExpectedMethod(); 

It's a little more code but you have:

  • type checking
  • your IDE will find occurrences where Listener1 is used.

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.