一、工廠模式(Factory Pattern)的介紹 工廠模式是我們最常用的實例化對象模式了,是用工廠方法代替new操作的一種模式。在工廠模式中,我們在創建對象時不會對客戶端暴露創建邏輯,並且是通過使用一個共同的介面來指向新創建的對象。使用工廠模式可能會多做一些工作,但會給你系統帶來更大的可擴展性和 ...
一、工廠模式(Factory Pattern)的介紹
工廠模式是我們最常用的實例化對象模式了,是用工廠方法代替new操作的一種模式。在工廠模式中,我們在創建對象時不會對客戶端暴露創建邏輯,並且是通過使用一個共同的介面來指向新創建的對象。使用工廠模式可能會多做一些工作,但會給你系統帶來更大的可擴展性和儘量少的修改量。
二、工廠模式的分類
1.簡單工廠模式
2.工廠方法模式
3.抽象工廠模式
三、簡單工廠模式
1.父類產品
1 /** 2 * 父類產品 3 */ 4 public class Product { 5 public Product(){ 6 System.out.println("------product------"); 7 } 8 public void change(){ 9 System.out.println("-----父類方法------"); 10 } 11 }
· 2. A B C 三種子類產品
1 //子類產品A 2 public class ProductA extends Product { 3 public ProductA() { 4 System.out.println("------產品A------"); 5 } 6 @Override 7 public void change() { 8 System.out.println("------方法A------"); 9 } 10 }
1 //子類產品B 2 public class ProductB extends Product { 3 public ProductB() { 4 System.out.println("------產品B------"); 5 } 6 7 @Override 8 public void change() { 9 System.out.println("------方法B------"); 10 } 11 }
1 //子類產品C 2 public class ProductC extends Product { 3 public ProductC() { 4 System.out.println("------產品C------"); 5 } 6 7 @Override 8 public void change() { 9 System.out.println("------方法C------"); 10 } 11 }
3、創建產品的工廠類
工廠類是整個模式的關鍵.包含了必要的邏輯判斷,根據外界給定的信息,決定究竟應該創建哪個具體類的對象。
1 /** 2 * 創建產品的工廠 3 */ 4 public class Factory { 5 6 /** 7 * 靜態方法,直接調用 8 * 自動向上轉型,轉為product 9 */ 10 public static Product createProduct(String param){ 11 if (param.equals("A")){ 12 Product productA = new ProductA(); 13 return productA; 14 }else if (param.equals("B")){ 15 Product productB = new ProductB(); 16 return productB; 17 }else if (param.equals("C")){ 18 ProductC productC = new ProductC(); 19 return productC; 20 } 21 return new Product(); 22 } 23 24 public static void change(Product product){ 25 product.change(); 26 } 27 }
4、比較普通創建對象的方法和測試簡單工廠方法
1 public class TestProduct { 2 @Test 3 public void test1(){ 4 /** 5 * 沒有工廠模式 6 * 每個產品都需要創建 7 */ 8 ProductA productA = new ProductA(); 9 ProductB productB = new ProductB(); 10 ProductC productC = new ProductC(); 11 System.out.println("------productA------" + productA); 12 System.out.println("------productB------" + productB); 13 System.out.println("------productC------" + productC); 14 } 15 16 @Test 17 public void test2(){ 18 /** 19 * 工廠設計 20 * 參數不同,返回產品不同 21 * 向下轉型,和本身的類型相關 22 */ 23 ProductA productA = (ProductA) Factory.createProduct("A"); 24 ProductB productB = (ProductB) Factory.createProduct("B"); 25 ProductC productC = (ProductC) Factory.createProduct("C"); 26 System.out.println("------productA------" + productA); 27 System.out.println("------productB------" + productB); 28 System.out.println("------productC------" + productC); 29 // 多態測試 30 Factory.change(productA); 31 Factory.change(productB); 32 Factory.change(productC); 33 } 34 }
5.小結簡單工廠
簡單工廠的所有產品都是在一個工廠生產的,通過在構造時判斷傳入的參數不同來生產不同的產品,這種模式會隨著產品的增加而越來越龐大,每次新增產品都要添加修改判斷條件,即修改工廠類,給維護和擴展帶來不便。並且一但工廠類中出錯,所有產品都無法創建了。
四、工廠方法模式
1.抽象產品工廠類
工廠方法模式的核心,它與應用程式無關。是具體工廠角色必須實現的介面或者必須繼承的父類。在java中它由抽象類或者介面來實現。
1 /** 2 * 抽象產品工廠類 3 * Product為抽象產品 4 */ 5 public abstract class Factory { 6 abstract Product create(); 7 }
2.具體的A、B工廠類
2個工廠都繼承抽象工廠類,它含有和具體業務邏輯有關的代碼。由應用程式調用以創建對應的具體產品的對象。
1 /** 2 * A工廠生產A產品 3 */ 4 public class AFactory extends Factory { 5 @Override 6 public Product create() { 7 return new ProductA(); 8 } 9 } 10 11 12 /** 13 * B工廠生產B產品 14 */ 15 public class BFactory extends Factory { 16 @Override 17 Product create() { 18 return new ProductB(); 19 } 20 }
3.抽象產品介面
它是具體產品繼承的父類或者是實現的介面。在java中一般有抽象類或者介面來實現。
1 /** 2 * 抽象產品介面 3 */ 4 public interface Product { 5 void method(); 6 }
4.具體產品類
A、B 產品都實現抽象產品介面
1 /** 2 * 具體產品A 3 */ 4 public class ProductA implements Product{ 5 @Override 6 public void method() { 7 System.out.println("------ProductA------"); 8 } 9 } 10 11 12 /** 13 * 具體產品B 14 */ 15 public class ProductB implements Product { 16 @Override 17 public void method() { 18 System.out.println("------ProductB------"); 19 } 20 }
5.測試工廠方法
1 public class TestFactoryMethod { 2 /** 3 * 創建不同的工廠,生產不同的產品 4 * @param args 5 */ 6 public static void main(String[] args) { 7 AFactory aFactory = new AFactory(); 8 Product product = aFactory.create(); 9 product.method(); 10 11 BFactory bFactory = new BFactory(); 12 Product product1 = bFactory.create(); 13 product1.method(); 14 /* 輸出為: 15 ------ProductA------ 16 ------ProductB------*/ 17 } 18 }
6.小結工廠方法
工廠方法模式是簡單工廠模式的進一步抽象化和推廣,工廠方法模式里不再只由一個工廠類決定那一個產品類應當被實例化,這個決定被交給抽象工廠的子類去做。工廠方法模式實現了不同產品在不同工廠生產,維護和擴展比簡單工廠模式好,即使一個產品出現問題,其他產品也可以正常生產。
五、抽象工廠模式
1.抽象工廠類
1 /** 2 * 抽象工廠類 3 * 可以創建分別生產A、B產品 4 */ 5 public interface AbstractFactory { 6 7 ProductA factoryA(); 8 9 ProductB factoryB(); 10 }
2.具體工廠
A1和B1是一個產品族,由具體工廠1生產;A2和B2是一個產品族,由具體工廠2生產
1 /** 2 * 具體工廠1 3 * 生產產品A1和B1 4 */ 5 public class Factory1 implements AbstractFactory { 6 @Override 7 public ProductA factoryA() { 8 return new ProductA1(); 9 } 10 11 @Override 12 public ProductB factoryB() { 13 return new ProductB1(); 14 } 15 }
1 /** 2 * 具體工廠2 3 * 生產產品A2和B2 4 */ 5 public class Factory2 implements AbstractFactory { 6 @Override 7 public ProductA factoryA() { 8 return new ProductA2(); 9 } 10 11 @Override 12 public ProductB factoryB() { 13 return new ProductB2(); 14 } 15 }
3.抽象產品介面
抽象產品A介面
1 /** 2 * 抽象產品A介面 3 */ 4 public interface ProductA { 5 }
抽象產品B介面
1 /** 2 * 抽象產品B介面 3 */ 4 public interface ProductB { 5 }
4.具體產品
具體產品A1,實現抽象產品A介面
1 /** 2 * 具體產品A1 3 */ 4 public class ProductA1 implements ProductA { 5 public ProductA1(){ 6 System.out.println("------ProductA1------"); 7 } 8 }
具體產品A2,實現抽象產品A介面
1 /** 2 * 具體產品A2 3 */ 4 public class ProductA2 implements ProductA { 5 public ProductA2() { 6 System.out.println("------ProductA2------"); 7 } 8 }
具體產品B1,實現抽象產品B介面
1 /** 2 * 具體產品B1 3 */ 4 public class ProductB1 implements ProductB { 5 public ProductB1() { 6 System.out.println("------ProductB1------"); 7 } 8 }
具體產品B2,實現抽象產品B介面
1 /** 2 * 具體產品B2 3 */ 4 public class ProductB2 implements ProductB { 5 public ProductB2() { 6 System.out.println("------ProductB2------"); 7 } 8 }
5.測試抽象工廠
1 /** 2 * 測試抽象功能工廠 3 */ 4 public class TestAbstractFactory { 5 public static void main(String[] args) { 6 Factory1 factory1 = new Factory1(); 7 factory1.factoryA(); 8 factory1.factoryB(); 9 10 Factory2 factory2 = new Factory2(); 11 factory2.factoryA(); 12 factory2.factoryB(); 13 /* 輸出 14 ------ProductA1------ 15 ------ProductB1------ 16 ------ProductA2------ 17 ------ProductB2------*/ 18 } 19 }
6.小結
抽象工廠模式提供兩個具體工廠角色,分別對應於這兩個具體產品角色,每一個具體工廠角色只負責某一個產品角色的實例化。每一個具體工廠類只負責創建抽象產品的某一個具體子類的實例。
六、總結
簡單工廠 : 用來生產同一等級結構中的任意產品,即所有產品由一個工廠生產,當需要添加新產品時,需要修改工廠類,不易擴展。
工廠方法 :用來生產同一等級結構中的固定產品,即不同的產品由不同的工廠生產,添加新產品只需要添加新工廠,更容易擴展,不過如果同一級產品有多個,會產生很多平行的工廠。
抽象工廠 :用來生產不同產品族的全部產品,即一個工廠生產一個產品族,每個產品族都是多個產品的同一級結構。
以上三種工廠 方法在等級結構和產品族這兩個方向上的支持程度不同。所以要根據情況考慮應該使用哪種方法。