java设计模式实战:工厂模式+策略模式(注解+反射)

1.核心工厂类

/**
* 工厂类:初始化加载
* 作用:这个类初始化的时候会去加载指定包下的资源,通过类加载器将Class类对象加载进来,并存储在集合里面。
* 通过Class对象获取类上的注解,通过注解范围匹配到对应的策略上。该工厂通过静态内部类对外提供单例,不失为一种优雅的实现方式。
*/
public class CalPriceFactory {

    private static final String CAL_PRICE_PACKAGE = "org.landy.strategy.demo2.calc";//这里是一个常量,表示我们扫描策略的包

    private ClassLoader classLoader = getClass().getClassLoader();

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

//    private CalPriceFactory(){}
//    //根据客户的总金额产生相应的策略
//    public static CalPrice createCalPrice(Player customer){
//        if (customer.getTotalAmount() > 30000) {//3000则改为金牌会员计算方式
//            return new GoldVip();
//        }else if (customer.getTotalAmount() > 20000) {//类似
//            return new SuperVip();
//        }else if (customer.getTotalAmount() > 10000) {//类似
//            return new Vip();
//        }else {
//            return new Origin();
//        }
//    }


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

    //处理注解,我们传入一个策略类,返回它的注解
    private PriceRegion 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 PriceRegion) {
                return (PriceRegion) 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 CalPriceFactoryInstance.instance;
    }

    private static class CalPriceFactoryInstance {
        private static CalPriceFactory instance = new CalPriceFactory();
    }


}

2.注解类

//这是有效价格区间注解,可以给策略添加有效区间的设置
@Target(ElementType.TYPE)//表示只能给类添加该注解
@Retention(RetentionPolicy.RUNTIME)//这个必须要将注解保留在运行时
public @interface PriceRegion {

    int max() default Integer.MAX_VALUE;
    int min() default Integer.MIN_VALUE;

}

3.策略接口及实现类

public interface CalPrice {

    //根据原价返回一个最终的价格
    Double calPrice(Double originPrice);
}
//===========================
@PriceRegion(min=3000)
public class GoldVip implements CalPrice {
    @Override
    public Double calPrice(Double originPrice) {
        return originPrice * 0.7;
    }
}
//=======================================
@PriceRegion(max = 10000)
public class Origin implements CalPrice {
    @Override
    public Double calPrice(Double originPrice) {
        return originPrice;
    }
}
//==================================
@PriceRegion(min=20000,max=30000)
public class SuperVip implements CalPrice {
    @Override
    public Double calPrice(Double originPrice) {
        return originPrice * 0.8;
    }
}
//============================
@PriceRegion(max=20000)
public class Vip implements CalPrice {
    @Override
    public Double calPrice(Double originPrice) {
        return originPrice * 0.9;
    }
}

4.消费者及策略类

public class Player {

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

    //客户购买皮肤,就会增加它的总额
    public void buy(Double amount) {
        this.amount = amount;
        totalAmount += amount;
        //实现方式一:不使用工厂,直接客户类判断策略
//        if (totalAmount > 30000) {//30000则改为金牌会员计算方式
//            calPrice = new GoldVip();
//        } else if (totalAmount > 20000) {//类似
//            calPrice = new SuperVip();
//        } else if (totalAmount > 10000) {//类似
//            calPrice = new Vip();
//        }

        //实现方式二:我们将策略的制定转移给了策略工厂,将这部分责任分离出去
//        calPrice = CalPriceFactory.createCalPrice(this);

        //实现方式三:利用注解动态配置策略
//        虽然结合简单工厂模式,我们的策略模式灵活了一些,
//        但不免发现在工厂中多了if-else判断,也就是如果增加一个会员类别,我又得增加一个else-if语句,这是简单工厂的缺点,对修改开放。
//        那有什么方法,可以较好的解决这个问题呢?那就是使用注解,
//        所以我们需要给注解加入属性上限和下限,用来表示策略生效的区间,用来解决总金额判断的问题。

        calPrice = CalPriceFactory.getInstance().createCalPrice(this);

    }

    //计算客户最终要付的钱
    public Double calLastAmount() {
        return calPrice.calPrice(amount);
    }

    public Double getTotalAmount() {
        return totalAmount;
    }

    public void setTotalAmount(Double totalAmount) {
        this.totalAmount = totalAmount;
    }

    public Double getAmount() {
        return amount;
    }

    public void setAmount(Double amount) {
        this.amount = amount;
    }
}

//===================测试客户端类
public class Client {

    public static void main(String[] args) {
        Player player = new Player();
        player.buy(5000D);
        System.out.println("玩家需要付钱:" + player.calLastAmount());
        player.buy(12000D);
        System.out.println("玩家需要付钱:" + player.calLastAmount());
        player.buy(20000D);
        System.out.println("玩家需要付钱:" + player.calLastAmount());
        player.buy(40000D);
        System.out.println("玩家需要付钱:" + player.calLastAmount());
    }

}


  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值