112

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

3

6 Answers 6

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.

2
  • 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,... Sep 11, 2017 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 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.

    }

}
3
  • 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. Nov 24, 2021 at 4: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. Mar 5 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 :) Mar 7 at 23:08
56

Strategy - Quicksort or Mergesort [algo change]

Command - Open or Close [action change]

1
  • SOAP webservices are one example for command pattern . Feb 9, 2021 at 7:41
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.

0
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

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.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.