大话设计模式C++实现--策略模式
2016-07-05 17:20
465 查看
// 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;
}
//商城收银软件
//采用策略模式: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++中的string类的用法总结
- 大话设计模式c++实现--简单工程模型
- 关于校验和计算方面的C++代码,哪位大侠帮忙解读一下,小弟初学,不太懂!谢谢!
- c++书籍推荐
- CRC16 生成校验
- c++面向对象编程入门(一)
- VC++界面编程之--阴影窗口的实现详解
- C++ 类的静态成员
- C/C++堆、栈及静态数据区详解
- C++中变量使用前必须初始化,否则报错~
- C++ tricks
- 解决C/C++程序执行一闪而过的方法(三种办法)
- C++引用调用、内联函数、带默认形参值的函数
- 决策树生成c++实现(不含剪枝)
- 在C++中for、if...内定义的变量在其之外就失效了~
- c/c++ 管道编程
- Socket通信原理探讨(C++为例)
- C语言中scanf被跳过-求大神解决
- 3DES加解密 C语言
- C++ Scoket的升级版(多态的运用)