Skip to content

Commit 158398b

Browse files
使用枚举和反射、策略模式,去掉过多的if else
1 parent 69d8e70 commit 158398b

File tree

7 files changed

+173
-0
lines changed

7 files changed

+173
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.enumdemo.pay;
2+
3+
import com.enumdemo.pay.service.Alipay;
4+
import com.enumdemo.pay.service.EbankPay;
5+
import com.enumdemo.pay.service.PayService;
6+
import com.enumdemo.pay.service.WechatPay;
7+
8+
/**
9+
* @Author: EnjoyCoding
10+
* @Date: 2020\1\13 0013 21:14
11+
* @Description:
12+
* 详情参见: https://blog.csdn.net/u012557814/article/details/81671928
13+
*/
14+
public class Client {
15+
16+
public static void main(String[] args) throws Exception {
17+
String payType="1";
18+
// String payType = "ALIPAY";
19+
// String payType = "2";
20+
// String payType = "3";
21+
22+
23+
// if("1".equals(payType)) {
24+
// PayService pay=new Alipay();
25+
// } else if("2".equals(payType)) {
26+
// PayService pay=new WechatPay();
27+
// } else if ("3".equals(payType)) {
28+
// PayService pay=new EbankPay();
29+
// }
30+
//以上代码存在过多的if else判断,可以通过策略模式简化。
31+
PayService strategy = PayStrategy.getPayStrategy(payType);
32+
strategy.pay(12);
33+
34+
}
35+
36+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.enumdemo.pay;
2+
3+
/**
4+
* @Auther: Administrator
5+
* @Date: 2020\1\13 0013 20:38
6+
* @Description:
7+
*/
8+
public enum PayEnum {
9+
/**
10+
* 枚举常量对应的className,根据项目类的路径设置
11+
*
12+
*/
13+
ALIPAY("1","com.enumdemo.pay.service.Alipay"),
14+
WECHATPAY("2","com.enumdemo.pay.service.WechatPay"),
15+
EBANKPAY("3","com.enumdemo.pay.service.EbankPay")
16+
17+
;
18+
19+
private String payClassName;
20+
private String payType;
21+
22+
23+
24+
PayEnum(String payType, String payClassName) {
25+
this.setPayClassName(payClassName);
26+
this.setPayType(payType);
27+
}
28+
29+
public String getPayClassName() {
30+
return payClassName;
31+
}
32+
33+
public void setPayClassName(String payClassName) {
34+
this.payClassName = payClassName;
35+
}
36+
37+
public void setPayType(String payType) {
38+
this.payType = payType;
39+
}
40+
41+
public String getPayType() {
42+
return payType;
43+
}
44+
45+
/**
46+
* 根据支付类型,得到对应的支付类的路径
47+
*
48+
* @param payType
49+
* @return
50+
*/
51+
public static String getPayClassNameByType(String payType) {
52+
for (PayEnum payEnum : PayEnum.values()) {
53+
if (payEnum.getPayType().equals(payType)) {
54+
return payEnum.getPayClassName();
55+
}
56+
}
57+
return null;
58+
}
59+
60+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.enumdemo.pay;
2+
3+
import com.enumdemo.pay.service.PayService;
4+
5+
/**
6+
* @Author: EnjoyCoding
7+
* @Date: 2020\1\13 0013 20:40
8+
* @Description:
9+
*
10+
*/
11+
public class PayStrategy {
12+
13+
public static PayService getPayStrategy(String payType) throws Exception {
14+
//valueOf()方法,可以通过枚举常量的名称(比如ALIPAY),得到枚举常量。再通过枚举常量得到支付类的路径
15+
// String payClassName = PayEnum.valueOf(payType).getPayClassName();
16+
//直接根据支付类型,获取对应的支付类的路径
17+
String payClassName= PayEnum.getPayClassNameByType(payType);
18+
//根据类名进行反射,生成对象
19+
return (PayService) Class.forName(payClassName).newInstance();
20+
}
21+
22+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.enumdemo.pay.service;
2+
3+
/**
4+
* @Author: EnjoyCoding
5+
* @Date: 20:51 2020\1\13 0013
6+
* @Description:
7+
*/
8+
public class Alipay implements PayService {
9+
10+
public void pay(double total) {
11+
System.out.println("pay with alipay: " + total);
12+
}
13+
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.enumdemo.pay.service;
2+
3+
/**
4+
* @Author: EnjoyCoding
5+
* @Date: 2020\1\13 0013 20:55
6+
* @Description:
7+
*/
8+
public class EbankPay implements PayService {
9+
10+
public void pay(double total) {
11+
System.out.println("pay with ebankPay: " + total);
12+
}
13+
14+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.enumdemo.pay.service;
2+
3+
/**
4+
* @Auther: Administrator
5+
* @Date: 2020\1\13 0013 20:50
6+
* @Description:
7+
*/
8+
public interface PayService {
9+
10+
void pay(double total);
11+
12+
}
13+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.enumdemo.pay.service;
2+
3+
/**
4+
* @Author: EnjoyCoding
5+
* @Date: 2020\1\13 0013 20:53
6+
* @Description:
7+
*/
8+
public class WechatPay implements PayService {
9+
10+
public void pay(double total) {
11+
System.out.println("pay with wechatpay: " + total);
12+
}
13+
14+
}

0 commit comments

Comments
 (0)