策略模式结合简单工厂模式

Posted grocivey

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了策略模式结合简单工厂模式相关的知识,希望对你有一定的参考价值。

概述

策略模式是一种定义一系列算法的方法,从概念上来看,所有这些算法完成的都是相同的工作,只是实现不同,它可以以相同的方式调用所有的算法,减少各种算法类与使用算法类之间的耦合。

场景

超市收营员收钱,有打几折、满多少返现、正常收费等活动来最终算出客户应该付多少现金。

1.现金收款超类

package com.gzy.designpattern.strategyandfactory;

/*
 * @program: mybatis-plus
 * @description: 现金收款超类
 * @author: gaozhiyuan
 * @create: 2019-02-13 23:23
 */
 interface CashSuper {
      double acceptMoney(double money);
}

2.正常收费

package com.gzy.designpattern.strategyandfactory;

/*
 * @program: mybatis-plus
 * @description: normal accept money
 * @author: gaozhiyuan
 * @create: 2019-02-13 23:30
 */
public class CashNormal implements CashSuper {
    @Override
    public double acceptMoney(double money) {
        return money;
    }
}

3.打折

package com.gzy.designpattern.strategyandfactory;

/*
 * @program: mybatis-plus
 * @description: rebate accept money
 * @author: gaozhiyuan
 * @create: 2019-02-13 23:31
 */
 class CashRebate  implements  CashSuper{
    private double rebate;

    public CashRebate() {
    }

    public CashRebate(double rebate) {
        this.rebate = rebate;
    }

    @Override
    public double acceptMoney(double money) {
        return money*this.rebate;
    }
}

4.满多少返现

package com.gzy.designpattern.strategyandfactory;

/*
 * @program: mybatis-plus
 * @description: 返现
 * @author: gaozhiyuan
 * @create: 2019-02-13 23:34
 */
public class CashReturn implements CashSuper {
    //满足的条件
    private double moneyCondition;
    //返现金额
    private double moneyReturn;

    public CashReturn() {
    }

    public CashReturn(double moneyCondition, double moneyReturn) {
        this.moneyCondition = moneyCondition;
        this.moneyReturn = moneyReturn;
    }

    @Override
    public double acceptMoney(double money) {
        double result = money;
        if(money >= this.moneyCondition){
            result = money - this.moneyReturn;
        }
        return result;
    }
}

5.策略模式结合简单工厂类,可根据传入活动类型写死活动内容,也可通过不同的构造函数确定活动。

package com.gzy.designpattern.strategyandfactory;

/*
 * @program: mybatis-plus
 * @description:
 * @author: gaozhiyuan
 * @create: 2019-02-13 23:40
 */
public class CashContext {
      CashSuper cashSuper;

    public CashContext(String type) {//传入打折或返现类型
        switch (type) {
            case "normal":
                cashSuper = new CashNormal();
                break;
            case "rebate":
                cashSuper = new CashRebate(0.8);
                break;
            case "return":
                cashSuper = new CashReturn(300,100);
                break;
        }
        this.cashSuper = cashSuper;
    }

    public CashContext(){
        cashSuper = new CashNormal();
    }

    public CashContext(double rebate){
        cashSuper = new CashRebate(rebate);
    }

    public CashContext(double moneyCondition,double moneyReturn){
        cashSuper = new CashReturn(moneyCondition,moneyReturn);
    }

    public double acceptMoney(double money){
        return  cashSuper.acceptMoney(money);
    }
}

6,测试类

package com.gzy.designpattern.strategyandfactory;

/*
 * @program: mybatis-plus
 * @description: test
 * @author: gaozhiyuan
 * @create: 2019-02-13 23:56
 */
public class Test {
    public static void main(String[] args) {
        CashContext cashContext = new CashContext(300,100);
    //    CashContext cashContext = new CashContext(0.8);
    //    CashContext cashContext = new CashContext();
        double result = cashContext.acceptMoney(1000); //根据活动算出最终该付多少钱
        System.out.println(result); //
    }
}

测试结果

900.0

Process finished with exit code 0

 

以上是关于策略模式结合简单工厂模式的主要内容,如果未能解决你的问题,请参考以下文章

策略模式和简单工厂模式的结合使用

策略模式结合简单工厂模式

策略模式

浅谈简单工厂模式和策略模式

策略模式与简单工厂模式

2021突破瓶颈:巧用策略模式完美应付产品四次需求变更,代码如何优雅稳健成长