Pay.java
1 package factory; 2 3 public interface Pay { 4 5 void unifiedorder(); 6 7 }View Code
WeChatPay.java
1 package factory; 2 3 public class WechatPay implements Pay { 4 5 public void unifiedorder() { 6 System.out.println("微信支付 统一下单接口"); 7 } 8 }View Code
AliPay.java:
1 package factory; 2 3 public class AliPay implements Pay { 4 5 public void unifiedorder() { 6 System.out.println("支付宝支付 统一下单接口"); 7 } 8 9 }View Code
SimplePayFactory.java:
1 package factory; 2 3 public class SimplePayFactory { 4 5 public static Pay createPay(String payType){ 6 if (payType == null) { 7 return null; 8 }else if(payType.equalsIgnoreCase("WECHAT PAY")) { 9 return new WechatPay(); 10 }else if(payType.equalsIgnoreCase("Ali PAY")) { 11 return new AliPay(); 12 } 13 14 return null; 15 } 16 }View Code
Main.java:
1 package factory; 2 3 public class Main { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 8 Pay pay = SimplePayFactory.createPay("WECHAT PAY"); 9 10 Pay pay2 = SimplePayFactory.createPay("Ali PAY"); 11 12 pay.unifiedorder(); 13 14 pay2.unifiedorder(); 15 16 17 } 18 19 }View Code