I need implement a control solution for a computer game I am writing to take user input .
I am looking at the Command pattern, but not sure if its a good fit for this.
I need implement a control solution for a computer game I am writing to take user input .
I am looking at the Command pattern, but not sure if its a good fit for this.
Without knowing more about your input, it might be worthwhile to look at the Interpreter pattern to read and parse the inputs, coupled with the Command pattern to actually execute the commands.
The technique is usually called "double dispatch", but the interpreter pattern describes the general pattern.
In the command pattern, it's best to make commands immutable, which means no runtime input. Don't over complicate the problem. All you need to do is accept input real time and interpret it somehow (see Thomas' tip if you're set on a pattern). If you're trying to eliminate a big switch statement, or large if-else block, the abstract factory pattern might be worthwhile.
IMO, the Command pattern is overused for situations like this. If the command is entirely transient - you are not going to store or manipulate the object - then there is little point in the added complexity, object code overhead and boilerplate of using commands.
A more appropriate approach is a simple switch and method call. Perhaps you might want to separate out the interface you are calling from an implementation, although this can trivially be done later (if you like tests or traces, you'll probably do this sooner than later). If the keys can be changed, a simple map from input value to action value should be straightforward.