title: Reflections bad or good practice for commands
body: <p>Well, I saw there were some questions and answers about this, but they didn't really satisfy me.</p>
  
  <p>Let's say for example, I have programmed a console. It's a nice JFrame with an output and an input txtField/Area. But this console should not only be used for output, but also to run commands.</p>
  
  <p>Because I would need this console very often and I don't want to change the code of the console I programmed it this way:</p>
  
  <p>The console has one method to register commands.</p>
  
  <pre><code>console.registerCommand(String command, String methodToInvoke, Object invokeObject);
  </code></pre>
  
  <p>With this method I'm able to use this console everywhere without the need of changing or inharitance.</p>
  
  <p>Whenever the <code>String command</code> is written the console knows it's a registered keyword and executes the method via reflection. </p>
  
  <p>Would this be a good or bad practice? On code styling and in performance! And what could I do better?</p>
  
  <p>I also found it quite neat to use reflections this way to add ActionListeners to MenuItems in a TrayIcon. </p>
  
  <p><strong>Edit</strong></p>
  
  <p>To the Answer below:</p>
  
  <p>Ok with commands i would accept this is a way to do. But in the Tray example I wrote a TrayHelper Class which creates the TrayIcon. There I want to add MenuItems and their ActionListeners but without creating every Object myself and add them to the Tray. So I wrote Methods like this: </p>
  
  <pre><code>public void addMenuItem(String label, String methodToInvoke, String invokeObject);
  </code></pre>
  
  <p>This method not only executes the method when the MenuItem is clicked, but also creates the MenuItem first, adds an ActionListener to it which invokes the Method, and adds it to the TrayIcon.</p>
  
  <p>So in order to use this TrayHelper I can now just write:</p>
  
  <pre><code>th.addMenuItem("Exit","exitMethod",this);//executes the exitMethod of
                                           //this class after Menuitem Exit
                                           //was clicked
  </code></pre>
  
  <p>I don't really see how i could do this without reflection other than to write all the Objects myself again and adding them to the Tray. Or I'm blind :)</p>
  
  <p><strong>Edit 2</strong></p>
  
  <p>Ok, I was blind. I just didn't realize how to do this without reflection, but it is so simple. </p>
  
  <p>Especially with the Command pattern.</p>
  
  <p>Because of the anonymous classes I could do it that way, and I really like the way to write code this way (I always did it with ActionListeners)</p>
  
  <pre><code>th.addMenuItem("Test",new Command(){
         public void execute(){
              //do stuff
         }
  });
  </code></pre>
  
  <p>Thank you :)</p>