工廠模式

簡單工廠模式

1. 目的 
        工廠模式就是專門負責將大量有共同接口的類實例化,而且不必事先知道每次是要實例化哪一個類的模式。它定義一個用於創建對象的接口,由子類決定實例化哪一個類。
2 . 簡單工廠模式的結構 
 http://zhupan.iteye.com/upload/picture/pic/2305/0e1d5f00-1c49-4e5f-9c2c-986d989217bd.gif

3. 一個簡單例子
java 代碼
  1. // 產品接口         
  2. public interface Product {   
  3.   
  4.     public void getName();   
  5.   
  6. }   
  7.   
  8. // 具體產品A   
  9. public class ProductA implements Product {   
  10.   
  11.     public void getName() {   
  12.         System.out.println("  I am ProductA  ");   
  13.     }   
  14.   
  15. }   
  16.   
  17. // 具體產品B   
  18. public class ProductB implements Product {   
  19.   
  20.     public void getName() {   
  21.         System.out.println("  I am ProductB  ");   
  22.     }   
  23.   
  24. }   
  25.   
  26. // 工廠類   
  27. public class ProductCreator {   
  28.   
  29.     public Product createProduct(String type) {   
  30.         if (" A ".equals(type)) {   
  31.             return new ProductA();   
  32.         }   
  33.         if (" B ".equals(type)) {   
  34.             return new ProductB();   
  35.         } else  
  36.             return null;   
  37.     }   
  38.   
  39.     public static void main(String[] args) {   
  40.         ProductCreator creator = new ProductCreator();   
  41.         creator.createProduct(" A ").getName();   
  42.         creator.createProduct(" B ").getName();   
  43.     }   
  44. }  
4. 小結工廠模式的適用範圍 
• 在編碼時不能預見需要創建哪一種類的實例。 
• 一個類使用它的子類來創建對象。 
• 開發人員不希望創建了哪個類的實例以及如何創建實例的信息暴露給外部程序。  

 

抽象工廠模式 

1. 抽象工廠模式可以說是簡單工廠模式的擴展,它們主要的區別在於需要創建對象的複雜程度上。 
在抽象工廠模式中,抽象產品可能是一個或多個,從而構成一個或多個產品族。 在只有一個產品族的情況下,抽象工廠模式實際上退化到工廠方法模式。 
2. 抽象工廠模式的結構 

 http://zhupan.iteye.com/upload/picture/pic/2304/aa2dfa0b-cd37-4bfc-a411-dd75ee60ab5f.gif

3. 一個簡單例子

java 代碼
  1. //  產品 Plant接口         
  2. public interface Plant {   
  3. }   
  4.   
  5. // 具體產品PlantA,PlantB   
  6. public class PlantA implements Plant {   
  7.   
  8.     public PlantA() {   
  9.         System.out.println(" create PlantA ! ");   
  10.     }   
  11.   
  12.     public void doSomething() {   
  13.         System.out.println("  PlantA do something  ");   
  14.     }   
  15. }   
  16.   
  17. public class PlantB implements Plant {   
  18.     public PlantB() {   
  19.         System.out.println(" create PlantB ! ");   
  20.     }   
  21.   
  22.     public void doSomething() {   
  23.         System.out.println("  PlantB do something  ");   
  24.     }   
  25. }   
  26.   
  27. // 產品 Fruit接口   
  28. public interface Fruit {   
  29. }   
  30.   
  31. // 具體產品FruitA,FruitB   
  32. public class FruitA implements Fruit {   
  33.     public FruitA() {   
  34.         System.out.println(" create FruitA ! ");   
  35.     }   
  36.   
  37.     public void doSomething() {   
  38.         System.out.println("  FruitA do something  ");   
  39.     }   
  40. }   
  41.   
  42. public class FruitB implements Fruit {   
  43.     public FruitB() {   
  44.         System.out.println(" create FruitB ! ");   
  45.     }   
  46.   
  47.     public void doSomething() {   
  48.         System.out.println("  FruitB do something  ");   
  49.     }   
  50. }   
  51.   
  52. // 抽象工廠方法   
  53. public interface AbstractFactory {   
  54.     public Plant createPlant();   
  55.   
  56.     public Fruit createFruit();   
  57. }   
  58.   
  59. // 具體工廠方法   
  60. public class FactoryA implements AbstractFactory {   
  61.     public Plant createPlant() {   
  62.         return new PlantA();   
  63.     }   
  64.   
  65.     public Fruit createFruit() {   
  66.         return new FruitA();   
  67.     }   
  68. }   
  69.   
  70. public class FactoryB implements AbstractFactory {   
  71.     public Plant createPlant() {   
  72.         return new PlantB();   
  73.     }   
  74.   
  75.     public Fruit createFruit() {   
  76.         return new FruitB();   
  77.     }   
  78. }  
4. 小結 
在以下情況下,應當考慮使用抽象工廠模式。 
  首先,一個系統應當不依賴於產品類實例被創立,組成,和表示的細節。這對於所有形態的工廠模式都是重要的。 
  其次,這個系統的產品有多於一個的產品族。 
  第三,同屬於同一個產品族的產品是設計成在一起使用的。這一約束必須得在系統的設計中體現出來。 
  最後,不同的產品以一系列的接口的面貌出現,從而使系統不依賴於接口實現的細節。 
  其中第二丶第三個條件是我們選用抽象工廠模式而非其它形態的工廠模式的關鍵性條件。

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