Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

UNDO operation is mandatory in Command Pattern?

 
Malli Raman
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Is the UNDO operation is a must one in Command Pattern or what?

Because in my application, I am calling the RequestHandler based on the RequestObject. The Request URL will be parsed and RequestHandler Object is created using Java Reflection API's. Then we are calling RequestHandler.execute (Request) method. But there is no undo operation is defined in my design. Why do we require undo operation in the Command design pattern?

There is no undo operation is given the example in the following site:
http://www.dofactory.com/Patterns/PatternCommand.aspx

Regards,
M.S.Raman
 
Jeff Langr
author
Ranch Hand
Posts: 799
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ability to undo operations (commands) demonstrates one of the benefits of using the command pattern. It is not required.

-j-
 
David Hibbs
Ranch Hand
Posts: 374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeff Langr:
The ability to undo operations (commands) demonstrates one of the benefits of using the command pattern. It is not required.


...and 'Undo' is itself a command anyway.
 
Malli Raman
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeff Langr:
The ability to undo operations (commands) demonstrates one of the benefits of using the command pattern. It is not required.


Hi Jeff & David,

Thanks for quick reply.

Just for a clarification whether following case will be considered as a command pattern implementation:

1. User enters http:\\ viewCustomer.do
2. After parsing the URL in a controller servlet, calling the viewCustomerRH.execute(HttpRequest req, HttpResponse res)
3. Similarly for deleteCustomer.do calls deleteCustomerRH.execute(HttpRequest req, HttpResponse res) in that controller servlet.
[that is based on user request, the url be parsed in the controller servlet and the corresponding requesthandler method is called, here I don't have undo operation(As it is meaningless in this case)].

Regards,
M.S.Raman
 
David Hibbs
Ranch Hand
Posts: 374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Malli Raman:


Hi Jeff & David,

Thanks for quick reply.

Just for a clarification whether following case will be considered as a command pattern implementation:

1. User enters http:\\ viewCustomer.do
2. After parsing the URL in a controller servlet, calling the viewCustomerRH.execute(HttpRequest req, HttpResponse res)
3. Similarly for deleteCustomer.do calls deleteCustomerRH.execute


I think you may be confused here between Command Pattern and the Chain of Command pattern. All the command pattern implies is that there is a single, specific command given which will be executed.

Consider the military. If your superior officer issues you a Command, you carry it out.

This question sounds more like a Chain of Command question.

I'm guessing--possibly incorrectly--that what you refer to as viewCustomerRH and deleteCustomerRH are members of your action. If so, then this is NOT a Command pattern and is instead an example of delegation.

If they are not members, and are perhaps actions themselves, then it could *possibly* be a chain of command, but not necessarily. It sounds more like a work-flow...
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you could make the case for command. Based on the URL the container looks up a Java object that implements an execute() interface and executes it. The object encapsulates the processing that should happen for the URL. The interface and processing fit command. BTW: Where's the mapping from the name.do to fully.qualified.servlet.classname?

With Struts and many other "front controller" frameworks you go one step further by sending all requests through one object that does the look up and execute. I did that in my scratch-built Wiki and felt the things I executed were definitely commands. I even called the interface something Command.

In the long run, whether it is command or 80% command or a mix of command and chain of command doesn't matter a whole lot. You won't be graded on pattern purity. But it would be nice if you could say "It's like command, except this little thingie" to a new team member and they get what you mean in a fraction of the time it would take to describe the whole mess.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic