aspose file tools*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Command Pattern - what is it applicable for ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Command Pattern - what is it applicable for ?" Watch "Command Pattern - what is it applicable for ?" New topic
Author

Command Pattern - what is it applicable for ?

Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 761

I'm not sure where I've read this from, but somehow, this
statement got stucked in my mind:-
"Use the Command Pattern whenever you come upon code with
a huge switch statements. Switch statements are not OO."
Comments ?
Pho


Regards,

Pho
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

Let's say you have a program that expects one of several known arguments as input. For example, it's a game, and the player can run, sleep, eat, hide, etc.
One solution might be to 'switch' based on the command type, where each case in the switch does some necessary variable setting and calls the appropriate function run(), sleep(), eat(), hide(), etc. As these structures add more cases, however, they become more difficult to read and maintain.
A Command Pattern could be used to treat each case as a subclass of a generic Command, for example. Each object contains specific behavior, and becomes easier to add, remove, and debug because it is self-contained.
A switch statement would probably be computationally faster, ultimately, but is the effort required to debug and maintain a big one worth it? Using the Command pattern as a guide, you avoid potentially monolithic code structures, possibly sacrificing speed of computation to achieve more readable code.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
[This message has been edited by Michael Ernest (edited February 11, 2001).]


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

I got inspired, and pulled out GoF to explain this pattern is more detail. Here's a really quick and dirty example:

Actually, that example could use a few more passes before it's worth talking about, but I've played out the roles, anyway. We'd probably all like to see the Player object as an Invoker of the Command rather than GameController. I was just rattling along here without much forethought, but I hope the premise is apparent.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
[This message has been edited by Michael Ernest (edited February 11, 2001).]
Shankar Ganesh
Ranch Hand

Joined: Oct 01, 2000
Posts: 34
Originally posted by Michael Ernest:
I got inspired, and pulled out GoF to explain this pattern is more detail. Here's a really quick and dirty example:


Thanks Michael! Hope to c u inspired more often
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12805
    
    5
As I recall, another nice thing about the Command pattern is that you can provide an easy UNDO function if you keep a list of the last ## Command objects. Naturally your Command object must provide for storing the previous state.
Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Command Pattern - what is it applicable for ?