【C++ 策略设计模式 】

策略设计模式

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

StartegyMode.cpp

javascript 复制代码
#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;
}
相关推荐
ZJPRENO1 天前
吃透软件开发六大设计原则,告别烂代码
设计模式
咖啡八杯1 天前
GoF设计模式——命令模式
java·设计模式·架构
花椒技术2 天前
HJPusher / HJPlayer SDK 实践:我们为什么把直播推播链路拆成一套可复用能力
设计模式·harmonyos·直播
卷无止境2 天前
C++ 的Eigen 库全解析
c++
卷无止境2 天前
现代 C++特性大盘点:一门脱胎换骨的老语言
c++·后端
郝学胜_神的一滴2 天前
CMake 27:缓存变量的特性、语法、类型与实操全解
c++·cmake
艺艺生辉2 天前
迭代器模式-"我也想被增强for循环"
设计模式
咖啡八杯3 天前
GoF设计模式——策略模式
java·后端·spring·设计模式
博客18004 天前
酷宝的使用方法,超好用的免费界面库,C++、MFC可用
c++·mfc·界面库·库来帮·酷宝