tip 1
Adantage of Factory Method Pattern
- Factory Method Pattern allows the sub-classes to choose the type of objects to create.
- It promotes the loose-coupling by eliminating the need to bind application-specific classes into the code. That means the code interacts solely with the resultant interface or abstract class, so that it will work with any classes that implement that interface or that extends that abstract class.
tip 2
在《设计模式之禅》这本书中分了两章节讲工厂模式:
- 工厂方法模式
- 抽象工厂模式
网上的大部分资料都是将工厂模式分成三种:
- 简单/静态工厂模式
- 工厂方法模式
- 抽象工厂模式
tip 3
- Static Factory
Is a class with a Static Method to product various sub types of Product. - Simple Factory
Is a class that can produce various sub types of Product. (It is better than the Static Factory. When new types are added the base Product class does not need to be changed only the Simple Factory Class) - Factory Method
Contains one method to produce one type of product related to its type. (It is better than a Simple Factory because the type is deferred to a sub-class.) - Abstract Factory
Produces a Family of Types that are related. It is noticeably different than a Factory Method as it has more than one method of types it produces. (This is complicated refer to next diagram for better real-life example).