原廠模式(Factory Pattern),factorypattern

來源:互聯網
上載者:User

原廠模式(Factory Pattern),factorypattern

一、原廠模式(Factory Pattern)的介紹

  原廠模式是我們最常用的執行個體化對象模式了,是用Factory 方法代替new操作的一種模式。在原廠模式中,我們在建立對象時不會對用戶端暴露建立邏輯,並且是通過使用一個共同的介面來指向新建立的對象。使用原廠模式可能會多做一些工作,但會給你系統帶來更大的可擴充性和盡量少的修改量。

二、原廠模式的分類

  1.簡單原廠模式

  2.Factory 方法模式

  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、比較普通建立對象的方法和測試簡單Factory 方法

 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     @Test17     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.小結簡單工廠

  簡單工廠的所有產品都是在一個工廠生產的,通過在構造時判斷傳入的參數不同來生產不同的產品,這種模式會隨著產品的增加而越來越龐大,每次新增產品都要添加修改判斷條件,即修改工廠類,給維護和擴充帶來不便。並且一但工廠類中出錯,所有產品都無法建立了。

四、Factory 方法模式

  1.抽象產品工廠類

  Factory 方法模式的核心,它與應用程式無關。是具體工廠角色必須實現的介面或者必須繼承的父類。在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     @Override17     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  * 具體產品B14  */15 public class ProductB implements Product {16     @Override17     public void method() {18         System.out.println("------ProductB------");19     }20 }

  5.測試Factory 方法

 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.小結Factory 方法

  Factory 方法模式是簡單原廠模式的進一步抽象化和推廣,Factory 方法模式裡不再只由一個工廠類決定那一個產品類應當被執行個體化,這個決定被交給抽象工廠的子類去做。Factory 方法模式實現了不同產品在不同工廠生產,維護和擴充比簡單原廠模式好,即使一個產品出現問題,其他產品也可以正常生產。

三、抽象原廠模式

  //todo 未完待續

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.