File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes OO, Patterns, UML and Refactoring and the fly likes UNDO operation is mandatory  in Command Pattern? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "UNDO operation is mandatory  in Command Pattern?" Watch "UNDO operation is mandatory  in Command Pattern?" New topic
Author

UNDO operation is mandatory in Command Pattern?

Malli Raman
Ranch Hand

Joined: Nov 07, 2001
Posts: 312
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

Joined: May 14, 2003
Posts: 762
The ability to undo operations (commands) demonstrates one of the benefits of using the command pattern. It is not required.

-j-


Books: Agile Java, Modern C++ Programming with TDD, Essential Java Style, Agile in a Flash. Contributor, Clean Code.
David Hibbs
Ranch Hand

Joined: Dec 19, 2002
Posts: 374
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.


"Write beautiful code; then profile that beautiful code and make little bits of it uglier but faster." --The JavaPerformanceTuning.com team, Newsletter 039.
Malli Raman
Ranch Hand

Joined: Nov 07, 2001
Posts: 312
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

Joined: Dec 19, 2002
Posts: 374
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

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: UNDO operation is mandatory in Command Pattern?
 
Similar Threads
Head First Design Pattern -Command Pattern
Require Explanation
What is a factory ?
design pattern
Chain of Responsibility Pattern