• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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
 
Today you are you, that is turer than true. There is no one alive who is youer than you! - Seuss. Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic