This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes OO, Patterns, UML and Refactoring and the fly likes 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 "command pattern" Watch "command pattern" New topic
Author

command pattern

anagha desai
Greenhorn

Joined: May 08, 2008
Posts: 22
Was reading Head First design patterns for above pattern .

(From the xample given in the book )

I have either not understood / or feel that there seem to be a couple of issues with this pattern ::

1 >
looks like there will be a number of Command Objects to be created for the same receiver if the receiver is to perform a no of actions :

Ex : Lets say there is a Receiver called :: "Gadget"
It can perform the following operations ::
" on "
" off "
" run "
" bark "

Now for such a receiver if I were to use this pattern - then I would have 4 Command Objects to encapsulate / wrap the four calls ?

Wouldnt this be too much of classes ?

2 >
The receiver is wrapped in the command
The command is wrapped in the invoker
Client calls the execute method of command embedded in invoker

The client needs to upfront know the command that needs to be used
Client needs to know the type of Receiver and needs to set it to the command

What I dont understand / find very nice is - the client needs to do a lot of things himself .

Why dont I just call the method of the receiver rather than juggle around with so many settings of one object into the other ?


I am sure that I will get responses - but please do help me in grasping this concept .

The problem at my end seems to be - I do try and read but unless the concept is understood - it tends to fade over time

This is the third / fourth time that I am reading this and other patterns !

Regards,
-anagha
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Regarding 1> - how many is *too* many? And what bad things happen if we have too many?

Don't know the example from the book, so I can't help you with 2>, sorry.


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
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 860
I don't know the example in the book either. A good use for the command pattern would be when you know you can't define all possible actions and you know in the future you or others would like to provide more of them.

One example I can think of is you have a class that iterates through a file system performing actions on each file. You initially need to only delete files. However, you know that in the future this will probably grow to performing more actions. The command pattern allows you to separate file iteration from file action and if done properly anyone can in the future provide new actions. This is particularly important for framework code. Other possible future actions are zip, rename, checksum, copy, move, ftp, ....

Compare this to the lack of flexibility of creating a class that couples iteration and action to closely.


http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
anagha desai
Greenhorn

Joined: May 08, 2008
Posts: 22
Thanks Ilja and Steve for the responses .

So from the framework perspective - would Action classes be an example of command pattern ( not simply because they contain a "execute" method !)

Here we extend and define our own action classes as we desire and then make calls to the model / business logic ( similar to the Receiver in command pattern )

So struts action classes could be thought of as command objects ?

Thanks ,
-anagha
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by steve souza:
A good use for the command pattern would be when you know you can't define all possible actions and you know in the future you or others would like to provide more of them.


Another good use is to implement undo/redo functionality. Here each command also knows how to undo itself, and you remember each executed command on a stack.
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 860
Here is a nice writeup on wikipedia.

http://en.wikipedia.org/wiki/Command_pattern
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 860
So from the framework perspective - would Action classes be an example of command pattern ( not simply because they contain a "execute" method !

Just because an object has an execute method doesn't make it a command pattern. More important than the method name 'execute' is that it follows the spirit of the description of the Command pattern. I took the following from wikipedia...

In object-oriented programming, the Command pattern is a design pattern in which objects are used to represent actions. A command object encapsulates an action and its parameters.

So you don't have to use the name execute if you honor the above.

So struts action classes could be thought of as command objects ?

The struts action class sounds like an implementation of the Command pattern, though I haven't used it, so can't say for sure.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: command pattern
 
Similar Threads
multi-inheritance
About Command Pattern Design attern
Club Business Delegate with Command Pattern, pros-cons
Too Simple?
difference between factory pattern , abstract factory pattern and command pattern