Abstract Factory
Def:
- Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
- A hierarchy that encapsulates: many possible "platforms", and the construction of a suite of "products".
- The new operator considered harmful.
當你是在作一個platform ,而不是一個product 時,
就必須考慮這個pattern
它讓你的程式可以和底層分開
example:
Factory Method
Def:
- Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
- Defining a "virtual" constructor.
- The new operator considered harmful.
就必須為所有的工廠作一個interface
讓所有的工廠都有一個規範
可以共用同一個interface
減少讓client 使用的複雜度
可以多看一些dependency injection
考慮用object pool
讓程式少作initialization
多作clone , 及多用reuse 的class
沒有留言:
張貼留言