不得不会的23种Java设计模式(七)——策略模式

定义

定义一系列的算法,把每一个算法封装起来, 并且使它们可相互替换。本模式使得算法可独立于使用它的客户而变化。

UML类图结构

在这里插入图片描述

  • 环境类(Context):用一个ConcreteStrategy对象来配置。维护一个对Strategy对象的引用。可定义一个接口来让Strategy访问它的数据
  • 抽象策略类(Strategy):定义所有支持的算法的公共接口。 Context使用这个接口来调用某ConcreteStrategy定义的算法。
  • 具体策略类(ConcreteStrategy):以Strategy接口实现某具体算法。

案例1

package test.strategy01;

public abstract class Strategy {

    //定义算法抽象方法
    public abstract void algorithmInterface();
}

package test.strategy01;

public class ConcreteStrategyA extends Strategy {
    @Override
    public void algorithmInterface() {
        System.out.println("算法A实现");
    }
}

package test.strategy01;

public class ConcreteStrategyB extends Strategy {
    @Override
    public void algorithmInterface() {
        System.out.println("算法B实现");
    }
}

package test.strategy01;

public class ConcreteStrategyC extends Strategy {
    @Override
    public void algorithmInterface() {
        System.out.println("算法C实现");
    }
}

package test.strategy01;

public class Context {
    Strategy strategy;

    public Context(Strategy strategy){
        this.strategy = strategy;
    }

    //上下文接口
    public void contextInterface(){
        strategy.algorithmInterface();
    }
}

package test.strategy01;

public class Test {
    public static void main(String[] args) {
        Context context;

        context = new Context(new ConcreteStrategyA());
        context.contextInterface();

        context = new Context(new ConcreteStrategyB());
        context.contextInterface();

        context = new Context(new ConcreteStrategyC());
        context.contextInterface();
    }
}

在这里插入图片描述

案例2

商店的收银系统,这个商店有普通顾客,会员,超级会员以及金牌会员的区别,针对各个顾客,有不同的打折方式,并且一个顾客每在商店消费1000就增加一个级别,那么我们就可以使用策略模式,因为策略模式描述的就是算法的不同,而且这个算法往往非常繁多,并且可能需要经常性的互相替换。

package test.strategy02;

public interface CalPrice {
    //根据商品原价进行打折计算,返回最终的结算价格
    Double calPrice(Double originalPrice);
}

package test.strategy02;

public class Common implements CalPrice {
    @Override
    public Double calPrice(Double originalPrice) {
        return originalPrice;
    }
}

package test.strategy02;

public class Vip implements CalPrice {
    @Override
    public Double calPrice(Double originalPrice) {
        return originalPrice*0.8;
    }
}

package test.strategy02;

public class SuperVip implements CalPrice {
    @Override
    public Double calPrice(Double originalPrice) {
        return originalPrice * 0.7;
    }
}

package test.strategy02;

public class GoldVip implements CalPrice {
    @Override
    public Double calPrice(Double originalPrice) {
        return originalPrice*0.5;
    }
}

package test.strategy02;

public class Customer {
    private Double totalAmount = 0D;//客户累计消费
    private Double amount = 0D;//客户单次消费金额
    //默认原价
    private CalPrice calPrice = new Common();

    public void buy(Double amount){
        this.amount = amount;
        totalAmount += amount;
        if(totalAmount > 3000){
            calPrice = new GoldVip();
        }else if(totalAmount > 2000){
            calPrice = new SuperVip();
        }else if(totalAmount > 1000){
            calPrice = new Vip();
        }
    }

    public Double calLastAmount() {
        return calPrice.calPrice(amount);
    }
}

package test.strategy02;

public class Test {
    public static void main(String[] args) {
        Customer customer = new Customer();
        customer.buy(500D);
        System.out.println("客户需要付钱"+customer.calLastAmount());

        customer.buy(1200D);
        System.out.println("客户需要付钱"+customer.calLastAmount());

        customer.buy(1200D);
        System.out.println("客户需要付钱"+customer.calLastAmount());

        customer.buy(1200D);
        System.out.println("客户需要付钱"+customer.calLastAmount());
    }
}

在这里插入图片描述

案例3

package test.strategy03;

public interface CalPrice {
    //根据商品原价进行打折计算,返回最终的结算价格
    Double calPrice(Double originalPrice);
}

package test.strategy03;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


@Target(ElementType.TYPE)//表示只能给类添加该注解
@Retention(RetentionPolicy.RUNTIME)//这个必须要将注解保留在运行时
public @interface TotalValidRegion {

    //消费的默认最大值设置很大
    int max() default Integer.MAX_VALUE;

    //消费的默认最小值为0
    int min() default 0;
}

package test.strategy03;

@TotalValidRegion(max = 1000)
public class Common implements CalPrice{
    @Override
    public Double calPrice(Double originalPrice) {
        return originalPrice;
    }
}

package test.strategy03;


@TotalValidRegion(min=1000,max=2000)
public class Vip implements CalPrice {
    @Override
    public Double calPrice(Double originalPrice) {
        return originalPrice*0.8;
    }
}

package test.strategy03;

@TotalValidRegion(min=2000,max=3000)
public class SuperVip implements CalPrice {
    @Override
    public Double calPrice(Double originalPrice) {
        return originalPrice * 0.7;
    }
}

package test.strategy03;


