策略模式

简介:
#include <string>
using std::string;
/************************************************************************/
/* 请实现一个商场收银软件,包含正常收费,打折收费和返利收费三种具体策略 */
/************************************************************************/

/*
策略模式是一种定义一系列算法的方法,从概念上来看,所有这些算法完成都是相同的工作,只是实现不同,
它可以以相同的方式调用所有算法,减少各种算法类与使用算法类之间的耦合。

策略模式的straterty类觌为Context 定义了一系列的算法和行为,继承有助于析取这些算法中的公共功能。

用CashContext对象管理在堆中分配的算法对象(CashSuper),可以免去调用者手动delete对象
*/

class CashSuper
{
public:
virtual float acceptCash(float money){return 0.0;}
};

class CashNormal : public CashSuper
{
public:
virtual float acceptCash(float money)
{
return money;
}
};

class CashRebate : public CashSuper
{
private:
float moneyRebate;
public:
CashRebate(float r):moneyRebate(r){}
CashRebate():moneyRebate(1){}
float acceptCash(float money)
{
return moneyRebate*money;
}
};

class CashReturn : public CashSuper
{
private:
float moneyCondition;
float moneyReturn;
public:
CashReturn():moneyReturn(0),moneyCondition(0){}
CashReturn(float condition,float ret):moneyCondition(condition),moneyReturn(ret){}
float acceptCash(float money)
{
if(money>=moneyCondition)
money=(int)(money/moneyCondition)*moneyReturn;
return money;
}
};

class CashFactory
{
public:
static CashSuper* createCashAccept(string flag)
{
CashSuper *super=NULL;
if(flag=="正常收费")
super=new CashNormal;
else if (flag=="打8折")
super=new CashRebate(0.8f);
else if(flag=="满300 返100")
super=new CashReturn(300,100);
return super;
}
};

class CashContext
{
protected:
CashSuper *cs;
public:
CashContext():cs(0){}
~CashContext()
{
if(cs)
delete cs;
}
CashContext &operator=(CashSuper*csuper)
{
if(cs) delete cs;
cs=csuper;
return *this;
}
float GetRedult(float money)
{
return cs->acceptCash(money);
}
};


// strategy.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "CashCalculate.h"
#include <iostream>
using namespace std;

//请实现一个商场收银软件,包含正常收费,打折收费和返利收费三种具体策略
int _tmain(int argc, _TCHAR* argv[])
{
//////////////////////////////////////////////////////////////////////////
//策略模式
CashContext cs;
int choice=-1;
cout<<"请输入收费方式(0 1 2 )"<<endl;
cin>>choice;
switch(choice)
{
case 0:
cs=new CashNormal;
break;
case 1:
cs=new CashRebate(0.8f);
break;
case 2:
cs=new CashReturn(100,10);
break;
default:
return 0;
}

float price=0.0;
int number=0;
cout<<"输入单价"<<endl;
cin>>price;
cout<<"请输入数量"<<endl;
cin>>number;
float total=cs.GetRedult(price*number);
cout<<"结果为"<<total<<endl;
system("pause");
return 0;

//////////////////////////////////////////////////////////////////////////
//工厂模式
/*float price=0.0;
cout<<"请输入单价"<<endl;
cin>>price;
int number;
cout<<"请输入数量"<<endl;
cin>>number;
cout<<"单价:"<<price<<" "<<"数量:"<<number<<endl;
cout<<"----------------------------------------------"<<endl;
float currentMoney=price*number;
string choice;
cout<<"请输入收费方式(正常收费 打8折 满300 返100 )"<<endl;
cin>>choice;
CashSuper *cashsuper;
cashsuper=CashFactory::createCashAccept(choice);
cout<<"应交钱为"<<cashsuper->acceptCash(currentMoney);
delete cashsuper;
return 0;
*/



//////////////////////////////////////////////////////////////////////////
//一般模式
/*
float price=0.0;
cout<<"请输入单价"<<endl;
cin>>price;
int number;
cout<<"请输入数量"<<endl;
cin>>number;
cout<<"单价:"<<price<<" "<<"数量:"<<number<<endl;
cout<<"----------------------------------------------"<<endl;

float total=0.0;
float discount=1.0;
float freeLimit=0.0;
float freeMoney=0.0;

int choice=-1;
cout<<"请输入收费方式(0 正常收费 1 打折收费 2 返利收费 )"<<endl;
cin>>choice;
switch(choice)
{
case 0:
total=number*price;
break;
case 1:
cout<<"请输入折扣 如 0.8 0.6"<<endl;
cin>>discount;
total=number*price*discount;
break;
case 2:
cout<<"请输入返利单元和返利金额 比如 满100元 送 10元等"<<endl;
cin>>freeLimit>>freeMoney;
total=number*price;//
total=total-((int)(total/100) *10);
break;
default:
cout<<"输入非法"<<endl;
return 0;
}

cout<<"最终价格为"<<total<<endl;
*/
system("pause");
return 0;
}
复制代码

 

相关文章
|
4月前
|
算法 数据安全/隐私保护
行为型 策略模式
行为型 策略模式
18 1
|
8月前
|
设计模式 算法 Java
什么场景要使用策略模式,什么场景不能使用?
如果,让我来设计,我最先想到的就是策略模式。另外,我把往期面试题解析的配套文档我已经准备好,想获得的可以在我的煮叶简介中找到。那么什么场景要使用策略模式,什么场景又不应该使用策略模式呢?我们可以先来看官方对策略模式的定义。
108 0
|
8月前
|
前端开发
策略模式
策略模式
55 0
|
9月前
|
设计模式 算法
策略模式详细介绍
策略模式(Strategy Pattern)是一种行为型设计模式,它定义了一系列的算法,并将每个算法封装到具有共同接口的独立类中,使得它们可以互相替换。策略模式可以让算法的变化独立于使用它的客户端。
94 0
|
9月前
|
算法 测试技术 C#
C#策略模式
C#策略模式
40 0
|
10月前
|
设计模式 前端开发
关于策略模式我所知道的
关于策略模式我所知道的
58 0
|
10月前
|
算法 程序员 开发工具
简单说说我对策略模式的了解
简单说说我对策略模式的了解
62 0
|
设计模式 算法
我学会了,策略模式
策略模式属于行为型模式,这个类型的设计模式总结出了 类、对象之间的经典交互方式,将类、对象的行为和使用解耦了,花式的去使用对象的行为来完成特定场景下的功能。
96 0
我学会了,策略模式