博客
关于我
设计模式学习-策略模式
阅读量: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远程连接设置
查看>>
MySql连接出现1251Client does not support authentication protocol requested by server解决方法
查看>>
Mysql连接时报时区错误
查看>>
MySql连接时提示:unknown Mysql server host
查看>>
MySQL连环炮,你扛得住嘛?
查看>>
mysql逗号分隔的字符串如何搜索
查看>>
MySQL通用优化手册
查看>>
Mysql通过data文件恢复
查看>>
MYSQL遇到Deadlock found when trying to get lock,解决方案
查看>>
MYSQL遇到Deadlock found when trying to get lock,解决方案
查看>>
mysql部署错误
查看>>
MySQL配置信息解读(my.cnf)
查看>>
Mysql配置文件my.ini详解
查看>>
MySQL配置文件深度解析:10个关键参数及优化技巧---强烈要求的福利来咯。
查看>>
Mysql配置表名忽略大小写(SpringBoot连接表时提示不存在,实际是存在的)
查看>>
mysql配置读写分离并在若依框架使用读写分离
查看>>
MySQL里为什么会建议不要使用SELECT *?
查看>>
MySQL里的那些日志们
查看>>
mysql重新安装?忘记root密码?重装Windows、Linux系统导致mysql没法用吗? 这里有你想要的答案
查看>>
mysql重置root密码
查看>>