先說下工廠模式的特性
1.對於調用者來說,影藏了復雜的邏輯處理過程,調用者只關心執行結果。
2.工廠要對結果負責,保證生產出符合規范的產品。
Git代碼地址 https://github.com/wujiachengSH/WjcFactoryDemo
下述的3個栗子分別為簡單工廠,工廠方法,抽象工廠
先來個栗子看看什么是工廠把
首先是簡單工廠模式
聲明一個動物工廠
1 package com.wjc.Factory; 2 3 public interface Animal { 4 5 String eat(); 6 7 String dirnk(); 8 9 String run(); 10 11 }
來2個實現類
1 package com.wjc.Factory; 2 3 public class Elephant implements Animal { 4 5 @Override 6 public String eat() { 7 // TODO Auto-generated method stub 8 return "Elephant e"; 9 } 10 11 @Override 12 public String dirnk() { 13 // TODO Auto-generated method stub 14 return "Elephant d"; 15 } 16 17 @Override 18 public String run() { 19 // TODO Auto-generated method stub 20 return "Elephant r"; 21 } 22 23 }
1 package com.wjc.Factory; 2 3 public class Leopard implements Animal { 4 5 @Override 6 public String eat() { 7 // TODO Auto-generated method stub 8 return "Leopard e"; 9 } 10 11 @Override 12 public String dirnk() { 13 // TODO Auto-generated method stub 14 return "Leopard d"; 15 } 16 17 @Override 18 public String run() { 19 // TODO Auto-generated method stub 20 return "Leopard r"; 21 } 22 23 24 25 }
然后我們來定義一個工廠
1 package com.wjc.Factory.simple; 2 3 import com.wjc.Factory.Animal; 4 import com.wjc.Factory.Elephant; 5 import com.wjc.Factory.Leopard; 6 7 public class SimpleFactory { 8 9 public Animal getAnimal(String name) { 10 if ("Elephant".equals(name)) { 11 return new Elephant(); 12 }else if ("Leopard".equals(name)) { 13 return new Leopard(); 14 }else { 15 return null; 16 } 17 } 18 19 }
測試一下這段代碼
1 package com.wjc.Factory.simple; 2 3 import com.wjc.Factory.Animal; 4 5 public class Test { 6 7 public static void main(String[] args) { 8 SimpleFactory simpleFactory = new SimpleFactory(); 9 Animal animal = simpleFactory.getAnimal("Leopard"); 10 System.out.println(animal.eat()); 11 12 } 13 }
可以看到工廠模式的意義在於,當需要使用對象的時候,不再通過New對象的方式拿取對象實例,而是通過工廠來獲取對象
通過工廠來聲明Bean最大的好處就在於
可以在Bean工廠中控制Bean是單例的?原型模式的?被代理的等等等。
不過上述簡單工廠能力過於強大,一個工廠竟然可以生產多種動物,顯然不符合原理。我們來看正宗的工廠
工廠模式代碼
1.聲明一個工廠接口
1 package com.wjc.Factory.func; 2 3 import com.wjc.Factory.Animal; 4 5 public interface Factory { 6 7 Animal getAnimal(); 8 9 }
2.分別實現工廠接口
1 package com.wjc.Factory.func; 2 3 import com.wjc.Factory.Animal; 4 import com.wjc.Factory.Elephant; 5 6 public class ElephantFactory implements Factory { 7 8 @Override 9 public Animal getAnimal() { 10 // TODO Auto-generated method stub 11 return new Elephant(); 12 } 13 14 }
1 package com.wjc.Factory.func; 2 3 4 import com.wjc.Factory.Animal; 5 import com.wjc.Factory.Leopard; 6 7 public class LeopardFactory implements Factory { 8 9 //來個單例工廠好了 10 private static class LeopardBean { 11 private static final Leopard INSTANCE = new Leopard(); 12 } 13 14 15 @Override 16 public Animal getAnimal() { 17 // TODO Auto-generated method stub 18 return LeopardBean.INSTANCE; 19 } 20 21 }
3.通過工廠生成Bean
1 package com.wjc.Factory.func; 2 3 public class Test { 4 5 public static void main(String[] args) { 6 ElephantFactory elephantFactory = new ElephantFactory(); 7 System.out.println(elephantFactory.getAnimal().eat()); 8 9 LeopardFactory leopardFactory = new LeopardFactory(); 10 System.out.println(leopardFactory.getAnimal().eat()); 11 12 } 13 14 }
可以看到標准的Bean工廠,可以在工廠中聲明和配置Bean對象的實現特性,甚至可以把上一篇代理模式使用進去。
https://www.cnblogs.com/wujc/p/10554933.html
但是上述代碼還是存在着一個問題,工廠很多的情況下,獲取實例其實並不方便,我們再進行進一步的封裝,來靠近IOC
我們來定義一個默認工廠,調用剛才封裝的幾個工廠
先寫一個抽象方法
1 package com.wjc.Factory.abstra; 2 3 import com.wjc.Factory.Animal; 4 import com.wjc.Factory.func.ElephantFactory; 5 import com.wjc.Factory.func.LeopardFactory; 6 7 public abstract class AbstarctFactory { 8 9 protected abstract Animal getAnimal(); 10 11 public Animal getAnimal(String name) { 12 if ("Elephant".equals(name)) { 13 return new ElephantFactory().getAnimal(); 14 }else if ("Leopard".equals(name)) { 15 return new LeopardFactory().getAnimal(); 16 }else { 17 return null; 18 } 19 } 20 21 }
來個實現方法,其中有一個默認的產生對象
1 package com.wjc.Factory.abstra; 2 3 import com.wjc.Factory.Animal; 4 import com.wjc.Factory.func.LeopardFactory; 5 6 public class Factory extends AbstarctFactory { 7 8 private LeopardFactory defaultFactory = new LeopardFactory(); 9 10 @Override 11 protected Animal getAnimal() { 12 // TODO Auto-generated method stub 13 return defaultFactory.getAnimal(); 14 } 15 16 }
測試一下性能
1 package com.wjc.Factory.abstra; 2 3 public class Test { 4 5 public static void main(String[] args) { 6 7 Factory factory = new Factory(); 8 System.out.println(factory.getAnimal("Elephant").eat()); 9 10 11 } 12 13 }
上述改造后的代碼就是抽象工廠模式了
小結一下工廠模式,特性是封裝了創建Bean的過程。