Monday, April 2, 2007

Abstract Factory

Why should we bother with Abstract Factory? Because it separates implementaton of styles from functions. That's to say, when there are several styles of implementing something, which has nothing to do with logic or semantic functions in an upper level, we can follow the Abstract Factory's idea.
  • For each logic unit, provide an interface that's used for later interaction. Different styles of implementations are subclasses of this interface.
  • Create an interface for creating semantic products (units). Different styles are also implementation of this interface. However each subclass provides fully-functioned interface for creating all products in this style.
Here is a graphical illustration of Abstract Factory:


What are its advantages and disadvantages?
  • If we want to add another style, the current framework can still be used without a change since the interface is fixed.
  • If we want to add another logic/semantic unit, we have to add a function to the interface of Abstract Factory and implements all the styles of this logic/semantic unit. Thus it is a little difficult!
  • Sometimes, there should be only one factory. Hence a singleton should be incorporated with it.
But be sure not to confuse styles and functions!

No comments: