工厂模式

工厂模式

 

 工厂模式

 

 


 

工厂模式工厂模式工厂模式工厂模式工厂模式

 

 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

 

上一篇:新版二开cp盲盒小纸条月老小程序源


下一篇:上个厕所的功夫,搞懂MySQL事务隔离级别,Java基础视频