工厂模式-微信支付、支付宝支付(转载)

当我们做支付项目时,常常项目中需要接入多种支付方式(微信、支付宝、银联等)。
其实这些支付方式,都存在一些类似的业务接口,比如支付、查询、退款等等。
区别就在于每一家参数不一致(参数名不一致)。我们可以利用工厂模式将支付厂商封装起来。
利用工厂模式,我们可以更方便的选择支付方式接口。

下面代码仅仅是结构,暂无具体的支付实现。

下面代码以java代码为例,工厂模式与编程语言无关,其他编程语言也可以,如C# , Go。

代码中类似ResultCreatePay、CreatePayParas仅仅是命名,暂无具体属性。

一、创建接口
/** 支付厂商(微信、支付宝、银联等)公共支付接口
*/
public interface Pay {

/** 1.创建支付
*/
ResultCreatePay CreatePay(CreatePayParas createPayParas);

/** 2.支付后,支付厂商回调通知结果处理
*/
ResultPayNotify PayNotify(PayNotifyParas payNotifyParas);

/** 3.查询支付结果状态
*/
ResultQueryPayStatus QueryPayStatus(QueryPayStatusParas queryPayStatusParas);

/** 4.创建退款
*/
ResultCreateRefund CreateRefund(CreateRefundParas createRefundParas);

/** 5.退款后,支付厂商回调通知结果处理
*/
ResultRefundNotify RefundNotify(RefundNotifyParas refundNotifyParas);

/*** 其他公共接口,暂不一一列举 ***/
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

二、实现接口的实体类
微信支付:

/** 支付厂商(微信)支付相关接口实现
*/
public class WeiXinPay implements Pay {

/** 1.创建支付
*/
@Override
public ResultCreatePay CreatePay(CreatePayParas createPayParas){
//实现微信接口
return new ResultCreatePay()
}

/** 2.支付后,支付厂商回调通知结果处理
*/
@Override
public ResultPayNotify PayNotify(PayNotifyParas payNotifyParas){
//实现微信接口
return new ResultPayNotify()
}

/** 3.查询支付结果状态
*/
@Override
public ResultQueryPayStatus QueryPayStatus(QueryPayStatusParas queryPayStatusParas){
//实现微信接口
return new ResultQueryPayStatus()
}

/** 4.创建退款
*/
@Override
public ResultCreateRefund CreateRefund(CreateRefundParas createRefundParas){
//实现微信接口
return new ResultCreateRefund()
}

/** 5.退款后,支付厂商回调通知结果处理
*/
@Override
public ResultRefundNotify RefundNotify(RefundNotifyParas refundNotifyParas){
//实现微信接口
return new ResultRefundNotify()
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
支付宝支付:

/** 支付厂商(支付宝)支付相关接口实现
*/
public class ALiPay implements Pay {

/** 1.创建支付
*/
@Override
public ResultCreatePay CreatePay(CreatePayParas createPayParas){
//实现支付宝接口
return new ResultCreatePay()
}

/** 2.支付后,支付厂商回调通知结果处理
*/
@Override
public ResultPayNotify PayNotify(PayNotifyParas payNotifyParas){
//实现支付宝接口
return new ResultPayNotify()
}

/** 3.查询支付结果状态
*/
@Override
public ResultQueryPayStatus QueryPayStatus(QueryPayStatusParas queryPayStatusParas){
//实现支付宝接口
return new ResultQueryPayStatus()
}

/** 4.创建退款
*/
@Override
public ResultCreateRefund CreateRefund(CreateRefundParas createRefundParas){
//实现支付宝接口
return new ResultCreateRefund()
}

/** 5.退款后,支付厂商回调通知结果处理
*/
@Override
public ResultRefundNotify RefundNotify(RefundNotifyParas refundNotifyParas){
//实现支付宝接口
return new ResultRefundNotify()
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

三、创建工厂,生成基于特定类型的实体类
public class PayFactory {

public Pay getPay(String payType){
if(payType.equals(“ALiPay”)){
return new ALiPay();
} else if(payType.equals(“WeiXinPay”)){
return new WeiXinPay();
}

  return null;

}
}
1
2
3
4
5
6
7
8
9
10
11
12

四、使用工厂,通过传递支付类型来获取实体类的对象
public class Demo {

public static void main(String[] args) {
PayFactory payFactory = new PayFactory();

  //获取支付宝支付
  Pay aLiPay = payFactory.getPay("ALiPay");
  //调用aLiPay中的支付、查询、退款等等  
  ResultCreatePay  result1 = aLiPay.CreatePay(new CreatePayParas());

  //获取微信支付   
  Pay weiXinPay = payFactory.getPay("WeiXinPay");
  //调用weiXinPay中的支付、查询、退款等等  
  ResultCreatePay  result1 = weiXinPay.CreatePay(new CreatePayParas());

}
}

原文链接:https://blog.csdn.net/sinat_16998945/article/details/107025375

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值