0

I have a TcpListener Listening on a NetworkStream to perform a command examples. "Copy, Move, Delete, ...."

switch(command)
{
   case "copy":
   // do copy
   break;
   case "delete":
   // do delete
   break;
   case "move":
   // do move
   break;
   .......................................
}

i have implemented it using switch - case and if statement but when it comes to maintenance or adding a new command, specially when the commands list goes beyond 100 commands it becomes very hard and tedious, so is there a way to do this efficiently, i have tried searching on Google but i can't seem to get the jargon of it correctly
Any help :) Yaser

3
  • 2
    Have you considered using a dictionary<string, delegate>? Commented May 7, 2015 at 15:06
  • @ZoharPeled can you please Elaborate more or give me an example Commented May 7, 2015 at 15:07
  • @GrantWinney i have created separated methods but i want a more efficient way, a more professional way than if else Commented May 7, 2015 at 15:09

3 Answers 3

1

You're looking for Command Pattern. You'll have to create an interface ICommand and implementation of that interface one per command.

For example: CopyCommand, DeleteCommand, MoveCommand etc.

Then you need a factory method which creates instance of respective ICommand based on the string provided.

Finally you'll call ICommand.Execute. That makes it readable, maintainable, clean and so on..

Your code will become

ICommand command = commandFactory.Create(commandString);
command.Execute();

Where commandFactory is an instance of "Factory" which will create instance of appropriate ICommand.

Your Factory class could be implemented using a huge switch case or Dictionary<string, Func<ICommand>> if you prefer.

Sign up to request clarification or add additional context in comments.

Comments

1

As @Zohar Peled has already mentioned Dictionary is a really cheap solution for this problem, not so elegant, but really cheap:

        //Initialize command lists
        Dictionary<string, Action> commands = new Dictionary<string, Action>();

        commands.Add("move", DoMyMove);
        commands.Add("add", ()=> Console.WriteLine(""));
        commands.Add("remove", DoMyRemove);
        commands.Add("close", DoMyClose);

delegate

        private void DoMyMove()
        {
             // TODO
        }

Usage:

        commands[command].Invoke();

It's extendable easly and clear for basic developers...

2 Comments

I'm giving you -1 for stealing my idea :-) , but +1 for writing the code example and another +1 for giving credit where credit is due. :-)
You r gentle and sry... :(
1

One option is to define a Dictionary<string,action> on the class level that will hold the name of the command and an action delegate to the actual method that needs to be performed. Then in your main method you don't need to use a switch, simply get the action delegate from the dictionary using the dictionary's TryGetValue Method and if it returns true simply invoke the action delegate.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.