簡單工廠模式又叫靜態工廠方法模式,經過產品接口、具體產品類和工廠類實現。客戶端(使用具體產品類的類)不須要知道產品類的詳細信息,只須要知道產品類的對應參數,經過調用工廠類的靜態方法來建立對象。php
簡單工廠模式、工廠方法模式和抽象工廠模式是工廠模式的三種實現,工廠模式將對象建立和對象使用分離開來,將軟件解耦,使其知足單一職責原則。git
以Fruit做爲產品接口,Apple、Orange、Banana是具體產品類,Factory是工廠類,SimpleFactoryPattern是客戶端。github
簡單工廠模式的具體實現以下:thinkphp
1 public class SimpleFactoryPattern { 2 public static void main(String[] args){ 3 //不須要知道類的詳細信息,只須要向工廠方法傳遞一個與具體產品類對應的參數就能夠獲得對應的對象 4 Fruit fruit1=Factory.createFactory("apple"); 5 fruit1.eat(); 6 Fruit fruit2=Factory.createFactory("banana"); 7 fruit2.eat(); 8 Fruit fruit3=Factory.createFactory("orange"); 9 fruit3.eat(); 10 } 11 } 12 13 interface Fruit{ 14 public abstract void eat(); 15 } 16 17 class Apple implements Fruit{ 18 public void eat(){ 19 System.out.println("eat apple"); 20 } 21 } 22 class Orange implements Fruit{ 23 public void eat(){ 24 System.out.println("eat orange"); 25 } 26 } 27 class Banana implements Fruit{ 28 public void eat(){ 29 System.out.println("eat banana"); 30 } 31 } 32 class Factory{ 33 public static Fruit createFactory(String name){ 34 Fruit fruit=null; 35 if(name.equals("apple")){ 36 fruit=new Apple(); 37 } 38 else if(name.equals("orange")){ 39 fruit=new Orange(); 40 } 41 else if(name.equals("banana")){ 42 fruit=new Banana(); 43 } 44 return fruit; 45 } 46 }
1)將對象建立和對象使用分離開來,減弱了代碼的耦合性。app
2)提供了出new操做符以外的另一種建立對象的形式,雖然靜態工廠方法內部仍是使用new操做符建立對象。ide
3)客戶端無需知道所建立具體產品類的類名,只須要知道此類對應的參數便可,對於一些複雜的類,經過簡單工廠模式減小了使用者的記憶量。函數
1)靜態工廠方法包含邏輯判斷和建立對象兩個功能,違反了單一職責原則。ui
2)若是增長新的產品類,就須要在靜態工廠方法中添加代碼,違反了開閉原則(對擴展開發,對修改關閉)。spa
能夠經過反射機制避免當增長新產品類,在靜態工廠方法中添加代碼的狀況,使程序符合開閉原則。經過Class.forName(String className).newInstance()獲得某個類的實例,這裏只是調用類的默認構造函數(無參構造函數)建立對象,實現代碼以下:.net
1 /** 2 * Created by hfz on 2016/10/8. 3 */ 4 public class SimpleFactoryPattern { 5 public static void main(String[] args){ 6 //不須要知道類的詳細信息,只須要向工廠方法傳遞一個與具體產品類對應的參數就能夠獲得對應的對象,傳遞類名,經過反射機制建立對象 7 Fruit fruit1=Factory.createFactory("Apple"); 8 fruit1.eat(); 9 Fruit fruit2=Factory.createFactory("Banana"); 10 fruit2.eat(); 11 Fruit fruit3=Factory.createFactory("Orange"); 12 fruit3.eat(); 13 } 14 } 15 16 interface Fruit{ 17 public abstract void eat(); 18 } 19 20 class Apple implements Fruit{ 21 public void eat(){ 22 System.out.println("by reflection,eat apple"); 23 } 24 } 25 class Orange implements Fruit{ 26 public void eat(){ 27 System.out.println("by reflection,eat orange"); 28 } 29 } 30 class Banana implements Fruit{ 31 public void eat(){ 32 System.out.println("by reflection,eat banana"); 33 } 34 } 35 class Factory{ 36 public static Fruit createFactory (String name){ 37 Fruit fruit=null; 38 try { 39 fruit = (Fruit) Class.forName(name).newInstance(); 40 } 41 catch (ClassNotFoundException ex){ 42 System.out.println(); 43 } 44 catch (InstantiationException ex1){ 45 System.out.println(); 46 } 47 catch (IllegalAccessException ex2){ 48 System.out.println(); 49 } 50 return fruit; 51 } 52 }
靜態工廠方法負責建立全部具體產品類,一旦這個工廠方法出現問題,全部的客戶端(不一樣客戶端調用不一樣的具體產品類)都會受到牽連。
參考:
1)http://www.jasongj.com/design_pattern/simple_factory/
2)http://www.hollischuang.com/archives/1401
3)http://blog.csdn.net/fengyuzhengfan/article/details/38086743