aspose file tools*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Head First Design Pattern -Command Pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Head First Design Pattern -Command Pattern" Watch "Head First Design Pattern -Command Pattern" New topic
Author

Head First Design Pattern -Command Pattern

Malli Raman
Ranch Hand

Joined: Nov 07, 2001
Posts: 312
Hi

I have few questions about the command pattern?

1) What is the difference between the command pattern and builder pattern.

2) Does the command pattern must support UNDO operation of previous command? Practically is it possible? and Is it mandatory for a command pattern?

In the Head First Design pattern, Customer ordering burger is given as the example for command pattern.

ie The customer will set the command(order burger) to waitress and cook will process the order based on the customer order.

In this case practically the undo operation is not possible (i.e once you ordered a burger the customer cannot cancel(undo) the order. So in this case how the UNDO will support in for the command pattern). In this case why can't we use the builder pattern?

Thanks & Regards,
M.S.Raman
Francis Shillitoe
Greenhorn

Joined: Aug 30, 2002
Posts: 22
The command pattern lets you encapsulate a request as an object. Classes that follow this pattern have names which are verbs like "Load" or "Save". It is not mandatory to include an undo() method. Only an execute() method is required. Depending on what your class does, it may be impossible to undo the command.

The builder pattern is unrelated. In the builder pattern the construction logic of an object is moved outside of the class to instantiate.


<a href="http://www.shillitoe.com" target="_blank" rel="nofollow">http://www.shillitoe.com</a>
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I use Command for two main reasons ...

1) I want to transport a request from one time or place to another. A client encapsulates logic to do something into a command and sends it over a network to a server, the server accepts it and executes it. Or a client puts a command in a queue and a thread pool pulls it out of the queue later and executes it.

2) I want to provide plug in behavior. This is probably more properly Strategy pattern as a specialization of Command. My Wiki server receives a GET or POST request. It pulls a key parameter off the request, uses the key to get a command from a map and executes the command. I can plug in new commands by adding key and command to the map from configuration without touching my core code.

Builder solves a whole different problem. If you have something that takes several steps to build and you don't want to repeat that code all over the place you can provide a builder that anybody can use. I think. I haven't read that one for a while.


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
Malli Raman
Ranch Hand

Joined: Nov 07, 2001
Posts: 312
Originally posted by Francis Shillitoe:
The command pattern lets you encapsulate a request as an object. Classes that follow this pattern have names which are verbs like "Load" or "Save". It is not mandatory to include an undo() method. Only an execute() method is required. Depending on what your class does, it may be impossible to undo the command.

The builder pattern is unrelated. In the builder pattern the construction logic of an object is moved outside of the class to instantiate.


Still I am not clear about the difference.

Thanks from your reply. Can you consider the cook class can implement the builder pattern. Because the cooke will do the same process to cook a burger /pizza?
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
i think struts is a good example of Command Pattern.
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 861
Still I am not clear about the difference.

1)A builder is used to construct a complex object/set of objects that would be difficult for a user of the class to create programmatically.

I created a class called ArraySQL that allows a developer to query Object[][] (arrays) with a SQL-like syntax. Underneath the hood what has to be done to build the objects is very complex, however the builder pattern allowed me to provide a simpler SQL syntax for developers to query the arrays: "select rowNum(), * from array where fname='steve' and lname='souza' order by fname desc, lname desc".

Behind the scenes this simple SQL syntax gets converted into many different objecs. It would be difficult for the developer to build this underlying structure directly.

2) Firstly, the command pattern does not REQUIRE undo.

Also, the command pattern (unlike builder) has NOTHING to do with constructing objects. A good use of the command pattern follows. Say you provide a class that iterates over files and executes commands such as copy, delete, zip, move, rename. By using the command pattern you can allow other developers to register their own commands to execute such as encrypt, decrypt, countWords, changeOwnerShip etc.

The command interface gives away no clues as to what the action taken is. The interface usually has a method named 'execute(...)' which indicates the generic nature of the command.
[ December 18, 2004: Message edited by: steve souza ]

http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
Elisabeth Robson
author
Ranch Hand

Joined: May 14, 2004
Posts: 173
    
    6
Malli,
That's too funny - I hadn't thought before that once your burger is made, you can't really undo it (although I guess you could throw it in the trash, which would be the closest you could get to a real world undo).

Anyway, the diner example was supposed to simply be a metaphor to help readers understand the command pattern. But sometimes, it's true, you won't be able to undo. If you use the command pattern to implement a printer (command prints a file), then that would be an instance where again, you wouldn't be able to undo.

The difference between command and builder is this: Command is design to encapsulate method invocation, so that the caller doesn't have to worry about exactly WHAT will happen and HOW it will happen, the caller just knows that they can call execute() to make it happen. If you think about the remote control example, the caller (the person pushing the remote) is just pushing a button. If the remote wasn't labelled, the caller wouldn't know what would happen, but would expect something to turn on or off.

Builder, however, is for constructing things in steps. You could imagine the cook in the diner using Builder to construct his burger: he'd create a Burger object, and then he'd say, burger.makePatty(), burger.chopLettuce(), burger.chopOnion(), burger.sliceTomato(), burger.addBuns(), in the order he wanted to put things on the burger. He'd get back a fully prepared burger. So the cook has a lot of control over the "building" of the burger. Contrast this with the Factory pattern, where the building of the object is more often hidden from the caller - The waitress might use the Factory pattern to say cook.createBurger().

So Command and Builder are really quite different. Command is used to make it easy for you to insert objects that know how to "execute" something and you want to separate the caller from all the objects doing the executing. Builder gives the caller control over how an object is created by allowing the caller to specify the sequence of steps to create that object.

I hope this makes sense.

Elisabeth


Co-Author of Head First JavaScript Programming
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 861
You could imagine the cook in the diner using Builder to construct his burger: he'd create a Burger object, and then he'd say, burger.makePatty(), burger.chopLettuce(), burger.chopOnion(), burger.sliceTomato(), burger.addBuns(), in the order he wanted to put things on the burger.

Sounds like my example of builder pattern of a SQL statement is not really the builder pattern. The sql is a text representation of the object that you would like to build and it hides the underlying build process, and objects created. The builder pattern requires hardcoding of java commands that instruct the program how to create the underlying object. What pattern would text string instructions such as a sql statement be (i.e. select rownum(), * from array where col0='steve' order by col0 desc, col1 asc)?
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Steve, I think your SQL statement example closely matches the Interpreter pattern.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Malli Raman
Ranch Hand

Joined: Nov 07, 2001
Posts: 312
Thanks Elisabeth, Steve. The examples you have given is really useful to understand the command pattern and builder pattern.
 
jQuery in Action, 2nd edition
 
subject: Head First Design Pattern -Command Pattern