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

你可能感兴趣的文章
Mysql8.0以上重置初始密码的方法
查看>>
mysql8.0新特性-自增变量的持久化
查看>>
Mysql8.0注意url变更写法
查看>>
Mysql8.0的特性
查看>>
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
MySQL8修改密码的方法
查看>>
Mysql8在Centos上安装后忘记root密码如何重新设置
查看>>
Mysql8在Windows上离线安装时忘记root密码
查看>>
MySQL8找不到my.ini配置文件以及报sql_mode=only_full_group_by解决方案
查看>>
mysql8的安装与卸载
查看>>
MySQL8,体验不一样的安装方式!
查看>>
MySQL: Host '127.0.0.1' is not allowed to connect to this MySQL server
查看>>
Mysql: 对换(替换)两条记录的同一个字段值
查看>>
mysql:Can‘t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock‘解决方法
查看>>
MYSQL:基础——3N范式的表结构设计
查看>>
MYSQL:基础——触发器
查看>>
Mysql:连接报错“closing inbound before receiving peer‘s close_notify”
查看>>
mysqlbinlog报错unknown variable ‘default-character-set=utf8mb4‘
查看>>
mysqldump 参数--lock-tables浅析
查看>>
mysqldump 导出中文乱码
查看>>