2015年12月1日 星期二

Abstract Factory and Factory method

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.
定義一個interface利用工廠來產生這些物件並符合這個interface 為標準。
當你是在作一個platform ,而不是一個product  時,
就必須考慮這個pattern
它讓你的程式可以和底層分開

example:
Example of Abstract Factory

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


沒有留言: