Strategy- Strategy Pattern

Strategy- Strategy Pattern

After studying design, I think: design patterns in many of the same model, which describes are very much alike realize it is very similar. And its main difference is the general purpose of solving practical problems, but have their own emphasis. Implemented in C ++ design patterns generally also that several methods: virtual functions implemented by polymorphic achieve multiplexing interface functions and the like inheritance and composition, formed by independent abstract class, weak coupling is achieved or a name separate class changes, and by a reference or pointer to implement the message transfer communications.

Behavioral patterns Introduction

http://blog.163.com/zhoumhan_0351/blog/static/39954227201043153542462/

    Define a series of algorithms, encapsulate them one by one, and makes them interchangeable. This mode makes the algorithm can be varied independently of the clients that use it.

class Strategy{

public:

Strategy(){}

virtual void AlgorithmInterface(){}

    //...

}

class ConcreteStrategyA:public Strategy{

public:

ConcreteStrategyA () {}

void AlgorithmInterface(){

//...

}

}

class ConcreteStrategyB:public Strategy{

public:

ConcreteStrategyB () {}

void AlgorithmInterface(){

//...

}

}

class ConcreteStrategyC:public Strategy{

public:

ConcreteStrategyC () ​​{}

void AlgorithmInterface(){

//...

}

}

class Context{

public:

void Operaton(){

theStrategy->AlgorithmInterface();

}

private:

    Strategy * theStrategy; // replacement of the algorithm

}

Published 81 original articles · won praise 17 · views 6013

Guess you like

Origin blog.csdn.net/hopegrace/article/details/103870809
Recommended