Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Command Pattern - what is it applicable for ?

 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic