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
在《設(shè)計(jì)模式之禪》這本書中分了兩章節(jié)講工廠模式:
- 工廠方法模式
- 抽象工廠模式
網(wǎng)上的大部分資料都是將工廠模式分成三種:
- 簡單/靜態(tài)工廠模式
- 工廠方法模式
- 抽象工廠模式
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).