@TotalValidRegion(min=3000)
public class GoldVip implements CalPrice {
    @Override
    public Double calPrice(Double originalPrice) {
        return originalPrice*0.5;
    }
}

package test.strategy03;

public class Customer {
    private Double totalAmount = 0D;//客户在本商店消费的总额
    private Double amount = 0D;//客户单次消费金额
    private CalPrice calPrice = new Common();//每个客户都有一个计算价格的策略,初始都是普通计算,即原价

    //客户购买商品,就会增加它的总额
    public void buy(Double amount){
        this.amount = amount;
        totalAmount += amount;
        /* 变化点,我们将策略的制定转移给了策略工厂,将这部分责任分离出去 */
        calPrice = CalPriceFactory.getInstance().createCalPrice(this);
    }
    //计算客户最终要付的钱
    public Double calLastAmount(){
        return calPrice.calPrice(amount);
    }

    public Double getTotalAmount() {
        return totalAmount;
    }

    public Double getAmount() {
        return amount;
    }
}

package test.strategy03;

import java.io.File;
import java.io.FileFilter;
import java.lang.annotation.Annotation;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

public class CalPriceFactory {
    private static final String CAL_PRICE_PACKAGE = "test.strategy03";//这里是一个常量,表示我们扫描策略的包

    private ClassLoader classLoader = getClass().getClassLoader();//我们加载策略时的类加载器,我们任何类运行时信息必须来自该类加载器

    private List<Class<? extends CalPrice>> calPriceList;//策略列表

    //根据客户的总金额产生相应的策略
    public CalPrice createCalPrice(Customer customer){
        //在策略列表查找策略
        for (Class<? extends CalPrice> clazz : calPriceList) {
            TotalValidRegion validRegion = handleAnnotation(clazz);//获取该策略的注解
            //判断金额是否在注解的区间
            if (customer.getTotalAmount() > validRegion.min() && customer.getTotalAmount() < validRegion.max()) {
                try {
                    //是的话我们返回一个当前策略的实例
                    return clazz.newInstance();
                } catch (Exception e) {
                    throw new RuntimeException("策略获得失败");
                }
            }
        }
        throw new RuntimeException("策略获得失败");
    }

    //处理注解,我们传入一个策略类,返回它的注解
    private TotalValidRegion handleAnnotation(Class<? extends CalPrice> clazz){
        Annotation[] annotations = clazz.getDeclaredAnnotations();
        if (annotations == null || annotations.length == 0) {
            return null;
        }
        for (int i = 0; i < annotations.length; i++) {
            if (annotations[i] instanceof TotalValidRegion) {
                return (TotalValidRegion) annotations[i];
            }
        }
        return null;
    }

    //单例
    private CalPriceFactory(){
        init();
    }

    //在工厂初始化时要初始化策略列表
    private void init(){
        calPriceList = new ArrayList<>();
        File[] resources = getResources();//获取到包下所有的class文件
        Class<CalPrice> calPriceClazz = null;
        try {
            calPriceClazz = (Class<CalPrice>) classLoader.loadClass(CalPrice.class.getName());//使用相同的加载器加载策略接口
        } catch (ClassNotFoundException e1) {
            throw new RuntimeException("未找到策略接口");
        }
        for (int i = 0; i < resources.length; i++) {
            try {
                //载入包下的类
                Class<?> clazz = classLoader.loadClass(CAL_PRICE_PACKAGE + "."+resources[i].getName().replace(".class", ""));
                //判断是否是CalPrice的实现类并且不是CalPrice它本身,满足的话加入到策略列表
                if (CalPrice.class.isAssignableFrom(clazz) && clazz != calPriceClazz) {
                    calPriceList.add((Class<? extends CalPrice>) clazz);
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
    //获取扫描的包下面所有的class文件
    private File[] getResources(){
        try {
            File file = new File(classLoader.getResource(CAL_PRICE_PACKAGE.replace(".", "/")).toURI());
            return file.listFiles(new FileFilter() {
                public boolean accept(File pathname) {
                    if (pathname.getName().endsWith(".class")) {//我们只扫描class文件
                        return true;
                    }
                    return false;
                }
            });
        } catch (URISyntaxException e) {
            throw new RuntimeException("未找到策略资源");
        }
    }

    public static CalPriceFactory getInstance(){
        return new CalPriceFactory();
    }
}

package test.strategy03;


public class Test {
    public static void main(String[] args) {
        Customer customer = new Customer();
        customer.buy(500D);
        System.out.println("客户需要付钱"+customer.calLastAmount());

        customer.buy(1200D);
        System.out.println("客户需要付钱"+customer.calLastAmount());

        customer.buy(1200D);
        System.out.println("客户需要付钱"+customer.calLastAmount());

        customer.buy(1200D);
        System.out.println("客户需要付钱"+customer.calLastAmount());
    }
}

在这里插入图片描述

总结

  • 优点:提供了可以替换继承关系的办法;消除了一些if else条件语句;算法可以自由切换,扩展性比较好
  • 缺点:客户端必须知道所有的策略类,并自行决定使用哪一个策略类;策略类数量会增多,每个策略都是一个类,复用的可能性很小
  • 应用场景:多个类只有算法或行为上稍有不同的场景;算法需要自由切换的场景;需要屏蔽算法规则的场景。例如:出行方式,自行车、汽车等,每一种出行方式都是一个策略;商场促销方式,打折、满减等
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

yemuxiaweiliang

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

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

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

打赏作者

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

抵扣说明:

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

余额充值