做爲一個電氣工程師,研究等離子體方向,最近在自學設計模式,此爲整理博客。設計模式能夠分爲三大類,分別是建立型設計模式、行爲型設計模式以及結構型設計模式。設計模式
工廠模式是建立型的設計模式的一種。
先看工廠方法模式,並不那麼能「顧名思義」,誠然,我能夠揣測是用工廠來創造對象,可是這不免會跟IoC等概念混淆,而且若是按較爲官方的定義:Define an interface for creating an object,but let subclasses decide which class to instantiate.Factory Method lets a class defer instantiation to subclasses.
工廠方法模式是定義接口,而讓子類來決定實例化哪一個類;工廠方法讓類的實例化延遲到其子類。那按這個指導思想,工廠方法好像沒有那麼多條條框框了,試實現之。架構
心法:
萬事萬物皆對象,萬事萬物皆產品!app
再也不new了!讓工廠去new吧!使用工廠生產同一類產品!
框架
定義產品接口ide
public interface Product{ void operation(); }
產品實現類工具
public class ProductA implements Product{ private String name; private int size; @Override void operation(){ System.out.println("這是產品A"); } }
public class ProductB implements Product{ private String name; private int size; @Override void operation(){ System.out.println("這是產品B"); } }
抽象工廠方法類spa
public abstract class AbstractFactory{ public abstract <T extends Product> T createProduct(Class<T> c); }
具體工廠實現類設計
public class ConcreteFactory extends AbstractFactory{ @Override public <T extends Product> T createProduct(Class c){ Product product = null; try{ product = (T) c.newInstance(); } catch (Exception e){ System.out.println("生產產品失敗"); } return (T)product; } }
調用方Client類code
public class Client{ public static void main(String[] args){ AbstractFactory factory = new ConcreteFactory(); Product productA = factory.createProduct(ProductA.class); productA.operation(); Product productB = factory.createProduct(ProductB.class); productB.operation(); } }
去掉抽象類AbstractFactory,在工廠類中提供一個靜態方法返回產品component
public class SimpleFactory{ public static <T extends Product> T createProduct(Class c){ Product product = null; try { product = (T) c.newInstance(); } catch (Exception e){ System.out.println("生產產品失敗"); e.printStackTrace(); } return (T) product; } }
修改Client類中的調用方式
public class Client{ public static void main(String[] args){ Product productA = SimpleFactory.createProduct(ProductA.class); productA.operation(); Product productB = SimpleFactory.createProduct(ProductB.class); productB.operation(); } }
針對每一類產品提供一個工廠類來生產產品,uml圖爲:
抽象工廠方法類
public abstract class AbstractFactory{ public abstract Product createProduct(); }
ProductAFactory
public class ProductAFactory extends AbstractFactory{ @Override public Product createProduct(){ return new ProductB(); } }
ProductBFactory
public class ProductBFactory extends AbstractFactory{ @Override public Product createProduct(){ return new ProductB(); } }
調用方Client
public class Client{ public static void main(String[] args){ AbstractFactory factoryA = new ProductAFactory(); AbstractFactory factoryB = new ProductBFactory(); Product productA = factoryA.createProduct(); Product productB = factoryB.createProduct(); productA.operation(); productB.operation(); } }
定義:Provide an interface for creating families of creating families of related or dependent objects without specifying their concrete classes.
uml圖爲
代碼略
一些高分答案:
When using a factory your code is still actually responsible for creating objects. By DI you outsource that responsibility to another class or a framework, which is separate from your code.工廠模式中,代碼中還要建立對象,可是DI中,建立對象的責任轉交給了其餘類或者框架,與代碼已經解耦了。
I would suggest to keep the concepts plain and simple. Dependency Injection is more of a architectural pattern for loosely coupling software components. Factory pattern is just one way to separate the responsibility of creating objects of other classes to another entity. Factory pattern can be called as a tool to implement DI. Dependency injection can be implemented in many ways like DI using constructors, using mapping xml files etc. 認爲DI更大程度上是一種將模塊解耦的架構模式,而工廠模式能夠看做一類實現了DI的工具,而DI能夠經過多種方式實現。