工廠模式,主要實現了建立者和調用者的分離。ide
分類:一、簡單工廠模式;二、工廠方法模式;三、抽象工廠模式。測試
核心:實例化對象時,用工廠方法代替new操做。spa
1、簡單工廠模式code
也叫靜態工廠模式,工廠類中實現靜態方法,根據入參,生產不一樣的產品,工程項目中經常使用。對象
工廠類做爲類使用,產品類做爲接口使用,具體產品實現接口,用來生產同一等級結構中的任意產品,當新增產品時,須要修改已有的代碼。blog
1 package com.corey.factory.simpleFactory; 2 3 /** 4 * 產品類接口 5 * @author Corey 6 * 7 */ 8 public interface Computer { 9 void calcData(); 10 }
1 package com.corey.factory.simpleFactory; 2 3 /** 4 * 具體產品實現接口 5 * @author Corey 6 * 7 */ 8 public class HighEndComputer implements Computer{ 9 10 @Override 11 public void calcData() { 12 System.out.println("高端電腦計算速度快!"); 13 } 14 15 }
1 package com.corey.factory.simpleFactory; 2 3 /** 4 * 具體產品實現接口 5 * @author Corey 6 * 7 */ 8 public class LowEndComputer implements Computer{ 9 10 @Override 11 public void calcData() { 12 System.out.println("低端電腦計算速度慢!"); 13 } 14 15 }
1 package com.corey.factory.simpleFactory; 2 3 /** 4 * 產品工廠類,生產同一等級結構中的任意產品 5 * @author Corey 6 * 7 */ 8 public class ComputerFactory { 9 10 public static Computer CreateComputer(String type){ 11 Computer computer = null; 12 13 switch(type){ 14 case "HighEnd": 15 computer = new HighEndComputer(); 16 break; 17 case "LowEnd": 18 computer = new LowEndComputer(); 19 break; 20 default: 21 break; 22 } 23 return computer; 24 } 25 }
1 package com.corey.factory.simpleFactory; 2 3 /** 4 * 測試簡單工廠模式 5 * @author Corey 6 * 7 */ 8 public class Test { 9 10 public static void main(String[] args) { 11 Computer c1 = ComputerFactory.CreateComputer("HighEnd"); 12 c1.calcData(); 13 14 Computer c2 = ComputerFactory.CreateComputer("LowEnd"); 15 c2.calcData(); 16 17 } 18 19 }
運行結果:
高端電腦計算速度快!
低端電腦計算速度慢!
2、工廠方法模式接口
工廠類做爲接口使用,固定產品工廠類實現接口,用來生產統一等級結構中的固定產品,可擴展任意產品。內存
1 package com.corey.factory.factoryMethed; 2 3 /** 4 * 產品工廠類做爲接口使用 5 * @author Corey 6 * 7 */ 8 public interface ComputerFactory { 9 10 Computer CreateComputer(); 11 }
1 package com.corey.factory.factoryMethed; 2 3 /** 4 * 具體產品工廠類實現接口 5 * @author Corey 6 * 7 */ 8 public class HighEndComputerFactory implements ComputerFactory{ 9 10 @Override 11 public Computer CreateComputer() { 12 return new HighEndComputer(); 13 } 14 15 }
1 package com.corey.factory.factoryMethed; 2 3 /** 4 * 具體產品工廠類實現接口 5 * @author Corey 6 * 7 */ 8 public class LowEndComputerFactory implements ComputerFactory{ 9 10 @Override 11 public Computer CreateComputer() { 12 return new LowEndComputer(); 13 } 14 15 }
1 package com.corey.factory.factoryMethed; 2 3 /** 4 * 測試工廠方法模式 5 * @author Corey 6 * 7 */ 8 public class Test { 9 10 public static void main(String[] args) { 11 Computer c1 = new HighEndComputerFactory().CreateComputer(); 12 c1.calcData(); 13 14 Computer c2 = new LowEndComputerFactory().CreateComputer(); 15 c2.calcData(); 16 17 } 18 19 }
3、抽象工廠模式產品
工廠類做爲接口使用,內含生產配件方法接口,產品配件類做爲接口使用,具體產品配件類實現接口,具體產品工廠類實現工廠類,用來生產不一樣產品族的所有產品,沒法增長新產品,可擴展產品族。it
1 package com.corey.factory.abstractFactory; 2 3 /** 4 * 工廠類做爲接口使用,內含組裝配件方法接口 5 * @author Corey 6 * 7 */ 8 public interface ComputerFactory { 9 MainBoard createMainBoard(); //生產主板配件 10 HardDisk createHardDisk(); //生產硬盤配件 11 Memory createMemory(); //生產內存配件 12 13 }
1 package com.corey.factory.abstractFactory; 2 3 /** 4 * 產品配件類做爲接口使用 5 * 6 * @author Corey 7 * 8 */ 9 public interface HardDisk { 10 void speed(); //硬盤實現方法 11 }
1 package com.corey.factory.abstractFactory; 2 3 /** 4 * 具體產品配件類實現接口 5 * @author Corey 6 * 7 */ 8 public class HighEndHardDisk implements HardDisk{ 9 10 @Override 11 public void speed() { 12 System.out.println("高端硬盤速度快!"); 13 } 14 15 }
1 package com.corey.factory.abstractFactory; 2 3 /** 4 * 具體產品工廠類實現工廠類,用來生產不一樣產品族的所有產品 5 * @author Corey 6 * 7 */ 8 public class HighEndComputerFactory implements ComputerFactory { 9 10 @Override 11 public MainBoard createMainBoard() { 12 return new HighEndMainBoard(); 13 } 14 15 @Override 16 public HardDisk createHardDisk() { 17 return new HighEndHardDisk(); 18 } 19 20 @Override 21 public Memory createMemory() { 22 return new HighEndMemory(); 23 } 24 25 }
1 package com.corey.factory.abstractFactory; 2 3 /** 4 * 測試抽象工廠模式 5 * @author Corey 6 * 7 */ 8 public class Test { 9 10 public static void main(String[] args) { 11 ComputerFactory h = new HighEndComputerFactory(); 12 HardDisk hHardDisk = h.createHardDisk(); 13 MainBoard hMainBoard = h.createMainBoard(); 14 Memory hMemory = h.createMemory(); 15 hHardDisk.speed(); 16 hMainBoard.speed(); 17 hMemory.speed(); 18 19 ComputerFactory l = new LowEndComputerFactory(); 20 HardDisk lHardDisk = l.createHardDisk(); 21 MainBoard lMainBoard = l.createMainBoard(); 22 Memory lMemory = l.createMemory(); 23 lHardDisk.speed(); 24 lMainBoard.speed(); 25 lMemory.speed(); 26 } 27 }
運行結果:
高端硬盤速度快!
高端主板速度快!
高端內存速度快!
低端硬盤速度慢!
低端主板速度慢!
低端內存速度慢!
工廠模式基本就這些內容。
另,簡單工廠還有另外一種實現方式,就是編寫不一樣的靜態方法建立不一樣的產品,而無需傳參,可是調用者須要知道更多的方法名。