【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;
}
相关推荐
西北大程序猿17 分钟前
单例模式与锁(死锁)
linux·开发语言·c++·单例模式
你不是我我27 分钟前
【Java开发日记】说一说 SpringBoot 中 CommandLineRunner
java·开发语言·spring boot
心扬39 分钟前
python网络编程
开发语言·网络·python·tcp/ip
qq_454175791 小时前
c++学习-this指针
开发语言·c++·学习
尘浮7281 小时前
60天python训练计划----day45
开发语言·python
sss191s1 小时前
校招 java 面试基础题目及解析
java·开发语言·面试
哆啦A梦的口袋呀1 小时前
基于Python学习《Head First设计模式》第六章 命令模式
python·学习·设计模式
sduwcgg2 小时前
python的numpy的MKL加速
开发语言·python·numpy
钢铁男儿2 小时前
Python 接口:从协议到抽象基 类(定义并使用一个抽象基类)
开发语言·python
超闻逸事2 小时前
【题解】[UTPC2024] C.Card Deck
c++·算法