簡單介紹工廠模式(靜態工廠和抽象工廠)
阿新 • • 發佈:2018-12-02
註明 : 以下整理的資料來源於實驗樓網站.
工廠模式(Factory Pattern)的意義就跟它的名字一樣,在面向物件程式設計中,工廠通常是一個用來建立其他物件的物件。工廠模式根據不同的引數來實現不同的分配方案和建立物件。
靜態工廠最常見的應用場景就是java庫中的getInstance()方法會返回不同的Calendar物件
1靜態工廠
UML圖如下所示 : (建立一個人類工廠,根據需求建立對應的物件)
步驟如下 :
1建立一個Human介面
抽象工廠模式 : 針對的是多個產品的等級結構
1 /** 2 * @Author : zhuhuicong 3 * 2018/12/2 9:54 4 * @Version 1.0 5 */ 6 public interface Human { 7 void eat(); 8 void sleep(); 9 void beat(); 10 }View Code
2建立Male和FeMale實現類實現Human介面
1 /** 2 * @Author : zhuhuicong 3 * 2018/12/2 9:55 4 * @Version 1.0 5 */ 6View Codepublic class Female implements Human { 7 @Override 8 public void eat() { 9 System.out.println("Female eating ...."); 10 11 } 12 13 @Override 14 public void sleep() { 15 System.out.println("Female sleeping ...."); 16 17 } 18 19 @Override 20 public void beat() { 21 System.out.println("Female beating ....");22 23 } 24 }
1 /** 2 * @Author : zhuhuicong 3 * 2018/12/2 9:55 4 * @Version 1.0 5 */ 6 public class Male implements Human { 7 @Override 8 public void eat() { 9 System.out.println("Male eating ...."); 10 } 11 12 @Override 13 public void sleep() { 14 System.out.println("Male slieeping ...."); 15 16 } 17 18 @Override 19 public void beat() { 20 System.out.println("Male beating...."); 21 22 } 23 }View Code
3建立一個靜態工廠
1 public class ManyHumanFactory { 2 public static Male createMale(){ 3 return new Male(); 4 } 5 6 public static Female createFemlae(){ 7 return new Female(); 8 } 9 }View Code
4測試類:
1 public class ManyFacotoryTest { 2 public static void main(String[] args) { 3 Male male = ManyHumanFactory.createMale(); 4 male.eat(); 5 Female femlae = ManyHumanFactory.createFemlae(); 6 femlae.eat(); 7 } 8 } 9 10 Result : 11 Male eating .... 12 Female eating ....View Code
2 抽象工廠模式 (Abstract Factory Pattern) ----抽象工廠是一個父類的工廠(可以建立其它的工廠類 alias(工廠的工廠)) 可以將具有同一主題的單獨的工廠封裝起來.在工廠模式之上增加一層抽象的概念. 抽象工廠特別適合這樣一種產品結構,產品分為幾個系列,在每一個系列中,產品的佈局都是類似的. UML圖: 步驟如下: 1建立餐具和食物的介面:
1 /** 2 * @Author : zhuhuicong 3 * 2018/12/2 10:34 4 * @Version 1.0 5 */ 6 public interface TableWare { 7 String getToolName(); 8 }View Code
1 /** 2 * @Author : zhuhuicong 3 * 2018/12/2 10:33 4 * @Version 1.0 5 */ 6 public interface Food { 7 String getFoodName(); 8 }View Code
2建立餐具的實現類Knife和Cup:
1 /** 2 * @Author : zhuhuicong 3 * 2018/12/2 10:39 4 * @Version 1.0 5 */ 6 public class Knife implements TableWare { 7 @Override 8 public String getToolName() { 9 return "knife"; 10 } 11 } 12 13 14 public class Cup implements TableWare { 15 @Override 16 public String getToolName() { 17 return "cup"; 18 } 19 }View Code
3建立食物的實現類Milk和Apple
1 /** 2 * @Author : zhuhuicong 3 * 2018/12/2 10:38 4 * @Version 1.0 5 */ 6 public class Milk implements Food { 7 @Override 8 public String getFoodName() { 9 return "milk"; 10 } 11 } 12 13 public class Apple implements Food{ 14 @Override 15 public String getFoodName() { 16 return "apple"; 17 } 18 }View Code
4建立廚房的頂級介面
1 /** 2 * @Author : zhuhuicong 3 * 2018/12/2 10:36 4 * @Version 1.0 5 */ 6 public interface KitchenFactory { 7 Food getFood(); 8 TableWare getTableWare(); 9 }View Code
5根據自己的需求建立廚房實現類(實現廚房的頂級介面
1 /** 2 * @Author : zhuhuicong 3 * 2018/12/2 10:41 4 * @Version 1.0 5 */ 6 public class AKitchen implements KitchenFactory { 7 @Override 8 public Food getFood() { 9 return new Apple(); 10 } 11 12 @Override 13 public TableWare getTableWare() { 14 return new Knife(); 15 } 16 }View Code
6建立頂級的吃貨類,根據自己的需求傳入廚房(含測試)
1 /** 2 * @Author : zhuhuicong 3 * 2018/12/2 10:43 4 * @Version 1.0 5 */ 6 public class Foodahollc { 7 public static void eat(KitchenFactory k){ 8 System.out.println("A foodaholic id eating " + k.getFood().getFoodName()+ 9 " with "+k.getTableWare().getToolName()); 10 } 11 public static void main(String[] args) { 12 AKitchen aKitchen = new AKitchen(); 13 Foodahollc.eat(aKitchen); 14 } 15 }View Code 最後簡單總結一下兩種工廠模式的區別 : 工廠方法模式 : 針對的是一個產品的等級結構
抽象工廠模式 : 針對的是多個產品的等級結構