File tree Expand file tree Collapse file tree
src/main/java/com/cpucode/pattern/strategy/pay Expand file tree Collapse file tree Original file line number Diff line number Diff line change 117117- [x] [ 策略模式] ( src/main/java/com/cpucode/pattern/strategy/promotion/PromotionActivityTest2.java )
118118- [x] [ 策略模式] ( src/main/java/com/cpucode/pattern/strategy/promotion/PromotionActivityTest3.java )
119119- [x] [ 策略模式] ( src/main/java/com/cpucode/pattern/strategy/pay/PayStrategyTest.java )
120- - [x] [ 策略模式] ( src/main/java/com/cpucode/pattern/strategy/promotion/PromotionActivityTest3.java )
121120
122121## [ 模板模式] ( )
123122
Original file line number Diff line number Diff line change 1+ package com .cpucode .pattern .strategy .pay ;
2+
3+ import com .cpucode .pattern .strategy .pay .payport .PayState ;
4+ import com .cpucode .pattern .strategy .pay .payport .PayStrategy ;
5+ import com .cpucode .pattern .strategy .pay .payport .Payment ;
6+
7+ /**
8+ * @author : cpucode
9+ * @date : 2021/6/1
10+ * @time : 23:11
11+ * @github : https://github.com/CPU-Code
12+ * @csdn : https://blog.csdn.net/qq_44226094
13+ */
14+ public class Order {
15+ private String uid ;
16+ private String orderId ;
17+ private double amount ;
18+
19+ public Order (String uid ,String orderId ,double amount ){
20+ this .uid = uid ;
21+ this .orderId = orderId ;
22+ this .amount = amount ;
23+ }
24+
25+ /**
26+ * 完美地解决了 switch 的过程, 不需要在代码逻辑中写 switch 了
27+ * 更不需要写 if else if
28+ * @return
29+ */
30+ public PayState pay (){
31+ return pay (PayStrategy .DEFAULT_PAY );
32+ }
33+
34+ public PayState pay (String payKey ){
35+ Payment payment = PayStrategy .get (payKey );
36+
37+ System .out .println ("欢迎使用" + payment .getName ());
38+ System .out .println ("本次交易金额为: " + amount + ", 开始扣款..." );
39+
40+ return payment .pay (uid , amount );
41+ }
42+ }
Original file line number Diff line number Diff line change 1+ package com .cpucode .pattern .strategy .pay ;
2+
3+ import com .cpucode .pattern .strategy .pay .payport .PayStrategy ;
4+
5+ /**
6+ * @author : cpucode
7+ * @date : 2021/6/2
8+ * @time : 12:37
9+ * @github : https://github.com/CPU-Code
10+ * @csdn : https://blog.csdn.net/qq_44226094
11+ */
12+ public class PayStrategyTest {
13+ public static void main (String [] args ) {
14+ // 省略把商品添加到购物车, 再从购物车下单 , 直接从点单开始
15+ Order order = new Order ("1" ,"20180311001000009" ,324.45 );
16+
17+ /**
18+ * 开始支付, 选择微信支付、 支付宝、 银联卡、 京东白条、 财付通
19+ * 每个渠道它支付的具体算法是不一样的
20+ *
21+ * 基本算法固定的
22+ * 这个值是在支付的时候才决定用哪个值
23+ */
24+ System .out .println (order .pay (PayStrategy .ALI_PAY ));
25+ }
26+ }
Original file line number Diff line number Diff line change 1+ package com .cpucode .pattern .strategy .pay .payport ;
2+
3+ /**
4+ * 支付宝
5+ *
6+ * @author : cpucode
7+ * @date : 2021/6/2
8+ * @time : 12:03
9+ * @github : https://github.com/CPU-Code
10+ * @csdn : https://blog.csdn.net/qq_44226094
11+ */
12+ public class AliPay extends Payment {
13+ @ Override
14+ public String getName () {
15+ return "支付宝" ;
16+ }
17+
18+ @ Override
19+ protected double queryBalance (String uid ) {
20+ return 900 ;
21+ }
22+ }
Original file line number Diff line number Diff line change 1+ package com .cpucode .pattern .strategy .pay .payport ;
2+
3+ /**
4+ * 京东白条
5+ *
6+ * @author : cpucode
7+ * @date : 2021/6/2
8+ * @time : 12:04
9+ * @github : https://github.com/CPU-Code
10+ * @csdn : https://blog.csdn.net/qq_44226094
11+ */
12+ public class JDPay extends Payment {
13+ @ Override
14+ public String getName () {
15+ return "京东白条" ;
16+ }
17+
18+ @ Override
19+ protected double queryBalance (String uid ) {
20+ return 500 ;
21+ }
22+ }
Original file line number Diff line number Diff line change 1+ package com .cpucode .pattern .strategy .pay .payport ;
2+
3+ /**
4+ * 支付完成以后的状态
5+ *
6+ * @author : cpucode
7+ * @date : 2021/6/1
8+ * @time : 23:12
9+ * @github : https://github.com/CPU-Code
10+ * @csdn : https://blog.csdn.net/qq_44226094
11+ */
12+ public class PayState {
13+ private int code ;
14+ private Object data ;
15+ private String msg ;
16+
17+ public PayState (int code , String msg ,Object data ) {
18+ this .code = code ;
19+ this .data = data ;
20+ this .msg = msg ;
21+ }
22+
23+ @ Override
24+ public String toString (){
25+ return ("支付状态: [" + code + "]," + msg + ",交易详情: " + data );
26+ }
27+
28+ }
Original file line number Diff line number Diff line change 1+ package com .cpucode .pattern .strategy .pay .payport ;
2+
3+ import java .util .HashMap ;
4+ import java .util .Map ;
5+
6+ /**
7+ * 支付策略管理
8+ *
9+ * @author : cpucode
10+ * @date : 2021/6/2
11+ * @time : 12:29
12+ * @github : https://github.com/CPU-Code
13+ * @csdn : https://blog.csdn.net/qq_44226094
14+ */
15+ public class PayStrategy {
16+ public static final String ALI_PAY = "AliPay" ;
17+ public static final String JD_PAY = "JdPay" ;
18+ public static final String UNION_PAY = "UnionPay" ;
19+ public static final String WECHAT_PAY = "WechatPay" ;
20+ public static final String DEFAULT_PAY = ALI_PAY ;
21+
22+ private static Map <String , Payment > payStrategy = new HashMap <String , Payment >();
23+
24+ static {
25+ payStrategy .put (ALI_PAY ,new AliPay ());
26+ payStrategy .put (WECHAT_PAY ,new WechatPay ());
27+ payStrategy .put (UNION_PAY ,new UnionPay ());
28+ payStrategy .put (JD_PAY ,new JDPay ());
29+ }
30+
31+ public static Payment get (String payKey ){
32+ if (!payStrategy .containsKey (payKey )){
33+ return payStrategy .get (DEFAULT_PAY );
34+ }
35+
36+ return payStrategy .get (payKey );
37+ }
38+
39+ }
Original file line number Diff line number Diff line change 1+ package com .cpucode .pattern .strategy .pay .payport ;
2+
3+ /**
4+ * 支付渠道
5+ *
6+ * @author : cpucode
7+ * @date : 2021/6/1
8+ * @time : 23:11
9+ * @github : https://github.com/CPU-Code
10+ * @csdn : https://blog.csdn.net/qq_44226094
11+ */
12+ public abstract class Payment {
13+ /**
14+ * 支付类型
15+ * @return
16+ */
17+ public abstract String getName ();
18+
19+ /**
20+ * 查询余额
21+ * @param uid
22+ * @return
23+ */
24+ protected abstract double queryBalance (String uid );
25+
26+ /**
27+ * 扣款支付
28+ * @param uid
29+ * @param amount
30+ * @return
31+ */
32+ public PayState pay (String uid , double amount ) {
33+ if (queryBalance (uid ) < amount ){
34+ return new PayState (500 , "支付失败" , "余额不足" );
35+ }
36+
37+ return new PayState (200 , "支付成功" , "支付金额: " + amount );
38+ }
39+ }
Original file line number Diff line number Diff line change 1+ package com .cpucode .pattern .strategy .pay .payport ;
2+
3+ /**
4+ * 银联支付
5+ *
6+ * @author : cpucode
7+ * @date : 2021/6/2
8+ * @time : 12:06
9+ * @github : https://github.com/CPU-Code
10+ * @csdn : https://blog.csdn.net/qq_44226094
11+ */
12+ public class UnionPay extends Payment {
13+ @ Override
14+ public String getName () {
15+ return "银联支付" ;
16+ }
17+
18+ @ Override
19+ protected double queryBalance (String uid ) {
20+ return 120 ;
21+ }
22+ }
Original file line number Diff line number Diff line change 1+ package com .cpucode .pattern .strategy .pay .payport ;
2+
3+ /**
4+ * 微信支付
5+ *
6+ * @author : cpucode
7+ * @date : 2021/6/2
8+ * @time : 12:05
9+ * @github : https://github.com/CPU-Code
10+ * @csdn : https://blog.csdn.net/qq_44226094
11+ */
12+ public class WechatPay extends Payment {
13+ @ Override
14+ public String getName () {
15+ return "微信支付" ;
16+ }
17+
18+ @ Override
19+ protected double queryBalance (String uid ) {
20+ return 256 ;
21+ }
22+ }
You can’t perform that action at this time.
0 commit comments