代码实现:
#include <iostream>
using namespace std;
class Strategy
{
public:
virtual void crypt() = 0;
};
class DES : public Strategy
{
public:
virtual void crypt()
{
cout << "DES crypt" << endl;
}
};
class AES : public Strategy
{
public:
virtual void crypt()
{
cout << "AES crypt" << endl;
}
};
class Context
{
public:
void setStrategy(Strategy* strategy)
{
this->strategy = strategy;
}
void operate() {
strategy->crypt();
}
private:
Strategy* strategy;
};
void main()
{
Strategy* strategy = new DES;
Context* context = new Context;
context->setStrategy(strategy);
context->operate();
delete context;
delete strategy;
}