Please enlighten me greatly (Command and Strategy distinction)

Category: C/C++ -> Design Patterns Author: liaozeli Date: 2006-10-02 10:34:55
2006-10-02 10:34:55
I look at the recent Design Pattern, but always to no avail, for example, the most simple model of the Command and Strategy, looked out the difference between a half-day Mogao Qing, please enlighten me greatly, let me Sudden Impact, enlightened!
please greatly patience to read, I never put the garbage code to scrape together words!

# include "stdafx.h"
using namespace std;

/ / - Command patterns begin
class Command
virtual void Execute () = 0;

template <class Receiver>
class SimpleCommand: public Command
typedef void (Receiver :: * Action) ();

SimpleCommand (Receiver & r, Action a): _action (a), _receiver (r) {};
virtual void Execute () {(_receiver . * _action) ();};

Action _action;
Receiver _receiver;
/ / - -Command patterns end

class MyCommand1
void Action () {cout << "This is MyCommand1 Action "<< endl;};


class MyCommand2
void Action () {cout << "This is MyCommand2 Action" << endl;};


/ / - Strategy patterns begin
template <class AStrategy>
class Context
void Action () ; {theStrategy.DoAlgorithm ();}

AStrategy theStrategy;
/ / - Strategy patterns end

class MyStrategy1
void DoAlgorithm () {cout << "This is the MyStrategy1 Action!" ; << endl;};

class MyStrategy2
void DoAlgorithm () {cout < <"This is the MyStrategy2 Action!" << endl;};

int _tmain (int argc, _TCHAR * argv [])
cout << "This is Command Pattern invoke" << endl;
SimpleCommand <MyCommand1> acommand1 (MyCommand1 (), & MyCommand1 :: Action);;
acommand1.Execute ();
SimpleCommand <MyCommand2> acommand2 (MyCommand2 (), & MyCommand2 :: Action);
acommand2.Execute ();

cout << "This is Strategy Pattern invoke" << endl;
Context <MyStrategy1> aContext;
aContext.Action ();
Context <MyStrategy2> aContext1;
aContext1.Action ();

getchar ();
return 0;

2006-10-02 10:46:37
only from your code you can also see the difference between the command and Strategy, command object in your code to collect two types of information, a is the method name, the other is the method parameters, through the object Receiver to collect parameters. And you do not need in the strategy name information collection methods, but merely a method call strategy.
normal circumstances, command mode, the main role is to a method parameter information is collected separately for their implementation, so the command object needs to know the name of the method and its execution parameter information, The strategy is an algorithm when there are several candidates for a method. For example, a text editor - Syntax highlighting with explicit - apparently C + + and Delphi syntax highlighting is not the same, you can use this strategy mode, define two strategies such as cppStrategyDisply and DelphiStrategyDisplay, each defining a set in the inside display method in your program explicit method display a display method call strategy.
watch mode can not only see how their code is similar, we can not just look at them UML diagrams is too much alike, but depends on their purpose, to see if they detail difference. If you do not see this thing, you might think Bridge, state and strategy patterns do not need to distinguish, many models do not need to exist, the reason is very simple, many models have one thing in common: that is a class defined in another class a pointer to an object or a reference.
2006-10-02 10:56:59
to wiali (Vieri):
Oh, I think Command Pattern no problem, Strategy patterns seem to really give people bring doubt, Strategy patterns of Polymerization can also be AStrategy * pTheStrategy, context can change dynamically at runtime policy objects to provide different algorithms, and maintain their own state (Machinery Industry Press, "design mode" both), their scope of application different, and you do not provide an example of a dynamic Strategy, may be more suitable for static generic framework.

Also, I think the Command Pattern greater emphasis on one-time behavior, such that it can support transactions, such a concept of atomicity,
and should be external as internal and external behavior in order to perform an internal class approach to internal class for the body.
The Strategy focuses on algorithmic strategies can be run multiple times. Can be said to affect the external internal, because the internal class is for the external behavior
exist, Strategy and State patterns can talk together, they are based outside of the main body.

above is a personal opinion
2006-10-02 11:16:00
different concept is mainly used it.
command for command execution. usually has redo, undo function.

Strategy is mainly used to facilitate replacement algorithm
2006-10-02 11:22:57
If only a different concept (defined functions that operate on the concept), I think they will not be singled out gang Minato pages, right?
2006-10-02 11:34:22
command and strategy confused?
then the difference between the template and the strategy is not more dizzy?
2006-10-02 11:39:01
I want to know under what circumstances, be sure to use a pattern, and the other one does not apply, or that can be a huge benefit.
2006-10-02 11:50:55
is the template method and strategy were more difficult to distinguish.
recommend you do not see design pattern (as a reference manual reference can be) can watch design pattern explained, or Bob Martin of Agile software programming.
2006-10-02 12:03:09
flyinair2000 () Sir, you carefully read my code yet, this is the standard code GOF book, you can not tell just from the English name on what is the difference? ? ?
template and strategy will be more dizzy Why do? ? ?
template algorithms are abstract in the base class, and strategy algorithms are encapsulated in the subclass, are such a huge difference than what I have posted the code is not more obvious? ? ? This word does make me even more difficult to understand? ? ?
2006-10-02 12:11:34
The code book is a demonstration of it. command usually keep a stack or a queue together with.
record user to execute commands (for responses).

I think with the Strategy so different ah!
2006-10-02 12:23:54
I certainly would not put you to write template understood as C + + syntax keywords, but I think the so-called template method is one of the public variables and methods of extraction to the base class, sub-class as long as the different re-assignment, and then dynamically invoked by the multi-state, which is a C + + Virtual grammar basic application, that is, do not look at design patterns book himself can write out like pattern and abstract to the point of this level is even worse!
2006-10-02 12:35:22
I do not want to say, in which Robert Martin of the inside of the Agile Software Development, a special chapter is about Template Method and Strategy comparisons, see for yourself bar. (I do not have the leisure to bother quarrel with you, in front of me the reason I say that is because the two are comparable, and the command did not comparable)
2006-10-02 12:47:26
to find out more, command is actually a callback object-oriented package, template method and strategy are on the policy package.