1. 程式人生 > >12外觀模式Facade

12外觀模式Facade

clas 模塊 idt 結構 str 股票 pac 界面 abc

一、什麽是外觀模式

  Facade模式也叫外觀模式,是由GoF提出的 23種設計模式中的一種。Facade模式為一組具 有類似功能的類群,比如類庫,子系統等等,提供一個一致的簡單的界面。這個一致的簡單的界面被稱作facade。

二、外觀模式的結構

技術分享圖片

三、外觀模式的角色和職責

  Facade 為調用方定義簡單的調用接口。

  Clients 調用者。通過Facade接口調用提供某功能的內部類群。

  Packages 功能提供者。指提供功能的類群(模塊或子系統)。

A子系統

 1 /*
 2  * A子系統
 3  */
 4
public class SystemA { 5 /* 6 * A子系統實現功能 7 */ 8 public void doSomething() { 9 System.out.println("實現A子系統功能"); 10 } 11 }

B子系統

 1 /*
 2  * B子系統
 3  */
 4 public class SystemB {
 5 
 6     /*
 7      * B子系統實現功能
 8      */
 9     public void doSomething() {
10 System.out.println("實現B子系統功能"); 11 } 12 }

C子系統

 1 /*
 2  * C子系統
 3  */
 4 public class SystemC {
 5 
 6     /*
 7      * C子系統實現功能
 8      */
 9     public void doSomething() {
10         System.out.println("實現C子系統功能");
11     }
12 }

外觀

 1 //外觀
 2 public class Facade {
3 private SystemA systemA; 4 private SystemB systemB; 5 private SystemC systemC; 6 7 public Facade() { 8 systemA = new SystemA(); 9 systemB = new SystemB(); 10 systemC = new SystemC(); 11 } 12 13 public void doABC() { 14 this.systemA.doSomething(); 15 this.systemB.doSomething(); 16 this.systemC.doSomething(); 17 } 18 19 public void doAB() { 20 this.systemA.doSomething(); 21 this.systemB.doSomething(); 22 } 23 }

測試

1 public class MainClass {
2     public static void main(String[] args) {
3         Facade facade = new Facade();
4         facade.doABC();
5     }
6 }

1 public class MainClass2 {
2     public static void main(String[] args) {
3         Facade facade = new Facade();
4         facade.doAB();
5     }
6 }

====================================================================

國載

1 //國載
2 public class GuoZai {
3     
4     public void mai() {
5         System.out.println("買國債");
6     }
7 }

股票

1 //股票
2 public class Gupiao {
3     
4     public void mai() {
5         System.out.println("買股票");
6     }
7 }

期貨

1 //期貨
2 public class Qihuo {
3     
4     public void chao() {
5         System.out.println("買期貨");
6     }
7 }

基金

 1 //基金
 2 public class JiJin {
 3     private Gupiao gupiao;
 4     private GuoZai guozai;
 5     private Qihuo qihuo;
 6     
 7     public JiJin() {
 8         this.guozai = new GuoZai();
 9         this.gupiao = new Gupiao();
10         this.qihuo = new Qihuo();
11     }
12     
13     public void maiJijinA() {
14         this.guozai.mai();
15         this.gupiao.mai();
16     }
17     
18     public void maiJijinB() {
19         this.guozai.mai();
20         this.gupiao.mai();
21         this.qihuo.chao();
22     }
23 }

測試

 1 public class MainClass {
 2     public static void main(String[] args) {
 3 //        //80年代,基金出現之前
 4 //        Gupiao gupiao = new Gupiao();
 5 //        gupiao.mai();
 6 //        
 7 //        Qihuo qihuo = new Qihuo();
 8 //        qihuo.chao();
 9 //        
10 //        GuoZai guozhai = new GuoZai();
11 //        guozhai.mai();
12         //有了基金之後
13 //        JiJin jijin = new JiJin();
14 //        jijin.maiJijinA();
15         JiJin jijin = new JiJin();
16         jijin.maiJijinB();
17     }
18 }

12外觀模式Facade