Design of a simple Command Interpreter

Hi,

I have a scenario where a client pgm passes a command string to a Command Interpreter. There are several possible commands. The Command Interpreter accepts these command strings and makes API invocations on some other object.

Some Commands would need to return a value, whereas some others do not need to. These results need to be passed back to the client.

What should the Command Interpreter interface look like ?

For isntance, one command could be somethinfg like <getAllCustomers/>.Another command could be like <deleteCustomer name="foo"/>. The former would return a Document of all Customers. The second one does not return a value,but could throw an Exception.

I was thinking of a CommandInterface with a single method named processCommands(String commandXML(. But this would not work if I need to have different types od return values. Moreover, these APIs are more or less unrelated to each other. i.e. different from a Strategy pattern where there is a specific heirarchy/relationship.

Any ideas are welcome.

rgds.

Menon.

[1104 byte] By [rammenon1999a] at [2007-10-1 20:46:36]
# 1

Making it more clear.....

My questions are:

1) What should the Command Interpreter interface look like ?

2) What is the best way to dispatch the XML requests to different API

invocations on a particulat object. For instance, <getAllCustomers/>

should invoke CustomerManager.getAllCustomers(). <deleteCustomer

name="foo" should invoke CustomerManager.deleteCustomer("foo"); Note

that there is more than one class on which the APIs could be invoked.

For instance, there could be an AccountsManager class on whom Account

related queries would be dispatched to.>

rammenon1999a at 2007-7-13 2:44:54 > top of Java-index,Other Topics,Patterns & OO Design...
# 2

Hi,

The command interface needs to be rather generic in this way since you don't have so much in common for the commands.

public inteface Command {

/**

* Gets the name of the command. The name should be unique and is

* used during registration of commands in the interpreter.

*/

String getName();

Object execute(String request) throws Exception;

}

You then creates implementations of the command interface, and registers the implementations in the interpreter.

/Kaj

kajbja at 2007-7-13 2:44:54 > top of Java-index,Other Topics,Patterns & OO Design...
# 3

> 1) What should the Command Interpreter interface look like ?

I would use the Command Pattern.

http://www.javaworld.com/javaworld/javatips/jw-javatip68-p2.html

You can combine this with the memento pattern.

Here is the interface that i use when implementing the command pattern.

/**

* An Interface for a Command Object

* see Command Pattern [GOF:233]

* @author martin.spamer

* @version 0.1 - 14:08:58

**/

public interface CommandInterface

{

/**

* An Interface for a Command Object

* see Command Pattern [GOF:233]

* @param java.util.Properties

* @return java.util.Properties

*/

public java.util.Properties execute( java.util.Properties properties)

throws CommandException ;

}

> 2) What is the best way to dispatch the XML requests to different API

> invocations on a particulat object. For instance,

I use a CommandFactory which I drive with an xml config file.

and drive it something like:

<actions command="CommandString" classname="uk.co...ClassName"/>

MartinS.a at 2007-7-13 2:44:54 > top of Java-index,Other Topics,Patterns & OO Design...
# 4
Thx Martin.
rammenon1999a at 2007-7-13 2:44:54 > top of Java-index,Other Topics,Patterns & OO Design...