23种设计模式_MODE13策略模式_手写代码实现

在这里插入图片描述

1.策略模式测试

package com.zhaoshuangjian.mode13_策略模式;

import com.zhaoshuangjian.mode13_策略模式.mode13.公式计算.ICalculator;
import com.zhaoshuangjian.mode13_策略模式.mode13.公式计算.Mul;
import com.zhaoshuangjian.mode13_策略模式.mode13.公式计算.Plus;
import com.zhaoshuangjian.mode13_策略模式.mode13.公式计算.Sub;
import com.zhaoshuangjian.mode13_策略模式.mode13.商场打折.DianShang618;
import com.zhaoshuangjian.mode13_策略模式.mode13.商场打折.Man300Jian100;
import com.zhaoshuangjian.mode13_策略模式.mode13.商场打折.PriceContext;
import com.zhaoshuangjian.mode13_策略模式.mode13.商场打折.Shuang11;

/**
 * <p>策略模式测试</p>
 *
 * @Author zhaoshuangjian  2023-03-25 下午21:44
 */
public class StrategyTest {

    public static void main(String[] args) {

        // 1、 策略模式☞运算公式结果计算
        plus();
        sub();
        mul();

        // 2、 策略模式☞商场打折
        man300jian100();
        dianShang618();
        shuang11();
    }

    private static void plus(){
        System.out.println("===============2+ 8 乘法运算");
        ICalculator calculator = new Plus();
        double calculate = calculator.calculate("2+ 8");
        System.out.println("2+ 8 = "+calculate);
    }

    private static void sub(){
        System.out.println("===============2 - 8 减法运算");
        ICalculator calculator = new Sub();
        double calculate = calculator.calculate("2 - 8");
        System.out.println("2 - 8 = "+calculate);
    }

    private static void mul(){
        System.out.println("===============2*8 乘法运算");
        ICalculator calculator = new Mul();
        double calculate = calculator.calculate("2*8");
        System.out.println("2*8 = "+calculate);
    }

    private static void man300jian100(){

        System.out.println("===============全场满300减100!");
        double totalPrice = 450.0;
        System.out.println("商品原价:"+totalPrice+"¥");
        PriceContext price = new PriceContext(new Man300Jian100());
        System.out.println("商品折后的价钱:"+price.discount(totalPrice)+"¥");

    }

    private static void dianShang618(){

        System.out.println("===============电商节6.18欢快购!");
        double totalPrice = 700.0;
        System.out.println("商品原价:"+totalPrice+"¥");
        PriceContext price = new PriceContext(new DianShang618());
        System.out.println("商品折后的价钱:"+price.discount(totalPrice)+"¥");

        totalPrice = 1024.0;
        System.out.println("商品原价:"+totalPrice+"¥");
        System.out.println("商品折后的价钱:"+price.discount(totalPrice)+"¥");
    }

    private static void shuang11(){

        System.out.println("===============双11全场嗨翻购!");
        double totalPrice = 623.0;
        System.out.println("商品原价:"+totalPrice+"¥");
        PriceContext price = new PriceContext(new Shuang11());
        System.out.println("商品折后的价钱:"+price.discount(totalPrice)+"¥");

        totalPrice = 1500.0;
        System.out.println("商品原价:"+totalPrice+"¥");
        System.out.println("商品折后的价钱:"+price.discount(totalPrice)+"¥");
    }


    /**
     *
     * 百科上如此介绍策略模式:
     * 策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换。
     * 策略模式让算法独立于使用它的客户而独立变化。
     *
     * 通过上述两个算法计算的例子,我们可以领略到策略模式的好处:
     * (1)用户或算法使用者可以选择不同的算法类来实现自己业务算法的需要
     * (2)开发者扩展新的算法类,只需要新建一个类实现calculate方法即可,扩展相当方便
     * (4)总结就是,遵守了"开闭原则",对扩展开放,对修改关闭!
     * 缺点:
     * (1)如果基于选择的策略模式很多的话,这就意味着子类有很多,维护起来增加复杂性
     * (2)对于使用者来说,他必须知道全部的策略类,才可以做到策略的任意切换
     */

}

2.计算辅助类,主要提取公式中的数数组

package com.zhaoshuangjian.mode13_策略模式.mode13.公式计算;

/**
 * <p>计算辅助类,主要提取公式中的数数组</p>
 *
 * @Author zhaoshuangjian  2023-03-25 下午21:44
 */
public class CalculatorHelper {

    public static double[] getValArray(String formula , String splitChar){

        //记得消除空格
        String array[] = formula.trim().split(splitChar);
        double arrayDouble[] = new double[2];
        arrayDouble[0] = Double.parseDouble(array[0]);
        arrayDouble[1] = Double.parseDouble(array[1]);
        return arrayDouble;

    }

}

3.定义计算接口,提供根据公式(字符串)计算值的方法

package com.zhaoshuangjian.mode13_策略模式.mode13.公式计算;

/**
 * <p>定义计算接口,提供根据公式(字符串)计算值的方法</p>
 *
 * @Author zhaoshuangjian  2023-03-25 下午21:44
 */
public interface ICalculator {

