Timeline for Is there a more intelligent way to do this besides a long chain of if statements or switch?
Current License: CC BY-SA 3.0
6 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Jun 15, 2014 at 22:29 | comment | added | Harrison Nguyen | This is awesome and easy to understand. Thanks for the suggestion. It's pretty much exactly what I was looking for and seems to be manageable for future addition of commands. | |
Jun 13, 2014 at 19:33 | comment | added | Cruncher |
Rather than the loop, we could use this as a map if you make Command an abstract class that overrides equals and hashCode to be the same as the string that represents the command
|
|
Jun 13, 2014 at 9:15 | comment | added | LastFreeNickname | +1 for using a "light" Command pattern! It should be noted that when you're dealing with non-Strings you can make use of e.g. intelligent enums that know how to execute their logic. This even saves you the for-loop. | |
Jun 13, 2014 at 9:10 | comment | added | Neil | This pattern tends to fit the console command paradygm nicely, but only because it neatly separates the command logic from the event bus and because new commands are likely to be added in the future. I think it should be noted that this isn't a solution for any circumstance where you have a long chain of if...elseif | |
Jun 13, 2014 at 7:40 | comment | added | jhr |
If messages never need to be parsed elsewhere (I assumed that in my answer) this is indeed preferable to my solution for Command is more self-contained if it knows when to be called itself. It does produce a slight overhead if the list of commands is huge but that's probably negligible.
|
|
Jun 13, 2014 at 7:29 | history | answered | mrjink | CC BY-SA 3.0 |