博客
关于我
设计模式学习-策略模式
阅读量:339 次
发布时间:2019-03-04

本文共 4643 字,大约阅读时间需要 15 分钟。

商场促销打折场景下,使用策略模式实现计算最终支付金额

收费基类:

package com.zawl.designpattern.strategy;import java.math.BigDecimal;/** * @Description 收费基类 * @Author xiayunan 
* @Version V1.0.0 * @Since 1.0 * @Date 2019/11/16 7:05 */public interface CashSuper { public BigDecimal acceptCash(BigDecimal money);}

正常收费类继承收费基类:

package com.zawl.designpattern.strategy;import java.math.BigDecimal;/** * @Description 正常收费类 * @Author xiayunan 
* @Version V1.0.0 * @Since 1.0 * @Date 2019/11/16 7:06 */public class CashNormal implements CashSuper { @Override public BigDecimal acceptCash(BigDecimal money) { return money; }}

折扣收费类实现收费基类

package com.zawl.designpattern.strategy;import java.math.BigDecimal;/** * @Description 打折收费类 * @Author xiayunan 
* @Version V1.0.0 * @Since 1.0 * @Date 2019/11/16 7:07 */public class CashDiscount implements CashSuper { private BigDecimal discountRate = new BigDecimal(1); /** * 折扣率,如打八折,就输入0.8 * @param discountRate */ public CashDiscount(BigDecimal discountRate){ this.discountRate = discountRate; } @Override public BigDecimal acceptCash(BigDecimal money) { return money.multiply(discountRate).setScale(2,BigDecimal.ROUND_HALF_UP); }}

返现收费类实现收费基类:

package com.zawl.designpattern.strategy;import java.math.BigDecimal;/** * @Description 返现收费类 * @Author xiayunan 
* @Version V1.0.0 * @Since 1.0 * @Date 2019/11/16 7:08 */public class CashReturn implements CashSuper{ private BigDecimal moneyCondition = BigDecimal.ZERO; private BigDecimal moneyReturn = BigDecimal.ZERO; /** * 返利条件,如满300返100则moneyCondition为300,moneyReturn为100 * @param moneyCondition * @param moneyReturn */ public CashReturn(double moneyCondition, double moneyReturn) { this.moneyCondition = new BigDecimal(moneyCondition); this.moneyReturn = new BigDecimal(moneyReturn); } @Override public BigDecimal acceptCash(BigDecimal money) { //如果消费金额大于返利条件, if(money.compareTo(moneyCondition)>=0){ money = money.subtract(moneyReturn); } return money; }}

收费上下文对象:

package com.zawl.designpattern.strategy;import java.math.BigDecimal;/** * @Description 收费上下文对象 -- 策略模式 * @Author xiayunan 
* @Version V1.0.0 * @Since 1.0 * @Date 2019/11/16 7:29 */public class CashContext { private CashSuper cashSuper; /** * 简单工厂模式创建对象 * @param type */ public CashContext(String type) { switch (type){ case CashConstants.NORMAL: cashSuper = new CashNormal(); break; case CashConstants.DISCOUNT: cashSuper = new CashDiscount(new BigDecimal(0.8)); break; case CashConstants.RETURN: cashSuper = new CashReturn(300,100); break; } } public BigDecimal getResult(double money){ return cashSuper.acceptCash(new BigDecimal(money)); } public enum CashEnum { NORMAL("正常收费","1"), DISCOUNT("打折收费","2"), RETURN("返现收费","3"); CashEnum(String key,String value) { this.key = key; this.value = value; } public String key; public String value; public String getKey() { return key; } public void setKey(String key) { this.key = key; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } }}

收费类型常量类;

package com.zawl.designpattern.strategy;/** * @Description 收费类型常量类 * @Author xiayunan 
* @Version V1.0.0 * @Since 1.0 * @Date 2019/11/16 7:57 */public class CashConstants { /**正常收费*/ public static final String NORMAL = "NORMAL"; /**打折收费*/ public static final String DISCOUNT = "DISCOUNT"; /**返现收费*/ public static final String RETURN = "RETURN";}

客户端调用:

package com.zawl.designpattern.strategy;import java.math.BigDecimal;/** * @Description 客户端 * @Author xiayunan 
* @Version V1.0.0 * @Since 1.0 * @Date 2019/11/16 7:09 */public class Client { public static void main(String[] args) { CashContext context = new CashContext(CashConstants.RETURN); BigDecimal result = context.getResult(300); System.out.println("最终收费:"+result.toString()); context = new CashContext(CashConstants.NORMAL); result = context.getResult(300); System.out.println("最终收费:"+result.toString()); context = new CashContext(CashConstants.DISCOUNT); result = context.getResult(300); System.out.println("最终收费:"+result.toString()); }}

运行结果:

转载地址:http://ojne.baihongyu.com/

你可能感兴趣的文章
Nginx之二:nginx.conf简单配置(参数详解)
查看>>
Nginx从入门到精通
查看>>
Nginx代理websocket配置(解决websocket异常断开连接tcp连接不断问题)
查看>>
Nginx代理初探
查看>>
nginx代理地图服务--离线部署地图服务(地图数据篇.4)
查看>>
Nginx代理外网映射
查看>>
Nginx代理模式下 log-format 获取客户端真实IP
查看>>
Nginx代理解决跨域问题(导致图片只能预览不能下载)
查看>>
Nginx代理访问提示ERR_CONTENT_LENGTH_MISMATCH
查看>>
Nginx代理配置详解
查看>>
Nginx代理静态资源(gis瓦片图片)实现非固定ip的url适配网络环境映射ip下的资源请求解决方案
查看>>
Nginx代理静态资源(gis瓦片图片)实现非固定ip的url适配网络环境映射ip下的资源请求解决方案
查看>>
nginx优化日志拒绝特定404请求写入
查看>>
Nginx优化解析
查看>>
Nginx使用proxy_cache指令设置反向代理缓存静态资源
查看>>
Nginx做反向代理时访问端口被自动去除
查看>>
Nginx入门教程-简介、安装、反向代理、负载均衡、动静分离使用实例
查看>>
Nginx入门简介和反向代理、负载均衡、动静分离理解
查看>>
nginx入门篇----nginx服务器基础配置
查看>>
nginx反向代理
查看>>