2021-03-15

抽象工厂模式(Abstract factory pattern)
代码示例:

public class KJPay implements KJIPay {
    @Override
    public void KJPay() {
        System.out.println("跨境支付");
    }
}
public class WXPay implements IPay {
    @Override
    public void pay() {
        System.out.println("微信支付");
    }
}
public class YLPay implements IPay {
    @Override
    public void pay() {
        System.out.println("银联支付");
    }
}
public class ZFBPay implements IPay {
    @Override
    public void pay() {
        System.out.println("支付宝支付");
    }
}
public abstract class IPayFactory {
    public abstract IPay createIpay();
    public abstract KJIPay createKJIpay();
}
public class KJPayFactory extends IPayFactory {
    @Override
    public IPay createIpay() {
        return null;
    }
    @Override
    public KJIPay createKJIpay() {
        return new KJPay();
    }
}
public class WXPayFactory extends IPayFactory {
    @Override
    public IPay createIpay() {
        return new WXPay();
    }
    @Override
    public KJIPay createKJIpay() {
        return new KJIPay() {
            @Override
            public void KJPay() {
                System.out.println("通过微信跨境支付");
            }
        };
    }
}
public class YLPayFactory extends IPayFactory {
    @Override
    public IPay createIpay() {
        return new YLPay();
    }
    @Override
    public KJIPay createKJIpay() {
        return new KJIPay() {
            @Override
            public void KJPay() {
                System.out.println("通过银联跨境支付");
            }
        };
    }
}
public class ZFBPayFactory extends IPayFactory {
    @Override
    public IPay createIpay() {
        return new ZFBPay();
    }
    @Override
    public KJIPay createKJIpay() {
        return new KJIPay() {
            @Override
            public void KJPay() {
                System.out.println("通过支付宝跨境支付");
            }
        };
    }
}
public interface IPay {
    void pay();
}
public interface KJIPay {
    void KJPay();
}
public class Test {
    public static void main(String[] args) {
        KJPayFactory factory = new KJPayFactory();
        factory.createKJIpay().KJPay();
        WXPayFactory wxPayFactory = new WXPayFactory();
        wxPayFactory.createIpay().pay();
        wxPayFactory.createKJIpay().KJPay();
        YLPayFactory ylPayFactory = new YLPayFactory();
        ylPayFactory.createIpay().pay();
        ylPayFactory.createKJIpay().KJPay();
        ZFBPayFactory zfbPayFactory = new ZFBPayFactory();
        zfbPayFactory.createIpay().pay();
        zfbPayFactory.createKJIpay().KJPay();
    }
}

在这里插入图片描述
执行结果:

跨境支付
微信支付
通过微信跨境支付
银联支付
通过银联跨境支付
支付宝支付
通过支付宝跨境支付
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值