Skip to content

Commit 818778f

Browse files
author
laileon
committed
add strategy
1 parent 0f078d3 commit 818778f

36 files changed

+306
-107
lines changed

src/com/blankj/custom/factory/Benz.java renamed to src/com/blankj/custom/desingn_pattern/factory/Benz.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.blankj.custom.factory;
1+
package com.blankj.custom.desingn_pattern.factory;
22

33
public class Benz implements Car {
44

src/com/blankj/custom/factory/BenzFactory.java renamed to src/com/blankj/custom/desingn_pattern/factory/BenzFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.blankj.custom.factory;
1+
package com.blankj.custom.desingn_pattern.factory;
22

33
public interface BenzFactory {
44
static Car createCar() {

src/com/blankj/custom/factory/Bmw.java renamed to src/com/blankj/custom/desingn_pattern/factory/Bmw.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.blankj.custom.factory;
1+
package com.blankj.custom.desingn_pattern.factory;
22

33
public class Bmw implements Car{
44

src/com/blankj/custom/factory/BmwFactory.java renamed to src/com/blankj/custom/desingn_pattern/factory/BmwFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.blankj.custom.factory;
1+
package com.blankj.custom.desingn_pattern.factory;
22

33
public interface BmwFactory {
44
static Car createCar() {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.blankj.custom.desingn_pattern.factory;
2+
3+
public interface Car {
4+
void create();
5+
}

src/com/blankj/custom/factory/Main.java renamed to src/com/blankj/custom/desingn_pattern/factory/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.blankj.custom.factory;
1+
package com.blankj.custom.desingn_pattern.factory;
22

33
public class Main {
44
public static void main(String[] args) {

src/com/blankj/custom/single/Single1.java renamed to src/com/blankj/custom/desingn_pattern/single/Single1.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.blankj.custom.single;
1+
package com.blankj.custom.desingn_pattern.single;
22

33
// 懒汉式(线程安全问题)
44
public class Single1 {

src/com/blankj/custom/single/Single2.java renamed to src/com/blankj/custom/desingn_pattern/single/Single2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.blankj.custom.single;
1+
package com.blankj.custom.desingn_pattern.single;
22

33
// 懒汉式()
44
public class Single2 {

src/com/blankj/custom/single/Single3.java renamed to src/com/blankj/custom/desingn_pattern/single/Single3.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.blankj.custom.single;
1+
package com.blankj.custom.desingn_pattern.single;
22

33
/**
44
* 码家学院
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.blankj.custom.desingn_pattern.strategy;
2+
3+
public class ConcreteStrategyA implements Strategy {
4+
@Override
5+
public void strategyInterface() {
6+
//相关的业务
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.blankj.custom.desingn_pattern.strategy;
2+
3+
public class ConcreteStrategyB implements Strategy {
4+
@Override
5+
public void strategyInterface() {
6+
//相关的业务
7+
}
8+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.blankj.custom.desingn_pattern.strategy;
2+
3+
public class Context {
4+
//持有一个具体策略的对象
5+
private Strategy strategy;
6+
7+
/**
8+
* 构造函数,传入一个具体策略对象
9+
*
10+
* @param strategy 具体策略对象
11+
*/
12+
public Context(Strategy strategy) {
13+
this.strategy = strategy;
14+
}
15+
16+
/**
17+
* 策略方法
18+
*/
19+
public void contextInterface() {
20+
strategy.strategyInterface();
21+
}
22+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
这个模式涉及到三个角色:
2+
3+
环境(Context)角色:持有一个Strategy的引用。
4+
5+
抽象策略(Strategy)角色:这是一个抽象角色,通常由一个接口或抽象类实现。此角色给出所有的具体策略类所需的接口。
6+
7+
具体策略(ConcreteStrategy)角色:包装了相关的算法或行为。
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.blankj.custom.desingn_pattern.strategy;
2+
3+
public interface Strategy {
4+
/**
5+
* 策略方法
6+
*/
7+
void strategyInterface();
8+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.blankj.custom.desingn_pattern.strategy.example;
2+
3+
public interface CalPrice {
4+
//根据原价返回一个最终的价格
5+
Double calPrice(Double orgnicPrice);
6+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package com.blankj.custom.desingn_pattern.strategy.example;
2+
3+
public class CalPriceFactory {
4+
private CalPriceFactory() {
5+
}
6+
7+
//根据客户的总金额产生相应的策略
8+
public static CalPrice createCalPrice(Player customer) {
9+
if (customer.getTotalAmount() > 30000) {//3000则改为金牌会员计算方式
10+
return new GoldVip();
11+
} else if (customer.getTotalAmount() > 20000) {//类似
12+
return new SuperVip();
13+
} else if (customer.getTotalAmount() > 10000) {//类似
14+
return new Vip();
15+
} else {
16+
return new Orgnic();
17+
}
18+
}
19+
20+
// private static final String CAL_PRICE_PACKAGE = "com.blankj.custom.desingn_pattern.strategy.example";//这里是一个常量,表示我们扫描策略的包
21+
//
22+
// private ClassLoader classLoader = getClass().getClassLoader();
23+
//
24+
// private List<Class<? extends CalPrice>> calPriceList;//策略列表
25+
//
26+
// //根据玩家的总金额产生相应的策略
27+
// public CalPrice createCalPrice(Player player) {
28+
// //在策略列表查找策略
29+
// for (Class<? extends CalPrice> clazz : calPriceList) {
30+
// PriceRegion validRegion = handleAnnotation(clazz);//获取该策略的注解
31+
// //判断金额是否在注解的区间
32+
// if (player.getTotalAmount() > validRegion.min() && player.getTotalAmount() < validRegion.max()) {
33+
// try {
34+
// //是的话我们返回一个当前策略的实例
35+
// return clazz.newInstance();
36+
// } catch (Exception e) {
37+
// throw new RuntimeException("策略获得失败");
38+
// }
39+
// }
40+
// }
41+
// throw new RuntimeException("策略获得失败");
42+
// }
43+
//
44+
// private PriceRegion handleAnnotation(Class<? extends CalPrice> clazz) {
45+
// Annotation[] annotations = clazz.getDeclaredAnnotations();
46+
// if (annotations == null || annotations.length == 0) {
47+
// return null;
48+
// }
49+
// for (int i = 0; i < annotations.length; i++) {
50+
// if (annotations[i] instanceof PriceRegion) {
51+
// return (PriceRegion) annotations[i];
52+
// }
53+
// }
54+
// return null;
55+
// }
56+
//
57+
// //单例
58+
// private CalPriceFactory() {
59+
// init();
60+
// }
61+
//
62+
// //在工厂初始化时要初始化策略列表
63+
// private void init() {
64+
// calPriceList = new ArrayList<Class<? extends CalPrice>>();
65+
// File[] resources = getResources();//获取到包下所有的class文件
66+
// Class<CalPrice> calPriceClazz = null;
67+
// try {
68+
// calPriceClazz = (Class<CalPrice>) classLoader.loadClass(CalPrice.class.getName());//使用相同的加载器加载策略接口
69+
// } catch (ClassNotFoundException e1) {
70+
// throw new RuntimeException("未找到策略接口");
71+
// }
72+
// for (int i = 0; i < resources.length; i++) {
73+
// try {
74+
// //载入包下的类
75+
// Class<?> clazz = classLoader.loadClass(CAL_PRICE_PACKAGE + "." + resources[i].getName().replace(".class", ""));
76+
// //判断是否是CalPrice的实现类并且不是CalPrice它本身,满足的话加入到策略列表
77+
// if (CalPrice.class.isAssignableFrom(clazz) && clazz != calPriceClazz) {
78+
// calPriceList.add((Class<? extends CalPrice>) clazz);
79+
// }
80+
// } catch (ClassNotFoundException e) {
81+
// e.printStackTrace();
82+
// }
83+
// }
84+
// }
85+
//
86+
// //获取扫描的包下面所有的class文件
87+
// private File[] getResources() {
88+
// try {
89+
// File file = new File(classLoader.getResource(CAL_PRICE_PACKAGE.replace(".", "/")).toURI());
90+
// return file.listFiles(new FileFilter() {
91+
// public boolean accept(File pathname) {
92+
// if (pathname.getName().endsWith(".class")) {//我们只扫描class文件
93+
// return true;
94+
// }
95+
// return false;
96+
// }
97+
// });
98+
// } catch (URISyntaxException e) {
99+
// throw new RuntimeException("未找到策略资源");
100+
// }
101+
// }
102+
//
103+
// public static CalPriceFactory getInstance() {
104+
// return CalPriceFactoryInstance.instance;
105+
// }
106+
//
107+
// private static class CalPriceFactoryInstance {
108+
//
109+
// private static CalPriceFactory instance = new CalPriceFactory();
110+
// }
111+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.blankj.custom.desingn_pattern.strategy.example;
2+
3+
public class Client {
4+
public static void main(String[] args) {
5+
Player player = new Player();
6+
player.buy(5000D);
7+
System.out.println("玩家需要付钱:" + player.calLastAmount());
8+
player.buy(12000D);
9+
System.out.println("玩家需要付钱:" + player.calLastAmount());
10+
player.buy(12000D);
11+
System.out.println("玩家需要付钱:" + player.calLastAmount());
12+
player.buy(12000D);
13+
System.out.println("玩家需要付钱:" + player.calLastAmount());
14+
}
15+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.blankj.custom.desingn_pattern.strategy.example;
2+
3+
@PriceRegion(min=3000)
4+
public class GoldVip implements CalPrice {
5+
@Override
6+
public Double calPrice(Double orgnicPrice) {
7+
return orgnicPrice * 0.7;
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.blankj.custom.desingn_pattern.strategy.example;
2+
3+
@PriceRegion(max = 10000)
4+
public class Orgnic implements CalPrice {
5+
@Override
6+
public Double calPrice(Double orgnicPrice) {
7+
return orgnicPrice;
8+
}
9+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.blankj.custom.desingn_pattern.strategy.example;
2+
3+
public class Player {
4+
private Double totalAmount = 0D;//客户在鹅厂消费的总额
5+
private Double amount = 0D;//客户单次消费金额
6+
private CalPrice calPrice = new Orgnic();//每个客户都有一个计算价格的策略,初始都是普通计算,即原价
7+
8+
// //客户购买皮肤,就会增加它的总额
9+
// public void buy(Double amount) {
10+
// this.amount = amount;
11+
// totalAmount += amount;
12+
// if (totalAmount > 30000) {//30000则改为金牌会员计算方式
13+
// calPrice = new GoldVip();
14+
// } else if (totalAmount > 20000) {//类似
15+
// calPrice = new SuperVip();
16+
// } else if (totalAmount > 10000) {//类似
17+
// calPrice = new Vip();
18+
// }
19+
// }
20+
21+
//客户购买皮肤,就会增加它的总额
22+
public void buy(Double amount) {
23+
this.amount = amount;
24+
totalAmount += amount;
25+
/* 变化点,我们将策略的制定转移给了策略工厂,将这部分责任分离出去 */
26+
calPrice = CalPriceFactory.createCalPrice(this);
27+
// calPrice = CalPriceFactory.getInstance().createCalPrice(this);
28+
}
29+
30+
//计算客户最终要付的钱
31+
public Double calLastAmount() {
32+
return calPrice.calPrice(amount);
33+
}
34+
35+
public Double getTotalAmount() {
36+
return totalAmount;
37+
}
38+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.blankj.custom.desingn_pattern.strategy.example;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
//这是有效价格区间注解,可以给策略添加有效区间的设置
9+
@Target(ElementType.TYPE)//表示只能给类添加该注解
10+
@Retention(RetentionPolicy.RUNTIME)//这个必须要将注解保留在运行时
11+
public @interface PriceRegion {
12+
int max() default Integer.MAX_VALUE;
13+
int min() default Integer.MIN_VALUE;
14+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.blankj.custom.desingn_pattern.strategy.example;
2+
3+
@PriceRegion(min=20000,max=30000)
4+
public class SuperVip implements CalPrice {
5+
@Override
6+
public Double calPrice(Double orgnicPrice) {
7+
return orgnicPrice * 0.8;
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.blankj.custom.desingn_pattern.strategy.example;
2+
3+
@PriceRegion(max=20000)
4+
public class Vip implements CalPrice {
5+
@Override
6+
public Double calPrice(Double orgnicPrice) {
7+
return orgnicPrice * 0.9;
8+
}
9+
}

src/com/blankj/custom/factory/Car.java

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/com/blankj/custom/proxys/dynamicProxy/Client.java

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/com/blankj/custom/proxys/dynamicProxy/DynamicProxy.java

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/com/blankj/custom/proxys/dynamicProxy/ISubject.java

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/com/blankj/custom/proxys/dynamicProxy/MyInvocationHandler.java

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)