At present, I have read some design patterns, but others can't remember. Now I feel that the strategy pattern is very similar to the state pattern, so I want to write something
First, I read the instructions on the rookie Strategy mode and State mode Therefore, I want to make an analysis and deeply understand the difference between the two modes
Strategy mode
First, let's take a look at what is the strategy mode. I summarize the following points:
1) Different strategies need to be used for different environments
2) Different methods are implemented in different strategies
3) The policy is introduced into the environment, and the methods in the policy are used to solve the problems in the environment
Based on the above points, let's write the implementation method
/* Policy base class, which has a policy method */ public class StrategyBase { virtual public void doOperation(); }
/* There are several strategies q r f */ public class Strategy_A : public StrategyBase { public void doOperation() { std::cout << "q It tested" <<std::endl; } } public class Strategy_B : public StrategyBase { public void doOperation() { std::cout << "Cross flash Slip away, slip away" <<std::endl; } } public class Strategy_D : public StrategyBase { public void doOperation() { std::cout << "Jiaotong University Hard and rigid" <<std::endl; } }
/* Create different states */ public class context { private StrategyBase* m_Strategy; public void context(StrategyBase* newStrategy) { m_Strategy = newStrategy; } public void do(){ m_Strategy->doOperation(); } }
/* Use different methods to achieve different functions */ public class Demo { private Context* context; public static void Main() { context = new Context(new Strategy_A()); context->do(); context = new Context(new Strategy_B()); context->do(); context = new Context(new Strategy_C()); context->do(); } }
This implementation is very simple, that is, a base class method is rewritten by a subclass, and then the subclass object. The base class pointer points to the subclass object, and the method of the rewritten subclass is called. This is the familiar polymorphism
State mode
State mode is to express different behaviors through different states. From this point of view, the strategy mode is the same. In fact, it is still different
1) Have an environment and design different strategies according to different environments
2) State switching can be done in subclasses
3) "The switching of different states can be realized by subclasses", that is, the states of state mode are often coupled, while the strategy mode algorithm clusters are completely decoupled.
4) The state mode separates the operations corresponding to each state, that is, for different states, different subclasses implement specific operations, and the switching of different states is realized by subclasses. When it is found that the incoming parameter is not the parameter corresponding to its own state, it switches the state to the Context class by itself; The policy mode directly depends on the parameters injected into the Context class to select the policy, and there is no operation to switch the state.
StatusA mStatusA = new StatusA( mContext) public class StatusA { StatusA(Context context) { } void do1() { print ("StatusA do1"); } void do2() { print ("StatusA do2"); } } public class StatusB { StatusA(Context context) { } void do1() { print ("StatusB do1"); } void do2() { print ("StatusB do2"); } } public class Context() { StatusBasic* mStatus = null; StatusA* mStatusA = new StatusA(); StatusB* mStatusB = new StatusB(); private setState(StatusBasic* newStatus) { mStatus = newStatus; } doAction_1() { setState(mStatusA); mStatus->do1(); } doAction_2() { mStatus->do2(); } }
To sum up later, when something is done in the context environment, the state changes. This is not perceived externally. I don't know that the state of the context changes. When I continue to operate, I find that the results are different
int main() { Context context = new Context(); context.doAction_1(); context.doAction_2(); }
int main() { Context context = new Context(); context.doAction_2(); }
doAction_ The execution result of 2 is different because the unconscious state has changed