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

你可能感兴趣的文章
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named 'pandads'
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>