设计模式--策略模式
来源:互联网 发布:数控剪板机编程 编辑:程序博客网 时间:2024/05/18 03:31
在面向对象程序设计过程中,我们常常说道利用多态这个特性,我觉得策略模式是最最原始的利用多态的一个设计模式了。
拿一个现实中的例子来澄清整个过程吧,比如两个人A,B同时去沃尔玛买一袋米,对于这袋米的销售价格,沃尔玛方面是这样规定的:
- 普通用户:原价100元
会员用户:原价基础上打八折
所以针对不同的角色我们需要套用不同的价格算法。这时候我们引入策略模式就恰如其分了。
首先我们定义一个大米类:
class Rice{ private double price; private Discount discount; public void setPrice(double price) { this.price = price; } public void setDiscount(Discount discount) { this.discount = discount; } public double getPrice() { return discount.getfinalPrice(price); }}
然后我们的定义了一个策略接口叫做Discount
interface Discount{ public double getfinalPrice(double price);}
然后在实现两个策略,分别对应普通客户和VIP客户。
class NormalCusDiscount implements Discount{ @Override public double getfinalPrice(double price) { return price; }}class VIPCusDiscount implements Discount{ @Override public double getfinalPrice(double price) { return price *0.8; }}
然后我们在客户端调用的时候就可以对大米对象注入不同的策略对象。大米对象执行相同的逻辑将得到不同的结果。
public static void main(String[] args) { Rice rice = new Rice(); rice.setPrice(100.0f); Discount discount = new VIPCusDiscount(); rice.setDiscount(discount); System.out.println("the final price for VIP is: " +rice.getPrice()); rice.setDiscount(new NormalCusDiscount()); System.out.println("the final price for Normal cus is: " +rice.getPrice());}
输出结果:
the final price for VIP is: 80.0the final price for Normal cus is: 100.0
可以看到在Rice的getPrice中我们就是利用多态这个特性针对接口编程。利用不同的策略算出最后的价格的。
按住惯例我们再用C++语言和C语言来实现一遍吧。先看C++的实现吧。
class Discount{public: virtual double getFinalPrice(double price) = 0; virtual ~Discount(){}};class Rice{public: void setPrice(double price){ this->price = price; } void setDiscount(Discount *discount){ this->discount = discount; } double getPrice(){ return discount->getFinalPrice(price); }private: double price; Discount* discount;};class NormalCusDiscount : public Discount{ double getFinalPrice(double price){ return price *1.0; }};class VIPCusDiscount :public Discount{ double getFinalPrice(double price){ return price *0.8; }};int main(int argc, const char * argv[]) { Rice * rice = new Rice(); rice->setPrice(100.0f); rice->setDiscount(new NormalCusDiscount()); cout << "the price for normal Cus is :" << rice->getPrice()<< endl; rice->setDiscount(new VIPCusDiscount()); cout << "the price for VIP Cus is :" << rice->getPrice()<< endl; return 0;}
现在我们再来看一看C语言的实现吧
typedef struct _Discount{ double (*getFinalPrice)(double price);}Discount;double getFinalPrice_Normal(double price){ return price;}double getFinalPrice_VIP(double price){ return price * 0.8;}Discount * newNormalCusDiscount(){ Discount * discount = (Discount *)malloc(sizeof(Discount)); discount->getFinalPrice =getFinalPrice_Normal; return discount;}Discount *newVIPCusDiscount(){ Discount *discount = (Discount*)malloc(sizeof(Discount)); discount->getFinalPrice =getFinalPrice_VIP; return discount;}typedef struct _Rice{ double price; Discount *discount; void (*setPrice)(struct _Rice * rice,double price); void (*setDiscount)(struct _Rice * rice,Discount * discount); double (*getPrice)(struct _Rice * rice);}Rice;void setPrice(struct _Rice * rice,double price){ if(rice == NULL) return; rice->price = price;}void setDiscount(struct _Rice * rice,Discount * discount){ if(rice == NULL || discount == NULL) return; rice->discount = discount;}double getPrice(struct _Rice * rice){ return rice->discount->getFinalPrice(rice->price);}Rice * newRice(){ Rice * rice = (Rice *)malloc(sizeof(Rice)); if (rice == NULL){ return NULL; } memset(rice, 0, sizeof(Rice)); rice->setPrice = setPrice; rice->setDiscount =setDiscount; rice->getPrice =getPrice; return rice;}int main(int argc, char** argv){ Rice * rice = newRice(); rice->setPrice(rice, 100.0f); rice->setDiscount(rice, newNormalCusDiscount()); printf("the final price for Normal Cus is %.2f\n", rice->getPrice(rice)); rice->setDiscount(rice,newVIPCusDiscount()); printf("the final price for VIP Cus is %.2f\n", rice->getPrice(rice)); return 0;}
以上就是用Java,C++,C语言实现的策略模式了。可以说策略模式使用频率是相当高的。
阅读全文
0 0
- 设计模式-策略模式
- 设计模式:策略模式
- 设计模式-策略模式
- 设计模式 - 策略模式
- 设计模式-策略模式
- 设计模式-----策略模式
- 设计模式 策略模式
- 设计模式-策略模式
- 设计模式-【策略模式】
- 设计模式-----策略模式
- 设计模式-策略模式
- 设计模式--策略模式
- 设计模式- 策略模式
- 设计模式- 策略模式
- 设计模式------策略模式
- 设计模式--策略模式
- 设计模式-策略模式
- 设计模式--策略模式
- Android Switch 按钮和滑动轨迹图片设置
- MATLAB 2012a 许可证过期,导致打开软件不断地提示mathworks激活解决办法
- Spring_定时器配置
- PHP session 跨子域问题总结
- BZOJ 4627: [BeiJing2016]回转寿司 cdq
- 设计模式--策略模式
- mybatis中各个表签的作用
- 微信小程序 this.setData is not a function
- window7 系统 设置 显示隐藏文件 文件扩展名
- 爬虫实例(中国大学排名前10)
- 【2017.11.16】外包单报价
- ubuntu16.04 安装
- STM32-1-LED点亮与熄灭
- Java判断一个数的取值范围和类型。