16. Strategy mode–Strategy

Hits: 0

Strategy mode:
Strategy mode defines algorithm families and encapsulates them separately so that they can be replaced with each other. This mode allows algorithm changes without affecting customers who use the algorithm . Additionally, the strategy pattern simplifies unit testing, as each algorithm has its own class that can be tested individually through its own interface.

The problems to be solved by the Strategy mode and the Template mode are the same (similar), both for decoupling between the concrete implementation of the business logic (algorithm) and the abstract interface. The Strategy mode encapsulates the logic (algorithm) into a class (Context), implements the implementation of the specific algorithm in the combined object by means of combination, and then delegates the implementation of the abstract interface to the combined object by means of delegation.

The typical structure diagram of Strategy mode is:

Taking the shopping mall sales strategy in “Dahua Design Mode” as an example, there are discount strategies and cashback strategies in the shopping mall, which are managed through context.
The implementation code of the Strategy pattern is as follows:

//strategy.h
#ifndef _STRATEGY_H_
#define _STRATEGY_H_

// Market strategy 
class  Strategy 
{ 
public :
    Strategy();
    virtual ~Strategy();
    virtual void AlgrithmInterface() = 0;
protected:
private:
};

// Discount strategy 
class  ConcreteStrategyA : public Strategy
{
public:
    ConcreteStrategyA();
    virtual ~ConcreteStrategyA();
    void AlgrithmInterface();
protected:
private:
};

// Cashback strategy 
class  ConcreteStrategyB : public Strategy
{
public:
    ConcreteStrategyB();
    virtual ~ConcreteStrategyB();
    void AlgrithmInterface();
protected:
private:
};
#endif //~_STRATEGY_H_


//Strategy.cpp
#include "Strategy.h"
#include <iostream>
using namespace std;
Strategy::Strategy()
{
}
Strategy::~Strategy()
{
    cout<<"~Strategy....."<<endl;
}
void Strategy::AlgrithmInterface()
{
}
ConcreteStrategyA::ConcreteStrategyA()
{
}
ConcreteStrategyA::~ConcreteStrategyA()
{
    cout<<"~ConcreteStrategyA....."<<endl;
}
void ConcreteStrategyA::AlgrithmInterface()
{
    cout << "test ConcreteStrategyA.....discount strategy" << endl ;
}
ConcreteStrategyB::ConcreteStrategyB()
{
}
ConcreteStrategyB::~ConcreteStrategyB()
{
    cout<<"~ConcreteStrategyB....."<<endl;
}
void ConcreteStrategyB::AlgrithmInterface()
{
    cout << "test ConcreteStrategyB.....Cashback Strategy" << endl ;
}


//Context.h
#ifndef _CONTEXT_H_
#define _CONTEXT_H_
class Strategy;
/**
* This class is the key to the Strategy mode and the fundamental difference between the Strategy mode and the Template mode.
*Strategy realizes the heterogeneity of algorithm (implementation) through "combination" (delegation), while Template mode adopts the way of inheritance
*The difference between these two modes is also the difference between inheritance and composition in the way of implementing interface reuse
*/
class Context
{
public:
    Context(Strategy* stg);
    ~Context();
    void DoAction();
protected:
private:
    Strategy* _stg;
};
#endif //~_CONTEXT_H_


//Context.cpp
#include "Context.h"
#include "Strategy.h"
#include <iostream>
using namespace std;
Context::Context(Strategy* stg)
{
    _stg = stg;
}
Context::~Context()
{
    if (!_stg)
        delete _stg;
}
void Context::DoAction()
{
    _stg->AlgrithmInterface();
}


//main.cpp 
# include  "Context.h"
 # include  "Strategy.h"
 # include  <iostream>
 using  namespace  std ;
 int  main ( int argc, char * argv[])
 {
     // discount strategy 
    Strategy* ps1 = new ConcreteStrategyA();
    Context* pc1 = new Context(ps1);
    pc1->DoAction();

    // Cashback 
    Strategy Strategy* ps2 = new ConcreteStrategyB();
    Context* pc2 = new Context(ps2);
    pc2->DoAction();

    return 0;
}

The following is a comparison of the advantages and disadvantages of Template mode and Strategy mode.
Advantages of Template mode (inheritance):
1) It is easy to modify and extend those reused implementations.
Disadvantages of Template mode (inheritance):
1) Encapsulation is destroyed, and the implementation details of the parent class in inheritance are exposed to subclasses;
2) “white box” reuse, the reason is in 1);
3) When the implementation of the parent class When changed, all its subclasses will have to change with it
4) The implementation inherited from the parent class cannot be changed during runtime (it is determined during compilation).

Advantages of Strategy pattern (combination):
1) “black box” reuse, because the internal details of the contained objects are invisible to the outside world;
2) Good encapsulation, because of 1);
3) Implementation and abstraction are very dependent Small (the dependency between the composed object and the composed object is small);
4) The implementation can be dynamically defined at runtime (through a pointer to the same type, typically a pointer to an abstract base class).
Disadvantages of Strategy mode (combination):
1) Too many objects in the system.

You may also like...

Leave a Reply

Your email address will not be published.