概述
写代码时总会出很多的if…else,或者case。如果在一个条件语句中又包含了多个条件语句就会使得代码变得臃肿,维护的成本也会加大,而策略模式就能较好的解决这个问题,本篇博客就带你详细了解策略模式。
定义:策略模式定义了一系列的算法,并将每一个算法封装起来,而且使他们可以相互替换,让算法独立于使用它的客户而独立变化。
策略模式的使用场景:
针对同一类型问题的多种处理方式,仅仅是具体行为有差别时;
需要安全地封装多种同一类型的操作时;
出现同一抽象类有多个子类,而又需要使用 if-else 或者 switch-case 来选择具体子类时。
UML
策略模式UML
所设计到的角色介绍
环境(具体使用类)角色:持有一个Strategy的引用
抽象策略(IStrategy)角色: 这是一个抽象角色,通常由一个接口或抽象类实现。此角色给出所有的具体策略类所需的接口。
具体策略(StrategyA)角色: 包装了相关的算法或行为。
策略模式例子
假设腾讯推出了3种会员,分别为会员,超级会员以及金牌会员,还有就是普通玩家,针对不同类别的玩家,购买《王者荣耀》皮肤有不同的打折方式,并且一个顾客每消费10000就增加一个级别,那么我们就可以使用策略模式,因为策略模式描述的就是算法的不同,这里我们举例就采用最简单的,以上四种玩家分别采用原价(普通玩家),九折,八折和七价的收钱方式。
那么我们首先要有一个计算价格的策略接口:
public interface CalPrice { /**
* 根据原价返回一个最终的价格
* @param orgnicPrice
* @return
*/
Double calPrice(Double orgnicPrice);
}下面是4种玩家的计算方式的实现
public class Orgnic implements CalPrice { @Override
public Double calPrice(Double orgnicPrice) { return orgnicPrice;
}
}public class Vip implements CalPrice { @Override
public Double calPrice(Double orgnicPrice) { return orgnicPrice * 0.9;
}
}public class SuperVip implements CalPrice { @Override
public Double calPrice(Double orgnicPrice) { return orgnicPrice * 0.8;
}
}public class GoldVip implements CalPrice { @Override
public Double calPrice(Double orgnicPrice) { return orgnicPrice * 0.7;
}
}我们看客户类,我们需要客户类帮我们完成玩家升级的功能。
public class Player {
//客户在腾讯消费的总额
private Double totalAmount = 0D; //客户单次消费金额
private Double amount = 0D; //个客户都有一个计算价格的策略,初始都是普通计算,即原价
private CalPrice calPrice = new Orgnic(); /**
* 客户购买皮肤,就会增加它的总额
* @param amount
*/
public void buy(Double amount) { this.amount = amount;
totalAmount += amount; if (totalAmount > 30000) {
calPrice = new GoldVip();
} else if (totalAmount > 20000) {
calPrice = new SuperVip();
} else if (totalAmount > 10000) {
calPrice = new Vip();
}
} /**
* 计算客户最终要付的钱
* @return
*/
public Double calFinalPrice() { return calPrice.calPrice(amount);
} public Double getTotalAmount() { return totalAmount;
} public void setTotalAmount(Double totalAmount) { this.totalAmount = totalAmount;
}
}接下来是客户端调用,系统会帮我们自动调整收费策略
public class Client {
public static void main(String[] args) {
Player player = new Player();
player.buy(5000D);
System.out.println("玩家需要付钱:" + player.calFinalPrice());
player.buy(12000D);
System.out.println("玩家需要付钱:" + player.calFinalPrice());
player.buy(12000D);
System.out.println("玩家需要付钱:" + player.calFinalPrice());
player.buy(12000D);
System.out.println("玩家需要付钱:" + player.calFinalPrice());
}
}
运行结果:
玩家需要付钱:5000.0玩家需要付钱:10800.0玩家需要付钱:9600.0玩家需要付钱:8400.0运行以后会发现,第一次是原价,第二次是九折,第三次是八折,最后一次则是七价。这样设计的好处是,客户不再依赖于具体的收费策略,依赖于抽象永远是正确的。
使用简单工厂进行改造
public class CalPriceFactory { private CalPriceFactory() {} /**
* 根据客户的总金额产生相应的策略
* @param player
* @return
*/
public static CalPrice createCalPrice(Player player) { if (player.getTotalAmount() > 30000) { return new GoldVip();
} else if (player.getTotalAmount() > 20000) { return new SuperVip();
} else if (player.getTotalAmount() > 10000) { return new Vip();
} else { return new Orgnic();
}
}
}这样就将制定策略的功能从客户类分离了出来,我们的客户类可以变成这样。
public class Player {
//客户在腾讯消费的总额
private Double totalAmount = 0D; //客户单次消费金额
private Double amount = 0D; //个客户都有一个计算价格的策略,初始都是普通计算,即原价
private CalPrice calPrice = new Orgnic(); /**
* 客户购买皮肤,就会增加它的总额
* @param amount
*/
public void buy(Double amount) { this.amount = amount;
totalAmount += amount;
calPrice = CalPriceFactory.createCalPrice(this);
} /**
* 计算客户最终要付的钱
* @return
*/
public Double calFinalPrice() { return calPrice.calPrice(amount);
} public Double getTotalAmount() { return totalAmount;
} public void setTotalAmount(Double totalAmount) { this.totalAmount = totalAmount;
}
}虽然结合简单工厂模式,我们的策略模式灵活了一些,但不免发现在工厂中多了if-else判断,也就是如果增加一个会员类别,我又得增加一个else-if语句,这是简单工厂的缺点,对修改开放。
那有什么方法,可以较好的解决这个问题呢?那就是使用注解, 所以我们需要给注解加入属性上限和下限,用来表示策略生效的区间,用来解决总金额判断的问题。
注解改造if-else
先我们做一个注解,这个注解是用来给策略添加的,当中可以设置它的上下限
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 PriceRegion { int max() default Integer.MAX_VALUE; int min() default Integer.MIN_VALUE;
}可以看到,我们只是使用这个注解来声明每一个策略的生效区间,于是对策略进行修改
@com.dsguo.strategy.PriceRegion(max = 10000)public class Orgnic implements com.dsguo.strategy.CalPrice { @Override
public Double calPrice(Double orgnicPrice) { return orgnicPrice;
}
}@com.dsguo.strategy.PriceRegion(max = 20000)public class Vip implements com.dsguo.strategy.CalPrice { @Override
public Double calPrice(Double orgnicPrice) { return orgnicPrice * 0.9;
}
}@com.dsguo.strategy.PriceRegion(min = 20000, max = 30000)public class SuperVip implements com.dsguo.strategy.CalPrice { @Override
public Double calPrice(Double orgnicPrice) { return orgnicPrice * 0.8;
}
}@com.dsguo.strategy.PriceRegion(min = 30000)public class GoldVip implements com.dsguo.strategy.CalPrice { @Override
public Double calPrice(Double orgnicPrice) { return orgnicPrice * 0.7;
}
}接下来就是在策略工厂中去处理注解
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 = "com.dsguo.strategy"; private ClassLoader classLoader = getClass().getClassLoader(); //策略表
private List<Class<? extends com.dsguo.strategy.CalPrice>> calPriceList; /**
* 单例
*/
private CalPriceFactory() {
init();
} /**
* 在工厂初始化时要初始化策略列表
*/
private void init() {
calPriceList = new ArrayList<Class<? extends com.dsguo.strategy.CalPrice>>();
File[] resources = getResources();
Class<com.dsguo.strategy.CalPrice> calPriceClazz = null; try {
calPriceClazz = (Class<com.dsguo.strategy.CalPrice>)classLoader.loadClass(com.dsguo.strategy.CalPrice.class.getName());
} catch (ClassNotFoundException e) {
e.printStackTrace(); throw new RuntimeException("未找到策略接口");
} for (int i = 0; i < resources.length; i++) {
Class<?> clazz = null; try {
String name = CAL_PRICE_PACKAGE + "." + resources[i].getName().replace(".class", ""); if (name.startsWith(".")) {
name = name.substring(1);
}
clazz = classLoader.loadClass(name);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} //判断是否是CalPrice的实现类并且不是CalPrice它本身,满足的话加入到策略列表
if (com.dsguo.strategy.CalPrice.class.isAssignableFrom(clazz) && clazz != calPriceClazz) {
calPriceList.add((Class<? extends com.dsguo.strategy.CalPrice>) clazz);
}
}
} public static CalPriceFactory getInstance() { return CalPriceFactoryInstance.instance;
} public static class CalPriceFactoryInstance { private static CalPriceFactory instance = new CalPriceFactory();
} private File[] getResources() { try {
File file = new File(classLoader.getResource(CAL_PRICE_PACKAGE.replace(".", "/")).toURI()); return file.listFiles(new FileFilter() { @Override
public boolean accept(File pathname) { //我们只扫描class文件
if (pathname.getName().endsWith(".class")) { return true;
} return false;
}
});
} catch (URISyntaxException e) {
e.printStackTrace(); throw new RuntimeException("未找到策略资源");
}
} /**
* 根据客户的总金额产生相应的策略
* @param player
* @return
*/
public com.dsguo.strategy.CalPrice createCalPrice(com.dsguo.strategy.Player player) { for (Class<? extends com.dsguo.strategy.CalPrice> clazz: calPriceList) { //获取该策略的注解
com.dsguo.strategy.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 com.dsguo.strategy.PriceRegion handleAnnotation(Class<? extends com.dsguo.strategy.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 com.dsguo.strategy.PriceRegion) { return (com.dsguo.strategy.PriceRegion) annotations[i];
}
} return null;
}
}
作者:ikonan
链接:https://www.jianshu.com/p/8c27994baaab
共同学习,写下你的评论
评论加载中...
作者其他优质文章
