• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Head First Design Pattern -Command Pattern

 
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Malli Raman
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think struts is a good example of Command Pattern.
 
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
author
Posts: 200
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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)?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Steve, I think your SQL statement example closely matches the Interpreter pattern.
 
Malli Raman
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Elisabeth, Steve. The examples you have given is really useful to understand the command pattern and builder pattern.
 
reply
    Bookmark Topic Watch Topic
  • New Topic