Creational (创建型模式)
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.
Abstract Factory (抽象工厂模式)
To create series of related or dependent objects without specifying their concrete classes. Usually the created classes all implement the same interface. The client of the abstract factory does not care about how these objects are created, he just knows how they go together.
提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。
Builder (建造者模式)
Builder is an interface that build parts of a complex object.
Sometimes, if the builder has a better knowledge of what it builds, this interface could be an abstract class with default methods (aka adapter).
If you have a complex inheritance tree for objects, it is logical to have a complex inheritance tree for builders too.
Note: Builders have often a fluent interface, see the mock builder of PHPUnit for example.
将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
Factory Method (工厂模式)
The good point over the SimpleFactory is you can subclass it to implement different ways to create objects
For simple case, this abstract class could be just an interface.
This pattern is a “real” Design Pattern because it achieves the “Dependency Inversion Principle” a.k.a the “D” in
S.O.L.I.D principles.
SOLID (single responsibility, open-closed, Liskov substitution, interface segregation and dependency inversion)
It means the FactoryMethod class depends on abstractions, not concrete classes. This is the real trick compared to SimpleFactory or StaticFactory.
定义一个用于创建对象的接口,让子类决定将哪一个类实例化。Factory Method使一个类的实例化延迟到其子类。