博客
关于我
设计模式学习-策略模式
阅读量: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/

你可能感兴趣的文章
Mysql:避免重复的插入数据方法汇总
查看>>
MyS中的IF
查看>>
M_Map工具箱简介及地理图形绘制
查看>>
m_Orchestrate learning system---二十二、html代码如何变的容易
查看>>
M×N 形状 numpy.ndarray 的滑动窗口
查看>>
m个苹果放入n个盘子问题
查看>>
n = 3 , while n , continue
查看>>
n 叉树后序遍历转换为链表问题的深入探讨
查看>>
N!
查看>>
N-Gram的基本原理
查看>>
n1 c语言程序,全国青少年软件编程等级考试C语言经典程序题10道七
查看>>
Nacos Client常用配置
查看>>
nacos config
查看>>
Nacos Config--服务配置
查看>>
Nacos Derby 远程命令执行漏洞(QVD-2024-26473)
查看>>
Nacos 与 Eureka、Zookeeper 和 Consul 等其他注册中心的区别
查看>>
Nacos 单机集群搭建及常用生产环境配置 | Spring Cloud 3
查看>>
Nacos 启动报错[db-load-error]load jdbc.properties error
查看>>
Nacos 报Statement cancelled due to timeout or client request
查看>>
Nacos 注册服务源码分析
查看>>