大话设计模式C++实现--策略模式

本文介绍了一种利用策略模式实现的收银系统,该系统能够根据不同优惠策略计算商品总价。通过具体实例展示了正常收费、打折及返现三种策略的具体实现方式,并通过上下文(Context)类来灵活切换策略。
// designpatten2_cashsuper.cpp : 定义控制台应用程序的入口点。
//商城收银软件
//采用策略模式:strategy
//
#include "stdafx.h"
#include<string>
#include<iostream>
using  namespace std;

//抽象类作为公共基类
class CashSuper
{
public:
virtual double acceptCash(double money)
{
return -1;
}
};
  
//正常收费子类,公有继承CashSuper
class CashNormal :public CashSuper
{
public:
double acceptCash(double money) override
{
return money;
}


};

//打折收费子类,公有继承CashSuper
class CashRebate :public CashSuper
{
private:
double moneyRebate = 1;
public:
CashRebate(double moneyRebate)
{
this->moneyRebate = moneyRebate;

 
double acceptCash(double money) override
{
return money*moneyRebate;
}


};




//返现收费子类,公有继承CashSuper
class CashReturn :public CashSuper
{
private:
double moneyCondition=0;
double moneyReturn = 0;
public:
CashReturn(double moneyCondition,double moneyReturn)
{
this->moneyCondition = moneyCondition;
this->moneyReturn = moneyReturn;
}


double acceptCash(double money) override
{
double result = money;
if (money >= moneyCondition)
result = money - ((int)(money / moneyCondition))*moneyReturn;
return result;
}
};


//context类
class CashContext
{
CashSuper* cs;
public:
CashContext(string type)
{
if (type == "正常收费")
cs = new CashNormal();
else if (type == "满300减100")
cs = new CashReturn(300, 100);
else if (type == "打8折")
cs = new CashRebate(0.8);
else
{
std::cerr << "error \n";
exit(1);
}//异常退出程序
}


double GetResult(double money)
{
return cs->acceptCash(money);
}
};

//应该是界面按钮功能,此处用函数代替
double buttun_click(double txtPrice, int txtNum, string type)
{
CashContext csuper = CashContext(type);
double totalPrices = 0;
totalPrices = csuper.GetResult(txtPrice*txtNum);
return totalPrices;
}

int _tmain(int argc, _TCHAR* argv[])
{
double total = 0;
double total1 = 0;

double txtPrice=200;
int txtNum=5;
string type = "打8折";
string type1 = "满300减100";

total = buttun_click(txtPrice, txtNum, type);
total1 = buttun_click(txtPrice, txtNum, type1);

cout << "单价:" << txtPrice << "   数量;" << txtNum << "   type:" << type <<"  合计:"<<total<< endl;
cout << "单价:" << txtPrice << "   数量;" << txtNum << "   type1:" << type1 << "  合计:" << total1 << endl;
return 0;
}

### C++ 中的策略模式 #### 策略模式概述 策略模式是一种行为型设计模式,它定义了一系列算法,并将每个算法封装起来,使它们可以互换。这种模式使得算法可以在不影响客户端的情况下发生变化[^3]。 #### UML 图解 在策略模式中,通常有一个上下文(Context)类用于维持对某个具体策略(Concrete Strategy)实例的引用。而所有的具体策略都实现了相同的接口(Strategies Interface),这允许Context能够调用任何具体的策略而不必关心其内部细节[^4]。 #### 实现步骤 为了实现这一模式,在C++里首先要创建一个表示不同策略的行为接口: ```cpp // 定义策略基类 class IStrategy { public: virtual ~IStrategy() {} virtual void execute() const = 0; }; ``` 接着为各种可能的具体操作提供多个派生自`IStrategy`的不同版本: ```cpp // 具体策略A class ConcreteStrategyA : public IStrategy { public: void execute() const override { std::cout << "Executing strategy A\n"; } }; // 具体策略B class ConcreteStrategyB : public IStrategy { public: void execute() const override { std::cout << "Executing strategy B\n"; } }; ``` 最后构建一个持有并能切换当前所使用的策略的对象——即所谓的“环境”或“上下文”。 ```cpp // 上下文类 class Context { private: IStrategy* _strategy; public: // 构造函数接收一个初始策略 explicit Context(IStrategy& s):_strategy(&s){} // 设置新的策略 void set_strategy(IStrategy& new_strategy){ _strategy=&new_strategy; } // 执行选定的策略 void perform_operation(){ if (_strategy != nullptr) _strategy->execute(); else std::cerr<<"No strategy selected.\n"; } }; ``` 这样就完成了简单的策略模式框架搭建。当需求改变时只需新增加对应的策略类即可满足扩展性的要求[^5]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值