• Post Reply Bookmark Topic Watch Topic
  • New Topic

what design pattern should work here?  RSS feed

 
Adrian Burlington
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

I wonder what design-pattern should work for this scenario. A user input a text in a textfield and the end result should be an execution of a command base on the user's input.
example, user input:

$empl9999in


the command is 'in'
the id is 9999
the subject is 'empl'

so an execution of the *in* command should go with the parameters of employee 9999

question, what is the best way to design this?
I realized I'll need a parser to understand what is the command and based on the user user's ending string (example 'in') to invoke the appropriate command.
Initially I thought of course on the command-design-pattern (so I can place the command also in a stack and invoke and undo cool!), but how would the 'invoker' work? (should I use a map of [command, command_class] ??

Thanks for any tips
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Object-oriented design patterns are for bigger problems and issues than what you describe.

you should describe why the user has to input the command the way you describe?

Why can't you have :

command argument1 argument2

Example:

in employee 9999

I'm moving this to the Java In General forum which is more appropriate for text manipulation questions such as this one.
 
Embla Tingeling
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adrian Burlington wrote:(should I use a map of [command, command_class] ??


Yes, the standard solution is to use the Command design pattern. For the association between command Strings and Command objects you use a map. Command would be an interface with an execute method.

Example,


 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An alternative to a Map is the Chain Of Responsibility pattern. Basically, you could ask each Command object whether it feels responsible for the given input, until you find one that does. This makes most sense when there isn't a simple one-to-one relationship (and therefore a Map wouldn't work). (As an example, this is basically how the JDBC DriverManager finds the appropriate driver for a given JDBC-URL.)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!