112

What is the difference between the Strategy pattern and the Command pattern? I am also looking for some examples in Java.

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
Krishna
  • 7,154
  • 16
  • 68
  • 80
  • first search hit: http://www.coderanch.com/t/100214/patterns/Command-vs-Strategy-Pattern – Mitch Wheat Jan 29 '11 at 03:36
  • 2
    Related: http://stackoverflow.com/questions/1673841/examples-of-gof-design-patterns – BalusC Jan 29 '11 at 04:23
  • possible duplicate of [Strategy pattern vs. Command pattern](http://stackoverflow.com/questions/3883692/strategy-pattern-vs-command-pattern) – pramodc84 Dec 18 '14 at 14:44

6 Answers6

152

Typically the Command pattern is used to make an object out of what needs to be done -- to take an operation and its arguments and wrap them up in an object to be logged, held for undo, sent to a remote site, etc. There will tend to be a large number of distinct Command objects that pass through a given point in a system over time, and the Command objects will hold varying parameters describing the operation requested.

The Strategy pattern, on the other hand, is used to specify how something should be done, and plugs into a larger object or method to provide a specific algorithm. A Strategy for sorting might be a merge sort, might be an insertion sort, or perhaps something more complex like only using merge sort if the list is larger than some minimum size. Strategy objects are rarely subjected to the sort of mass shuffling about that Command objects are, instead often being used for configuration or tuning purposes.

Both patterns involve factoring the code and possibly parameters for individual operations out of the original class that contained them into another object to provide for independent variability. The differences are in the use cases encountered in practice and the intent behind each pattern.

Jeffrey Hantin
  • 35,734
  • 7
  • 75
  • 94
  • 63
    Just brief, a command has different purpose. For example: CutCommand, DeleteCommand, CopyCommand, SortCommand,.... A strategy has same purpose but different approach. In sorting algorithm, we have: BubbleSort, SelectionSort,... – Hien Nguyen Sep 11 '17 at 10:34
  • 2
    Just to top up with an explanation that has been with me for a long time: "If I tell my driver that I want to reach destination A by 10 AM - that's command pattern. If I tell my driver to drive at 60km/hr for 10mins and then 20km/hr for 20 mins - that's strategy pattern" – Utsav T Feb 15 '23 at 17:59
85

Words are already given in the other answer. Here is the difference in concrete code.

public class ConcreteStrategy implements BaseStrategy {

    @Override
    public void execute(Object argument) {

        // Work with passed-in argument.

    }

}

public class ConcreteCommand implements BaseCommand {

    private Object argument;

    public ConcreteCommand(Object argument) {
        this.argument = argument;
    }

    @Override
    public void execute() {

        // Work with own state.

    }

}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 4
    Nice answer - but a little too terse. Your comments in the code about which objects are worked on is the key, but since you don't break it out people don't give you credit. It's probably the best answer here. – Gerard ONeill Nov 24 '21 at 04:42
  • @GerardONeill Fair enough. I suppose what I was driving at is that in the absence of clear intent hints, the two patterns are not the easiest thing to pull apart, because they both reify choice of code path into an object but do little else otherwise. – Jeffrey Hantin Mar 05 '23 at 15:03
  • @JeffreyHantin Hah - your answer is fine; but it needs to be more concrete. This answer was fine, but it needed more of an explanation. I'm glad we get to see both answers on the same question :) – Gerard ONeill Mar 07 '23 at 23:08
56

Strategy - Quicksort or Mergesort [algo change]

Command - Open or Close [action change]

Fakrudeen
  • 5,778
  • 7
  • 44
  • 70
14

The main difference is , the command does some action over the object. It may change the state of an object.

While Strategy decides how to process the object. It encapsulates some business logic.

Asif Malek
  • 169
  • 1
  • 7
8

Strategy pattern is useful when you have multiple implementations (algorithms) for a given feature and you want to change the algorithm at runtime depending on parameter type.

Salient features of Strategy pattern

  1. It's a behavioural pattern
  2. It's based on delegation
  3. It changes guts of the object by modifying method behaviour
  4. It's used to switch between family of algorithms
  5. It changes the behaviour of the object at run time

Real World Example of the Strategy Pattern

Command pattern is used to enable loose coupling between Invoker and Receiver. Command, ConcreteCommand, Receiver, Invoker and Client are major components of this pattern.

Different Receivers will execute same Command through Invoker & Concrete Command but the implementation of Command will vary in each Receiver.

e.g. You have to implement "On" and "Off" functionality for TV & DVDPlayer. But TV and DVDPlayer will have different implementation for these commands.

Have a look at below posts with code examples :

Using Command Design pattern

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
0

I think a big difference here is that Strategy pattern is used when you need to shuffle between different objects that implement the same interface, but Command Pattern is used to shuffle between some objects that implement different interfaces ( as it encapsulates them into other objects called "Command Objects" ) and pass these command objects just like Strategy pattern does.