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

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

策略模式在商场促销中的应用

在商场促销场景下,使用策略模式可以有效地实现计算最终支付金额的功能。本文将详细介绍相关实现。

收费基类

package com.zawl.designpattern.strategy;public interface CashSuper {    BigDecimal acceptCash(BigDecimal money);}

该接口定义了一个通用收费处理方法,用于计算最终支付金额。

正常收费类

package com.zawl.designpattern.strategy;public class CashNormal implements CashSuper {    @Override    public BigDecimal acceptCash(BigDecimal money) {        return money;    }}

实现了接口中的acceptCash方法,直接返回输入金额,不做任何调整。

折扣收费类

package com.zawl.designpattern.strategy;public class CashDiscount implements CashSuper {    private BigDecimal discountRate = new BigDecimal(1);    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;public class CashReturn implements CashSuper {    private BigDecimal moneyCondition = BigDecimal.ZERO;    private BigDecimal moneyReturn = BigDecimal.ZERO;    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;public class CashContext {    private CashSuper cashSuper;    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));    }    enum CashEnum {        NORMAL("正常收费", "1"),        DISCOUNT("打折收费", "2"),        RETURN("返现收费", "3");        CashEnum(String key, String value) {            this.key = key;            this.value = value;        }        private String key;        private String value;        public String getKey() {            return key;        }        public String getValue() {            return value;        }    }}

该类用于根据不同的收费策略选择合适的处理逻辑,提供计算最终支付金额的功能。

收费类型常量类

package com.zawl.designpattern.strategy;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;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/

你可能感兴趣的文章
Openlayers高级交互(1/20): 控制功能综合展示(版权、坐标显示、放缩、比例尺、测量等)
查看>>
Openlayers高级交互(10/20):绘制矩形,截取对应部分的地图并保存
查看>>
Openlayers高级交互(11/20):显示带箭头的线段轨迹,箭头居中
查看>>
Openlayers高级交互(12/20):利用高德逆地理编码,点击位置,显示坐标和地址
查看>>
Openlayers高级交互(13/20):选择左右两部分的地图内容,横向卷帘
查看>>
Openlayers高级交互(14/20):汽车移动轨迹动画(开始、暂停、结束)
查看>>
Openlayers高级交互(15/20):显示海量多边形,10ms加载完成
查看>>
Openlayers高级交互(16/20):两个多边形的交集、差集、并集处理
查看>>
Openlayers高级交互(17/20):通过坐标显示多边形,计算出最大幅宽
查看>>
Openlayers高级交互(18/20):根据feature,将图形适配到最可视化窗口
查看>>
Openlayers高级交互(19/20): 地图上点击某处,列表中显示对应位置
查看>>
Openlayers高级交互(2/20):清除所有图层的有效方法
查看>>
Openlayers高级交互(20/20):超级数据聚合,页面不再混乱
查看>>
Openlayers高级交互(3/20):动态添加 layer 到 layerGroup,并动态删除
查看>>
Openlayers高级交互(4/20):手绘多边形,导出KML文件,可以自定义name和style
查看>>
Openlayers高级交互(5/20):右键点击,获取该点下多个图层的feature信息
查看>>
Openlayers高级交互(6/20):绘制某点,判断它是否在一个电子围栏内
查看>>
Openlayers高级交互(7/20):点击某点弹出窗口,自动播放视频
查看>>
Openlayers高级交互(8/20):选取feature,平移feature
查看>>