Reputation: 330992
I am writing an application that sends command line commands to a 3rd party application.
So I have a list of commands like:
"process images"
"apply effect blur"
"save as png"
...
I don't know how to best organize them in OOP style.
Right now I have:
switch ( e.KeyCode )
{
case Keys.A:
SendCommand ( "process images" );
break;
case Keys.B:
SendCommand ( "apply effect blur" );
break;
case Keys.C:
SendCommand ( "save as png" );
break;
...
}
I want to be able to do this in a clean way. Maybe like:
SendCommand (Commands.ApplyBlur)
, etc.
But I don't know which way is the best. I thought of storing these in a collection but thought it wouldn't be clear or reasonable.
Upvotes: 2
Views: 210
Reputation: 2067
It may be beneficial to put you command names into an enumerated type so other programmers who use your command class don't have to know the exact text command to send, they can just select it from a set using auto-complete in Visual Studio
public enum CommandType
{
ApplyBlur,
ProcessImage,
Save
}
Upvotes: 0
Reputation: 59645
I suggest to use a dictionary to map between the input and the command. The simplest solution is to map directly between keys and the command text.
Dictionary<ConsoleKey, String> map = new Dictionary<ConsoleKey, String>()
{
{ ConsoleKey.A, "process image" },
{ ConsoleKey.B, "apply blur effect" },
{ ConsoleKey.C, "save as png" }
};
ConsoleKey key = Console.ReadKey().Key;
String command;
if (map.TryGetValue(key, out command))
{
SendCommand(command);
}
else
{
HandleInvalidInput();
}
Depending on your actual needs it might be a cleaner solution to perform a two step mapping - from input key to an enum value, and from the enum value to the command text. You should also think about creating a command class and providing static instances for your commands.
public class Command
{
public Command(String commandText)
{
this.CommandText = commandText;
}
public String CommandText { get; private set; }
public void Send()
{
// Dummy implementation.
Console.WriteLine(this.CommandText);
}
// Static command instances.
public static readonly Command ProcessImage = new Command("process image");
public static readonly Command BlurImage = new Command("apply blur effect");
public static readonly Command SaveImagePng = new Command("save as png");
}
With this class the code to send the commands would be something like the following.
Dictionary<ConsoleKey, Command> map = new Dictionary<ConsoleKey, Command>()
{
{ ConsoleKey.A, Command.ProcessImage },
{ ConsoleKey.B, Command.BlurImage},
{ ConsoleKey.C, Command.SaveImagePng }
};
ConsoleKey key = Console.ReadKey().Key;
Command command;
if (map.TryGetValue(key, out command))
{
command.Send();
}
else
{
HandleInvalidInput();
}
Upvotes: 3
Reputation: 18815
So here are my assumptions, the 3rd party app has a finite and static set of command switches, you won't be messing with the set of switches, just suppliying them to the app.
Seems to me that if those assumptions are correct, a command pattern is overkill, I would be just inclined to create a static Command(s) class that simple exposes the strings as a static property or constant and contains the static send command method.
so you could simply write as you suggested...
Command.SendCommand(Command.ProcessImages);
Upvotes: 2
Reputation: 3577
You could use the Command pattern to allow you to put each command in its own class. This will also allow you to make the commands accessible from multiple places in your program without having to rewrite or copy and paste the code each time you want to do something.
Upvotes: 1
Reputation: 1354
I'm just starting to understand Design Patterns, but this seems to be perfect for the Command Pattern.
Upvotes: 2