【C++ 策略设计模式 】

策略设计模式
定义一组算法,将每个算法都封装起来,并且使它们之间可以互换

StartegyMode.cpp


#include <iostream>
 
using namespace std;
 
class IStrategy {
public:
	IStrategy() {}
	virtual ~IStrategy() {}
 
	virtual void Discountsfun() = 0;
};
 
class qxj : public IStrategy {
public:
	qxj() {}
	~qxj() {}
 
	void Discountsfun() {
		cout << "run qxj 20'%' off" << endl;
	}
};
 
class gqj : public IStrategy {
public:
	gqj() {}
	~gqj() {}
 
	void Discountsfun() {
		cout << "run gqj 10'%' off " << endl;
	}
};
 
class Context {
public:
	Context(IStrategy *IStrategy) { pIStrategy = IStrategy; }
	~Context() { delete pIStrategy; }
 
	void run() {
		pIStrategy->Discountsfun();
	}
 
private:
	IStrategy *pIStrategy;
};
 
 // g++ StartegyMode.cpp -o StartegyMode
int main(int argc, char*argv[])
{
	Context context1(new qxj());
	context1.run();
 
	Context context2(new gqj());
	context2.run();
 
	return 0;
}

你可能感兴趣的:(c++,设计模式,开发语言)