3

I have written a Java program that reads in a file containing commands to execute(in a language I made up myself). The commands are read in as strings and put into an array. Now a "dispatcher"-method will loop through the array, interpreting the commands and calling the respective methods which will act upon them.

This of course leads to a big block of nested if-statements:

if commandReadIn == this, do that... 
if commandReadIn is of type x, get next element,treat next element as argument... 
etc. 

Right now I only have a handful of commands, but what if I wanted to add hundreds? The code would become unmaintainable.

Now I'm wondering if it's possible to completely get rid of the conditional logic. The command pattern doesn't seem to be of much use here, since I would have to interpret the strings at some point anyway.. which means lots of nested "if"s. If it's not possible, what would be the best approach of restructuring the commands and their grammar in a way that will make it easy to add, edit or remove commands?

4 Answers 4

10

Use the Command Pattern for your commands. Your implementation can be greatly simplified.

1) Create a Command interface with an execute method.
2) Create an implementation for each command.
3) When you start your program, create a map of command string -> command implementation.
4) When you read in a string, look up the appropriate implementation and invoke it.
5) Optionally, your execute method can take a custom Context object as an argument, allowing you to pass your command arguments in a generalized way. It's up to the implementations to understand the context object and retrieve arguments off it.

With this approach, you will have no if statements, except possibly a check to see if you failed to retrieve anything from your cache of command implementations.

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

3 Comments

Thanks for your answer, I have implemented the Command Pattern now. For the sake of simplicity I'm passing an object containing state information instead of a Context to the execute method now (the state Information is needed by some commands with a more complex grammar and compound commands). Would it be better to use the Context class instead (I don't really know how to work with contexts)? Also I'm wondering whether I should really pass the state information or if it's better to have the concrete commands request the state object from the client
"object containing state information " is a context!! You are doing it correctly. I wouldn't change anything. Your commands are more testable this way.
oh, I see.. I thought you were referring to a specific class (or interface, rather). You're right about commands being easier to test like this, I'll leave it as it is. Thanks a lot for your help!
2

Create a static map that will map the action name to its command executor class name. Create a factory class that would return the command executor instance given the action.

Comments

1

Check interpreter pattern also if your language contains not only sequence of keywords, but some arguments/operators etc.

Comments

1

How about using the Interpreter pattern? You would then need to code a parser that changes your commands into a parse tree. Visitors can be used to traverse the parse tree for such tasks as syntax checking and executing the commands.

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.