JAVA設計模式-Facade

Facade模式翻譯成零售商模式我想更加容易理解。當你想要買一塊香皂的的時候,你肯定不會去香皂廠買,而是去超市買,同樣,買衣服買手機都不會去廠家,而是去零售商家。我想用代碼來解釋會更加容易:

   

    首先是不用門面模式來買這些東西:

 

   

  1. class CoatFactory{  
  2.     class Coat{}  
  3.     public Coat saleCoat(){  
  4.         return new Coat();  
  5.     }  
  6. }  
  7.   
  8. class ComputerFactory{  
  9.     class Computer{}  
  10.     public Computer saleComputer(){  
  11.         return new Computer();  
  12.     }  
  13. }  
  14.   
  15. class MobileFactory{  
  16.     class Mobile{}  
  17.     public Mobile saleMobile(){  
  18.         return new Mobile();  
  19.     }  
  20. }  
  21.   
  22. public class Test{  
  23.     public static void main(String[] args){  
  24.         //買衣服   
  25.         CoatFactory coatFactory = new CoatFactory();  
  26.         System.out.println(coatFactory.saleCoat().getClass().getName());  
  27.         //買電腦   
  28.         ComputerFactory computerFactory = new ComputerFactory();  
  29.         System.out.println(computerFactory.saleComputer().getClass().getName());  
  30.         //買手機   
  31.         MobileFactory mobileFactory = new MobileFactory();  
  32.         System.out.println(mobileFactory.saleMobile().getClass().getName());  
  33.   
  34.     }  
  35. }  

 

    上面的代碼裏面,我們必須直接和生產者打交道,這樣會很麻煩,我們需要一個統一的接口,可以得到所有這些東西,那麼就用到了facade模式,下面就是用fadace模式實現的代碼:

 

   

  1. package facade;  
  2.   
  3. class Coat{}  
  4. class Computer{}  
  5. class Mobile{}  
  6.   
  7. class CoatFactory{    
  8.     public Coat saleCoat(){  
  9.         return new Coat();  
  10.     }  
  11. }  
  12.   
  13. class ComputerFactory{  
  14.     public Computer saleComputer(){  
  15.         return new Computer();  
  16.     }  
  17. }  
  18.   
  19. class MobileFactory{      
  20.     public Mobile saleMobile(){  
  21.         return new Mobile();  
  22.     }  
  23. }  
  24.   
  25. class Store  
  26. {  
  27.     public Coat saleCoat()  
  28.     {  
  29.         CoatFactory coatFactory = new CoatFactory();  
  30.         return coatFactory.saleCoat();  
  31.     }  
  32.     public Computer saleComputer()  
  33.     {  
  34.         ComputerFactory computerFactory = new ComputerFactory();  
  35.         return computerFactory.saleComputer();  
  36.     }  
  37.     public Mobile saleMobile()  
  38.     {  
  39.         MobileFactory mobileFactory = new MobileFactory();  
  40.         return mobileFactory.saleMobile();  
  41.     }  
  42. }  
  43.   
  44. public class Test{  
  45.     public static void main(String[] args){  
  46.         Store store =new Store();  
  47.         //買衣服   
  48.         System.out.println(store.saleCoat().getClass().getName());  
  49.         //買電腦   
  50.         System.out.println(store.saleComputer().getClass().getName());  
  51.         //買手機   
  52.         System.out.println(store.saleMobile().getClass().getName());  
  53.     }  
  54. }  

 

    在這裏,我們想要得到各種商品,只需要和Store這個類打交道就行了,不需要直接和廠家聯繫,降低了客戶端代碼和生產者之間的耦合,當生產者發生變化的時候,只需要更改Store這個類,而不必改客戶端。舉一個大家可能會經常用到的例子吧,數據庫連接,現在數據庫廠商非常多,比如oracle,sqlserver,mysql等等,當我們想要一個數據庫連接對象的時候,需要進行一系列操作,客戶端希望告訴一個類用戶名,密碼,url和數據庫廠商,就直接返回一個數據庫連接對象,那麼就可以使用fadace模式,這個模式中的Store的形式如下:

 

   

  1. class DatabaseSeller{  
  2.     public Oracle getOracleObject(String username,String password,String url){}  
  3.     public SqlServer getSqlServerObject(String username,String password,String url){}  
  4.     public MySql getMySqlObject(String username,String password,String url){}  
  5. }  

 

    這樣做的好處就是隱藏了數據庫連接操作的細節,用戶只關心自己的業務邏輯就行了,數據庫連接這些與業務邏輯無關的操作就讓其它類來做就行了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章