Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Strategy pattern and Command pattern

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

like image 928
Krishna Avatar asked Jan 29 '11 03:01

Krishna


People also ask

What is the difference between strategy and decorator pattern?

The strategy pattern allows you to change the implementation of something used at runtime. The decorator pattern allows you augment (or add to) existing functionality with additional functionality at run time.

What is the difference between strategy and bridge pattern?

While the Strategy pattern is meant for behavior, the Bridge pattern is meant for structure. The coupling between the context and the strategies is tighter than the coupling between the abstraction and the implementation in the Bridge pattern.

What is the difference between strategy and template design pattern?

But, the key difference is that Strategy Pattern is about modifying a behaviour of a context in runtime using strategies, while Template Method Pattern is about following a skeleton implementation of an algorithm and modifying its behaviour by overriding methods of the skeleton class in the subclasses.

What is the Command pattern used for?

In object-oriented programming, the command pattern is a behavioral design pattern in which an object is used to encapsulate all information needed to perform an action or trigger an event at a later time. This information includes the method name, the object that owns the method and values for the method parameters.


2 Answers

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.

like image 93
Jeffrey Hantin Avatar answered Sep 29 '22 19:09

Jeffrey Hantin


Words are already given. 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.     }  } 
like image 30
BalusC Avatar answered Sep 29 '22 20:09

BalusC