第2章 商场促销——策略模式
C++实现
//version 1.1
#include<iostream>
#include<stdio.h>
using namespace std;class Cash
{private:double total = 0;public: void submit(int num, double price){double totalPrices=num*price;total+=totalPrices;printf("单价: %d,数量: %d,合计:",price,total,totalPrices);}double getTotal(){return total;}
};
“哈哈,挺快的嘛。”大鸟说着,看了看小菜的代码。接着说:“现在我要求商场对商品搞活动,所有的商品打8折。”
class Cash
{private:double total = 0;int selectIndex = 0;public:void selectFormLoad() { String[] selectForm = { "正常收费", "打8折", "打7折", "打5折" }; selectedIndex = 0; } void submit(int num, double price){double totalPrices=0;switch (selectedIndex) { case 0: totalPrices = num * price; break; case 1: totalPrices = num * price * 0.8; break; case 2: totalPrices = num * price * 0.7; break; case 3: totalPrices = num * price * 0.5; break; } total+=totalPrices;printf("单价: %d,数量: %d,合计:",price,total,totalPrices);}int getSelectedIndex() { return selectedIndex; } void setSelectedIndex(int selectedIndex) { this.selectedIndex = selectedIndex; } double getTotal(){return total;}
};
“这比刚才灵活性上是好了,不过重复代码很多,4个分支语句除了打折多少不同以外几乎完全一样,应该考虑重构一下。不过这还不是最主要的,现在我的需求又来了,商场的活动加大,需要有满300返100的促销算法,你说该怎么办?”
#include<iostream>
#include<stdio.h>
using namespace std;
//现金收费抽象类
class CashSuper
{public:virtual double acceptCash(double money){;}
};
//正常收费子类
class CashNormal:public CashSuper
{double acceptCash(double money){return money;}
};
//打折收费子类
class CashRebate:public CashSuper
{private:double moneyRebate;public:CashRebate(double m){this->moneyRebate = m;}double acceptCash(double money){return money * moneyRebate;}
};
//返利收费子类
class CashReturn:public CashSuper
{private:double moneyCondition;double moneyReturn;public:CashReturn(double moneyCondition, double moneyReturn){this->moneyCondition = moneyCondition;this->moneyReturn = moneyReturn;}double acceptCash(double money){double result = money;if (money >= moneyCondition){result = money - money / moneyCondition * moneyReturn;}return result;}
};
//现金收费工厂类
class CashFactory
{public:CashSuper* createCash(string type){CashSuper *cs=new CashSuper();if ("正常收费"==type){printf("正常收费:");cs=new CashNormal();}else if ("满300返100"==type){printf("满300返100:");cs = new CashReturn(300, 100);}else if ("打8折"==type){printf("打8折:");cs = new CashRebate(0.8);}return cs;}
};double total=0;
void consume(string type, int num, double price)
{ CashFactory *CF;CashSuper *csuper=CF->createCash(type); double totalPrices = 0; totalPrices = csuper->acceptCash(num*price); total+=totalPrices; printf("单价: %lf,数量: %d,合计%lf:n",price,num,totalPrices);
}
//客户端代码
int main()
{consume("正常收费", 1, 1000);consume("满300返100", 1, 1000);consume("打8折", 1, 1000);printf("总计:%lfn",total);string type="正常收费";
}
//Strategy类,定义所有支持的算法的公共接口
class Strategy
{ public:virtual void algorithmInterface(){}
};
//ConcreteStrategy封装了具体的算法或行为,继承于Strategy
class ConcreteStrategyA:public Strategy
{ public:void algorithmInterface() { printf("算法A实现n"); }
};
class ConcreteStrategyB:public Strategy
{ public:void algorithmInterface() { printf("算法A实现n"); }
};
class ConcreteStrategyC:public Strategy
{ public:void algorithmInterface() { printf("算法C实现n"); }
};
//Context用一个ConcreteStrategy来配置,维护一个对Strategy对象的引用
class Context
{ private:Strategy *strategy=new Strategy(); public:void Context(Strategy strategy) { this.strategy = strategy; } void contextInterface() { strategy.algorithmInterface(); }
};
//客户端代码
int main()
{ Context *context; context = new Context(new ConcreteStrategyA()); context->contextInterface(); context = new Context(new ConcreteStrategyB()); context->contextInterface(); context = new Context(new ConcreteStrategyC()); context->contextInterface(); }
//CashContext类
class CashContext
{ CashSuper *cashSuper; public:CashContext(CashSuper cashSuper) { this.cashSuper = cashSuper; } double acceptCash(double money) { return cashSuper.acceptCash(money); }
}; void consume(String type, int num, double price)
{ CashContext cashContext = null; if ("正常收费"==type) { cashContext = new CashContext(new CashNormal()); } else if ("满300返100".equals(type)) { cashContext = new CashContext(new CashReturn(300, 100)); } else if ("打8折".equals(type)) { cashContext = new CashContext(new CashRebate(0.8)); } double totalPrices = cashContext.acceptCash(num * price); total += totalPrices; printf("单价: %lf,数量: %d,合计%lf:n",price,num,totalPrices);
}
//客户端代码
double total = 0;
int main()
{ consume("正常收费", 1, 1000); consume("满300返100", 1, 1000); consume("打8折", 1, 1000); printf("总计:%dn",total);
}
//改造后的CashContext
class CashContext
{ CashSuper *cashSuper; public:CashContext(CashSuper cashSuper) { this.cashSuper = cashSuper; } CashContext(String type) { if ("正常收费"==type) { cashSuper = new CashNormal(); } else if ("满300返100"==type) { cashSuper = new CashReturn(300, 100); } else if ("打8折"==type) { cashSuper = new CashRebate(0.8); } } double acceptCash(double money) { return cashSuper.acceptCash(money); }
} void consume(String type, int num, double price)
{ CashContext cashContext = new CashContext(type); double totalPrices = cashContext.acceptCash(num * price); total += totalPrices; printf("单价: %lf,数量: %d,合计%lf:n",price,num,totalPrices);
}
//客户端代码
double total = 0;
int main()
{ consume("正常收费", 1, 1000); consume("满300返100", 1, 1000); consume("打8折", 1, 1000); printf("总计:%dn",total);
}
“嗯,原来简单工厂模式并非只有建一个工厂类的做法,还可以这样子做。此时比刚才的模仿策略模式的写法要清楚多了,客户端代码简单明了。”
//简单工厂模式的用法
CashSuper csuper = ateCash(type);
...
totalPrices = csuper.acceptCash(num * price); //策略模式与简单工厂模式结合的用法
CashContext cashContext = new CashContext(type);
double totalPrices = cashContext.acceptCash(num * price);
“你的意思是,简单工厂模式我需要让客户端认识两个类,CashSuper和CashFactory,而策略模式与简单工厂模式结合的用法,客户端就只需要认识一个类CashContext。耦合度更加降低。”
本文发布于:2024-02-02 04:55:08,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170682091041490.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |