Factory Method:
**Intent:Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
**Applicability:
- a class can't anticipate the class of objects it must create.
- a class wants its subclasses to specify the objects it creates.
- classes delegate responsibility to one of several helper subclasses, and
- you want to localize the knowledge of which helper subclass is the delegate.
**Structure:
Product, Concrete Product, Creator, Concrete Creator
Factory Method和Abstract Factory的区别:1.One difference between the two is that with the Abstract Factory pattern, a class delegates the responsibility of object instantiation to another object via composition whereas the Factory Method pattern uses inheritance and relies on a subclass to handle the desired object instantiation.
在Abstract Factory模式中,client把实例化交给另一个类Abstract Factory来做。在Factory Method中,实例化由子类来完成。
2.对比两者的structure,Factory Method模式中的Creator和Concrete Creator在Abstract Factory模式中被Abstract Factory和Concrete Factory代替。而一个Factory是可以有多个Creator组成。
Creational Design Patterns:In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation.
Some examples of creational design patterns include:
* Abstract factory pattern: centralize
decision of what factory to instantiate. 用来创建一组product对象。
* Factory method pattern: centralize creation of an object of a specific type choosing one of several
implementations * Builder pattern: separate the construction of a complex object from its representation so that
the same construction process can create different representations
各种模式之间的关系:通常我们将Factory Method作为一种标准的创建对象的方法,当发现需要更多的灵活性的时候,就开始考虑向其它创建型模式转化。
References:
1.
http://en.wikipedia.org/wiki/Abstract_factory_pattern2.
http://en.wikipedia.org/wiki/Factory_method_pattern#JavaScript 3.
http://en.wikipedia.org/wiki/Builder_pattern4.
http://en.wikipedia.org/wiki/Creational_pattern 5.在 Java 中应用设计模式 - Factory Method,
http://www.ibm.com/developerworks/cn/java/designpattern/factory/