I'm working on a little text based console game for a bit of fun and am starting work on the user commands entered into the console.
Are there some good design patterns for deciding what to do with the commands the user enters into the console window?
My thoughts so far:
I've createed a basic query structure for obtaining the command and it's arguments. It works kinda as follows (the code is C#):
var userCommand = _getUserInputQuery.Invoke(new GetUserInputRequest
{
FullCommandText = Console.ReadLine()
});
I now have two items available to me in the userCommand. Firstly I have the name of the command as a string userCommand.UserCommand
and secondly I have a string array of arguments userCommand.UserCommandArgs
.
I figure there's a couple of ways I can now proceed with the information I have.
The most basic form could be a simple switch/if
on the command name and invoke the relevant class/method required (seems a bit icky though).
I also figured given a generic interface for all commands IUserInputCommand
I could create the class required using reflection and fire off a generic Invoke
method from the interface. (Although I'm not sure how useful this will be given that some command invokations will require different data than others).
What are some design patterns already available for solving this kind of problem? What would you do personally?