抽象工厂模式(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();
}
}
执行结果:
跨境支付
微信支付
通过微信跨境支付
银联支付
通过银联跨境支付
支付宝支付
通过支付宝跨境支付