Factory Method
英文简单描述
Intent
Define an interface for creating an object, but let subclasses decide which class to instantiate.
Factory methods eliminate the need to bind application-specific classes into your code.The code only deals with interfaces; therefore it can work with any user-defined classes that implement the right interface.
How to
Product
defines the interface of objects the factory method creates.
ConcreteProduct
implements the Product interface.
Creator
declares the factory method, which returns an object of type Product.
ConcreteCreator
overrides the factory method to return an instance of a ConcreteProduct.
Known Cases
Used in Abstract Factory
Document Application
UML
工厂方法有三种变体:
(1)并行的类层次结构
(2)根据类型id,创建不同子类对象
(3)利用泛型来去除Creator子类