工厂类
/**
* @Author: chuan.bai
* @Description 支付工厂
* @Date: Created on 10:58 2021/3/25
* @Modified By:
*/
public interface PayFactory {
void submitOrder();
void refundOrder();
void cancelOrder();
}
paypal实现
/**
* @Author: chuan.bai
* @Description paypal 支付工厂
* @Date: Created on 11:00 2021/3/25
* @Modified By:
*/
public class PayPalFactory implements PayFactory{
@Override
public void submitOrder() {
System.out.println("PayPalFactory.submitOrder");
}
@Override
public void refundOrder() {
System.out.println("PayPalFactory.refundOrder");
}
@Override
public void cancelOrder() {
System.out.println("PayPalFactory.cancelOrder");
}
}
amazon实现
/**
* @Author: chuan.bai
* @Description amazon 支付工厂
* @Date: Created on 11:01 2021/3/25
* @Modified By:
*/
public class AmazonPayFactory implements PayFactory{
@Override
public void submitOrder() {
System.out.println("AmazonPayFactory.submitOrder");
}
@Override
public void refundOrder() {
System.out.println("AmazonPayFactory.refundOrder");
}
@Override
public void cancelOrder() {
System.out.println("AmazonPayFactory.cancelOrder");
}
}
adyen实现
/**
* @Author: chuan.bai
* @Description adyen支付工厂
* @Date: Created on 11:02 2021/3/25
* @Modified By:
*/
public class AdyenPayFactory implements PayFactory{
@Override
public void submitOrder() {
System.out.println("AdyenPayFactory.submitOrder");
}
@Override
public void refundOrder() {
System.out.println("AdyenPayFactory.refundOrder");
}
@Override
public void cancelOrder() {
System.out.println("AdyenPayFactory.cancelOrder");
}
}
/**
* @Author: chuan.bai
* @Description 操作客户端
* @Date: Created on 11:03 2021/3/25
* @Modified By:
*/
public class PayClient {
private static List<String > paymethodList =new ArrayList<>();
static {
paymethodList.add("paypal");
paymethodList.add("amazon");
paymethodList.add("adyen");
}
public static void main(String[] args) {
//默认为paypal 支付
PayFactory payFactory = new PayPalFactory();
for (String paymethod : paymethodList) {
System.out.println("开始使用"+paymethod+"支付");
switch (paymethod){
case "adyen":
payFactory = new AdyenPayFactory();
break;
case "amazon":
payFactory = new AmazonPayFactory();
break;
case "paypal":
payFactory = new PayPalFactory();
break;
default:
payFactory = new PayPalFactory();
break;
}
//调用支付的 提交订单,取消订单,退款的方法
payFactory.submitOrder();
payFactory.cancelOrder();
payFactory.refundOrder();
}
}
}
输出结果
开始使用paypal支付
PayPalFactory.submitOrder
PayPalFactory.cancelOrder
PayPalFactory.refundOrder
开始使用amazon支付
AmazonPayFactory.submitOrder
AmazonPayFactory.cancelOrder
AmazonPayFactory.refundOrder
开始使用adyen支付
AdyenPayFactory.submitOrder
AdyenPayFactory.cancelOrder
AdyenPayFactory.refundOrder
Process finished with exit code 0