• 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

Command vs Strategy Pattern?

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can somebody explain with a simple example the basic difference between these two? I did a google and bit confused with all the different explanation.

Also can somebody send me the links to good Design Pattern resource(preferably with Java examples).

Regards,
Vijay
[ July 26, 2006: Message edited by: Vijay Kashyap ]
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vijay Kashyap:
Can somebody explain with a simple example the basic difference between these two? I did a google and bit confused with all the different explanation.



The difference is a little bit subtle, and more in the intention than in structure.

Command encapsulates a single action. It therefore tends to have a single method with a rather generic signature. It often is intended to be stored for a longer time and to be executed later - or it is used to provide undo functionality (in which case it will have at least two methods, of course).

Strategy, in contrasst, is used to customize an algorithm. A strategy might have a number of methods specific to the algorithm. Most often strategies will be instanciated immediately before executing the algorithm, and discarded afterwards.

I have to admit, though, that there are cases where I can't tell for sure whether something is a Command or a Strategy. It's not always that important, anyway...


Also can somebody send me the links to good Design Pattern resource(preferably with Java examples).



http://faq.javaranch.com/view?DesignPatternFaq
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cool strategy example:

IBM has a neat tank fighting game where you write a tank and send it out to battle others. You control code that scans for other tanks, moves around, chooses targets, aims, fires, etc. Some of the coolest tanks switch strategies for each of these tasks. For example, if there is nobody close they might seek and chase opponents. If they're under fire they might run away. If they get close to the game boundaries they might move to the middle. It looks something like:

The code that changes strategies could be anywhere, not all clumped together like that, maybe in response to various events. Hmmm, I wonder if that isn't State, yet another pattern with very similar structure and slightly different intent. What do you think?
 
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stan James:
IBM has a neat tank fighting game where you write a tank



Not any more!
robocode


It was Peter van der Linden's "Just Java" that made me aware of robocode's existance.
 
Vijay Kashyap
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Guys for clearing up the facts
 
reply
    Bookmark Topic Watch Topic
  • New Topic