Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
The more Java-like method, and the way that you'd do things the object-oriented way, would be to use polymorphism, with a Command interface and one implementation per command. You could use a Map data structure to relate the commands to the Command objects. If you're interested, I could certainly tell you more about it. In any case, for a handful of commands, the if-else is going to work just fine.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
This is the basic idea. You probably will want your execute() method to take some arguments -- that's up to you.
In particular, I think this is called the Command Pattern.
The message about unsafe operations leads me to believe you're using the new Java 5, which has generic types. In Java 5 (which I haven't used extensively myself, yet) you can say...
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |