
The pattern is best utilized when our system has to create multiple families of products or we want to provide a library of products without exposing the implementation details.
An example of an Abstract Factory in use could be UI toolkits. Across Windows, Mac and Linux, UI composites such as windows, buttons and textfields are all provided in a widget API like SWT. However, the implementation of these widgets vary across platforms. We could write a platform independent client thanks to the Abstract Factory implementation.
Intent
- Provide an interface for creating a family of related objects, without explicitly specifying their concrete classes.
- The
newoperator considered harmful.
Implementation

Window is our AbstractProduct.
|
|
This is our concrete product for Microsoft Windows.
|
|
This is our concrete product for Mac OSX.
|
|
Now we need to provide our AbstractFactory.
|
|
Next we need to provide ConcreteFactory implementations for MS Windows.
|
|
And for Mac OSX.
|
|
Here is our client to take advantage of all this functionality.
|
|




近期评论