package strategypattern.strategy;
/**
* 策略方法接口类
*
*/
public interface CalPrice {
/**
* 策略方法
* 根据原价返回一个最终的价格
*/
Double calPrice(Double orgnicPrice);
}
package strategypattern.strategy;
/**
* 策略方法实现类配合注解
* */
@PriceRegion(min=0,max = 10000)
public class Orgnic implements CalPrice {
@Override
public Double calPrice(Double orgnicPrice) {
return orgnicPrice;
}
}
package strategypattern.strategy;
/**
* 策略方法实现类配合注解
* */
@PriceRegion(min=10000,max=15000)
public class GoldVip implements CalPrice {
@Override
public Double calPrice(Double orgnicPrice) {
return orgnicPrice * 0.7;
}
}
package strategypattern.strategy;
/**
* 策略方法实现类配合注解
* */
@PriceRegion(min=15000,max=20000)
public class Vip implements CalPrice {
@Override
public Double calPrice(Double orgnicPrice) {
return orgnicPrice * 0.9;
}
}
package strategypattern.strategy;
/**
* 策略方法实现类配合注解
* */
@PriceRegion(min=20000,max=30000)
public class SuperVip implements CalPrice {
@Override
public Double calPrice(Double orgnicPrice) {
return orgnicPrice * 0.8;
}
}
package strategypattern.strategy;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 注解类
* @ClassName: PriceRegion
* @Description: TODO(这里用一句话描述这个类的作用)
* @author A18ccms a18ccms_gmail_com
* @date 2018年6月5日 下午3:30:32
*
*/
//这是有效价格区间注解,可以给策略添加有效区间的设置
@Target(ElementType.TYPE)//表示只能给类添加该注解
@Retention(RetentionPolicy.RUNTIME)//这个必须要将注解保留在运行时
public @interface PriceRegion {
int max() default Integer.MAX_VALUE;
int min() default Integer.MIN_VALUE;
}
package strategypattern.strategy;
/**
* 环境类
*/
public class Player {
private Double totalAmount = 0D;//客户在鹅厂消费的总额
private Double amount = 0D;//客户单次消费金额
private CalPrice calPrice = new Orgnic();//每个客户都有一个计算价格的策略,初始都是普通计算,即原价
//客户购买皮肤,就会增加它的总额
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;
}
}
package strategypattern.strategy;
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;
/**
*
* @ClassName: CalPriceFactory
* @Description: TODO(单例工厂类)
* @author A18ccms a18ccms_gmail_com
* @date 2018年6月5日 下午3:32:37
*
*/
public class CalPriceFactory {
private static final String CAL_PRICE_PACKAGE = "strategypattern.strategy";//这里是一个常量,表示我们扫描策略的包
private ClassLoader classLoader = getClass().getClassLoader();
private List<Class<? extends CalPrice>> calPriceList;//策略列表
//根据玩家的总金额产生相应的策略
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<Class<? extends CalPrice>>();
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();
}
}
package strategypattern.strategy;
/**测试类*/
public class Client {
public static void main(String[] args) {
Player player = new Player();
player.buy(5000D);
System.out.println("玩家需要付钱:" + player.calLastAmount());
player.buy(6000D);
System.out.println("玩家需要付钱:" + player.calLastAmount());
player.buy(6000D);
System.out.println("玩家需要付钱:" + player.calLastAmount());
player.buy(7000D);
System.out.println("玩家需要付钱:" + player.calLastAmount());
}
}
稍微做了下修改,环境类用单例调用了工厂类。并且注解最大值最小值做了调整,不然只写最小值不写最大值,最大值默认是20多万。必须两个都填或者改下逻辑判断,我把注解的最大值最小值都填上了。
https://www.cnblogs.com/zuoxiaolong/p/pattern8.html