• 策略模式【算法选择】


    策略模式

    引入问题

    商场收银系统:根据物品单价和数量,同时可能还有打折优惠或满减活动,开发收银系统。

    【普通代码】

    image-20220623110412942

    说明:图片来源于大话设计模式,仅供学习使用。

    可以看到完全是硬编码,不灵活,可以采用上章学到的工厂模式解耦。

    工厂模式代码

    image-20220623110635851

    简单工厂模式虽然也能解决这个问题,但这个模式只是解决对象的创建问题,而且由于工厂本身包括了所有的收费方式,商场是可能经常性地更改打折额度和返利额度,每次维护或扩展收费方式都要改动这个工厂,以致代码需重新编译部署,这真的是很糟糕的处理方式,所以用它不是最好的办法。面对算法的时常变动,应该有更好的办法。

    策略模式定义了算法家族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化,不会影响到使用算法的客户。看来商场收银系统应该考虑用策略模式

    (摘自大话数据结构)

    策略模式结构代码

    结构大概三部分:

    第一部分:需要有个抽象父类,定义算法,然后不同的算法是不同的子类。

    第二部分:Context类,构造方法参数为算法的抽象父类,一个执行方法,调用算法抽象父类的方法,供客户端调用执行。

    第三部分:客户端代码,在Context的构造方法中,传入不同子类的实现方式,从而调用不同的算法或实现逻辑。

    image-20220623112245610

    商城系统-策略模式实现

    只需要加一个Context类,然后再客户端根据不同的优惠里调用Context的方法,构造时传入不同的优惠实现子类。

    image-20220623112814825

    但是!!这样客户端还需要去判断算法,虽然使用了策略模式,但是还是可以继续优化代码。

    我们是不是可以结合简单工厂,把算法的判断逻辑,从客户端拿走,改成让工厂决定执行哪个算法。

    image-20220623113810327

    上面这种策略模式,已经挺好的了,但是还可以耦合性降低,在客户端不需要选择优惠类,结合工厂模式修改下Context代码即可

    【策略+工厂】

    CashStrategy

    package pers.keafmd.accumulate.designmode.policymode;
    
    /**
     * Keafmd
     *
     * @ClassName: CashStrategy
     * @Description: 收银策略抽象类
     * @author: 牛哄哄的柯南
     * @date: 2022-06-24 19:50
     */
    public abstract class CashStrategy {
        public abstract double settlement(double money);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    DiscountCashStrategy

    package pers.keafmd.accumulate.designmode.policymode;
    
    /**
     * Keafmd
     *
     * @ClassName: DiscountCashStrategy
     * @Description: 打折策略
     * @author: 牛哄哄的柯南
     * @date: 2022-06-24 19:57
     */
    public class DiscountCashStrategy extends CashStrategy{
    
        private double discount = 1d;
    
        public DiscountCashStrategy(double discount) {
            this.discount = discount;
        }
    
        @Override
        public double settlement(double money) {
            return money * discount;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    FullReductionCashStrategy

    package pers.keafmd.accumulate.designmode.policymode;
    
    /**
     * Keafmd
     *
     * @ClassName: FullReductionCashStrategy
     * @Description: 满减策略
     * @author: 牛哄哄的柯南
     * @date: 2022-06-24 19:59
     */
    public class FullReductionCashStrategy extends CashStrategy{
    
        private double fullDeduction = 0.0d;
        private double moneyReturn = 0.0d;
    
        public FullReductionCashStrategy(double fullDeduction, double moneyReturn) {
            this.fullDeduction = fullDeduction;
            this.moneyReturn = moneyReturn;
        }
    
        @Override
        public double settlement(double money) {
            double res = money;
            if(money >=fullDeduction){
                res = money - (int)Math.floor(money/fullDeduction) * moneyReturn;
            }
            return res;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    OriginalPriceCashStrategy

    package pers.keafmd.accumulate.designmode.policymode;
    
    /**
     * Keafmd
     *
     * @ClassName: OriginalPriceCashStrategy
     * @Description: 原价策略
     * @author: 牛哄哄的柯南
     * @date: 2022-06-24 19:56
     */
    public class OriginalPriceCashStrategy extends CashStrategy{
        @Override
        public double settlement(double money) {
            return money;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    CashContext

    package pers.keafmd.accumulate.designmode.policymode;
    
    /**
     * Keafmd
     *
     * @ClassName: CashContext
     * @Description: 收银上下文
     * @author: 牛哄哄的柯南
     * @date: 2022-06-24 20:05
     */
    public class CashContext {
        CashStrategy cashStrategy;
    
        public void selectPreferential(String strategy){
            switch (strategy){
                case "原价":
                    cashStrategy = new OriginalPriceCashStrategy();
                    break;
                case "满300减50":
                    cashStrategy = new FullReductionCashStrategy(300,50);
                    break;
                case "打八折":
                    cashStrategy = new DiscountCashStrategy(0.8);
                    break;
                default:
                    throw new RuntimeException("优惠不合法!");
            }
        }
    
        public double getPayFee(double money){
            return cashStrategy.settlement(money);
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    SupermarketClient

    package pers.keafmd.accumulate.designmode.policymode;
    
    import java.util.Scanner;
    
    /**
     * Keafmd
     *
     * @ClassName: SupermarketClient
     * @Description: 超市客户端
     * @author: 牛哄哄的柯南
     * @date: 2022-06-24 20:15
     */
    public class SupermarketClient {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            double lumpSum = 400d;
            System.out.println("所有商品优惠前总费用:"+ lumpSum);
            System.out.println("请选择优惠:原价,满300减50,打八折");
            String preferential = sc.next();
            CashContext cc = new CashContext();
            cc.selectPreferential(preferential);
    
            System.out.println("优惠后总费用:" + cc.getPayFee(lumpSum));
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    效果:

    所有商品优惠前总费用:400.0
    请选择优惠:原价,30050,打八折
    打八折
    优惠后总费用:320.0
    
    Process finished with exit code 0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    以上就是策略模式【算法选择】的全部内容

    版权声明:
    原创博主:牛哄哄的柯南
    博主原文链接:https://keafmd.blog.csdn.net/
    个人博客链接:https://www.keafmd.top/

    看完如果对你有帮助,感谢点击下面的点赞支持!
    [哈哈][抱拳]

    在这里插入图片描述
    加油!

    共同努力!

    Keafmd

  • 相关阅读:
    记GitLab服务器迁移后SSH访问无法生效的问题解决过程
    .NET Core(.NET6)中gRPC注册到Consul
    AVL树的插入+层次遍历+判断是否为完全二叉树板子
    基于JAVA校园二手交易平台计算机毕业设计源码+系统+mysql数据库+lw文档+部署
    【Python】逻辑回归变量的显著性分析
    【linux】Linux 查看内存使用情况的几种方法汇总
    Day36力扣打卡
    CHAPTER 4: DESIGN A RATE LIMITER
    静态住宅代理是什么?为什么要选择它?
    Java中的UDP通信(网络编程 一)
  • 原文地址:https://blog.csdn.net/weixin_43883917/article/details/125445180