    /**
     * <p>根据公式字符串,计算得出值</p>
     * @param formula 公式
     * @return 值
     */
    double calculate(String formula);

}

4.乘法运算实现乘法公式的结果计算

package com.zhaoshuangjian.mode13_策略模式.mode13.公式计算;

/**
 * <p>乘法运算实现乘法公式的结果计算</p>
 *
 * @Author zhaoshuangjian  2023-03-25 下午21:44
 */
public class Mul implements ICalculator{

    @Override
    public double calculate(String formula) {
        double[] valArray = CalculatorHelper.getValArray(formula, "\\*");
        return valArray[0] * valArray[1];
    }

}

5.加法运算实现加法公式的结果计算

package com.zhaoshuangjian.mode13_策略模式.mode13.公式计算;

/**
 * <p>加法运算实现加法公式的结果计算</p>
 *
 * @Author zhaoshuangjian  2023-03-25 下午21:44
 */
public class Plus implements ICalculator{

    @Override
    public double calculate(String formula) {
        double[] valArray = CalculatorHelper.getValArray(formula, "\\+");
        return valArray[0] + valArray[1];
    }

}

6.减法运算实现减法公式的结果计算

package com.zhaoshuangjian.mode13_策略模式.mode13.公式计算;

/**
 * <p>减法运算实现减法公式的结果计算</p>
 *
 * @Author zhaoshuangjian  2023-03-25 下午21:44
 */
public class Sub implements ICalculator{

    @Override
    public double calculate(String formula) {
        double[] valArray = CalculatorHelper.getValArray(formula, "-");
        return valArray[0] - valArray[1];
    }

}

7.电商节 - 6.18 – 满618元减99,满1000元打8.5折

package com.zhaoshuangjian.mode13_策略模式.mode13.商场打折;

/**
 * <p>电商节 - 6.18 -- 满618元减99,满1000元打8.5折</p>
 *
 * @Author zhaoshuangjian  2023-03-25 下午21:44
 */
public class DianShang618 implements ICalculatePrice{

    @Override
    public double getDiscountedPrice(double totalPrice) {

        if(totalPrice >= 618 ){
            return totalPrice - 99 ;
        }

        if(totalPrice >= 1000){
            return totalPrice * 0.85;
        }

        return totalPrice;
    }

}

8.定义价格计算策略接口,为每一个策略类提供统一的计算折扣价钱的方法

package com.zhaoshuangjian.mode13_策略模式.mode13.商场打折;

/**
 * <p>定义价格计算策略接口,为每一个策略类提供统一的计算折扣价钱的方法</p>
 *
 * @Author zhaoshuangjian  2023-03-25 下午21:44
 */
public interface ICalculatePrice {

    /**
     * <p>根据商品的实际总价计算得到商品折后的价钱</p>
     * @param totalPrice 实际商品总价
     * @return 折后价
     */
    double getDiscountedPrice(double totalPrice);

}

9.商品满减活动 – 满300元减100元

package com.zhaoshuangjian.mode13_策略模式.mode13.商场打折;

/**
 * <p>商品满减活动 -- 满300元减100元</p>
 *
 * @Author zhaoshuangjian  2023-03-25 下午21:44
 */
public class Man300Jian100 implements ICalculatePrice{

    @Override
    public double getDiscountedPrice(double totalPrice) {

        // 全场消费满300元,减100元
        if(totalPrice >= 300){
            return totalPrice - 100 ;
        }
        return totalPrice;
    }

}

10.价格上下文类

package com.zhaoshuangjian.mode13_策略模式.mode13.商场打折;

/**
 * <p>价格上下文类</p>
 * <p>对用户暴露计算折后价钱的方法,由用户选择使用具体的打折策略类来计算最终的商品价钱</p>
 *
 * @Author zhaoshuangjian  2023-03-25 下午21:44
 */
public class PriceContext {

    private ICalculatePrice price;

    public PriceContext(ICalculatePrice price) {
        this.price = price;
    }

    public double discount(double totalPrice){
        double discountedPrice = price.getDiscountedPrice(totalPrice);
        if(0.0 == discountedPrice){
            System.out.println("恭喜您,您本次消费免单!");
        }
        return discountedPrice;
    }

}

11.天猫双11购物狂欢节 – 满500减少166,满1000元随机打折,有可能免单哦

package com.zhaoshuangjian.mode13_策略模式.mode13.商场打折;

import java.util.Random;

/**
 * <p>天猫双11购物狂欢节 -- 满500减少166,满1000元随机打折,有可能免单哦</p>
 *
 * @Author zhaoshuangjian  2023-03-25 下午21:44
 */
public class Shuang11 implements ICalculatePrice{

    // 分别是免单、6.5折、7.5折、8.5折和9折
    private static double[] discount = new double[]{0.0,0.65,0.75,0.85,0.90};

    @Override
    public double getDiscountedPrice(double totalPrice) {

        if(totalPrice >= 500 && totalPrice < 1000){
            return totalPrice - 166 ;
        }

        if(totalPrice >= 1000){
            Random random = new Random();
            return totalPrice * (discount[random.nextInt(5)]);
        }
        return 0;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhaoshuangjian

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值