一.策略模式简介
策略模式是一种定义一系列算法的方法,从概念上来看,所有这些算法完成的都是相同的工作,只是实现不同,它可以以相同的方式调用所有的算法,减少了各种算法类与使用算法类之间的耦合。简单工厂模式是
创建型
模式,它的作用是
创建对象
。策略模式是
行为型
模式,作用是
在许多行为中选择一种行为
,关注的是行为的多样性。简单来说策略模式就是将多种行为方法封装成一种策略并统一做调用,调用的方法相同,具体策略执行的算法不同。下面以商场促销的应用为场景简要介绍策略模式的使用。并在最后和简单工厂进一步结合,做更好的封装,给用户层面提供更简单的操作。
二.单纯策略模式cpp代码实现
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
//策略基类
template <class mytype>
class strategy
{
public:
strategy() = default; //默认构造函数
//定义纯虚函数,由子类继承
virtual mytype getresult() const = 0;
//赋值运算符=重载
strategy<mytype>& operator=(const strategy<mytype>& strategy_t)
{
this->money_input = strategy_t.money_input;
}
mytype money_input;
private:
};
//打七折策略
template <class mytype> //定义模板
class strategy_7discount :public strategy<mytype>
{
public:
virtual mytype getresult()const
{
cout << "打七折策略" << endl;
return this->money_input * 0.7;
}
private:
};
//打八折策略
template <class mytype> //定义模板
class strategy_8discount :public strategy<mytype>
{
public:
virtual mytype getresult()const
{
cout << "打八折策略" << endl;
return this->money_input * 0.8;
}
private:
};
//满减策略
template <class mytype> //定义模板
class strategy_oversub :public strategy<mytype>
{
public:
virtual mytype getresult()const
{
cout << "满减策略" << endl;
if (this->money_input > 200)return this->money_input - 50;
else if (this->money_input > 100)return this->money_input - 20;
return this->money_input;
}
private:
};
//定义一个策略context
template <class mytype> //定义模板
class context
{
public:
strategy<mytype>* my_strategy; //定义我的策略,也就是在context内部维护了策略的一个基类指针
//context默认构造函数
context(strategy<mytype>* strategy_t)
{
my_strategy = strategy_t; //初始化context时传入具体的策略对象的指针
}
//上下文接口
mytype context_interface()
{
return my_strategy->getresult(); //根据具体的策略对象,调用其算法的方法
}
private:
};
int main(void)
{
float result = 0;
strategy_7discount<float>* strategy_7discount_t = new strategy_7discount<float>; //1.new创建具体的策略算法对象
strategy_7discount_t->money_input = 312; //2.设置策略对象的入口参数
context<float> my_context(strategy_7discount_t); //3.创建context策略对象my_context并将策略算法对象指针作为入口参数传入
result = my_context.context_interface(); //4.调用context策略的接口方法
cout << "最终的价格是:" << result << endl;
cout << "--------------------------------------------------------------" << endl;
int res = 0;
strategy_oversub<int>* strategy_oversub_t = new strategy_oversub<int>;
strategy_oversub_t->money_input = 402;
context<int> my_context1(strategy_oversub_t);
res = my_context1.context_interface();
cout << "最终的价格是:" << res << endl;
return 0;
}
执行结果:
打七折策略
最终的价格是:218.4
--------------------------------------------------------------
满减策略
最终的价格是:352
三.策略模式+简单工厂模式cpp代码实现
在之前代码的基础上增加一个工厂类cashcontext。然后在main中使用工厂来做策略调用。
//定义一个简单工厂,来选择使用哪个运算
template <class mytype> //定义模板
class cashcontext
{
public:
strategy<mytype>* strategy_t = NULL;
context<mytype>* my_context = NULL;
//默认构造函数
cashcontext() = default;
//有参构造函数
cashcontext(int choice, mytype money)
{
switch (choice)
{
case 1: //七折策略
strategy_t = new strategy_7discount<mytype>; //1.new创建具体的策略算法对象
break;
case 2: //八折策略
strategy_t = new strategy_8discount<mytype>; //1.new创建具体的策略算法对象
break;
case 3: //满减策略
strategy_t = new strategy_oversub<mytype>; //1.new创建具体的策略算法对象
break;
default:
cout << "输入参数有误,请重新输入" << endl;
break;
}
strategy_t->money_input = money; //设置入口参数
my_context = new context<mytype>(strategy_t); //创建context策略对象
}
//获取结果
mytype get_result(void)
{
return my_context->context_interface();
}
private:
};
int main(void)
{
float result = 0;
cashcontext<float> cashcontext_t(1, 452); //1表示七折策略
result = cashcontext_t.get_result();
cout << "最终的价格是:" << result << endl;
return 0;
}
执行结果:
打七折策略
最终的价格是:316.4
四.总结
简单工厂模式和策略模式的最大区别是:简单工厂模式注重返回的子类对象。策略模式注重执行策略方法的调用。简单工厂模式和策略模式的配合使用会使代码结构更加清晰。
版权声明:本文为weixin_42700740原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。