You can use either, or a non-control flow structure approach. It depends on your solution. For example, what are you going to do when the command comes, will commands ever be added, etc. What I am proposing below may be unnecessary -- just over-engineering.
This is something off the top of my head, but it'll give you an idea. You don't have to use an ICommand (it can be anything including an Action<t> or Func<t>).
static class CommandRepository {
Dictionary<string, ICommand> commands;
static CommandRepository() {
commands = new Dictionary<string, ICommand>(5);
commands.Add("0x00010001", new ...);
commands.Add("0x00020002", new ...);
...
}
public static ICommand GetCommand(string cmdStr) {
if (commands.ContainsKey(cmdStr))
return commands[cmdStr];
else
return null;
}
}
You would then use this class in your code.
void ExecuteCommand(string cmdStr) {
var cmd = CommandRepository.GetCommand(cmdStr);
if (cmd != null)
cmd.Execute();
else
throw new Exception();
}
You can take this further if you plan on adding commands a lot or perhaps dynamically. By using an IoC container like Unity. You can do your own research on that, but you could through a configuration file register multiple commands with different registration names and use that to resolve the objects instead of using a Dictionary.