Thursday, April 14, 2011

Design Pattern: Abstract Factory

The purpose of the Abstract Factory is to provide an interface for creating families of related objects, without specifying concrete classes.

Problem:

• How should one go about designing applications which have to be adopted to different Look and feel standards?

Solution:

•One could solve this problem by defining abstract widgets factory class that declares an interface for creating each basic kind of widgets ( control ) .

•There is also an abstract class for each kind of widget, and concrete sub classes implement widgets for specific look and standards.

Illustration:

image

Applicability:

Use the Abstract Factory pattern when

  • A system should be independent of how its products are created, composed, and represented.
  • A system should be configured with one of multiple families of products.
  • A family of related product objects is designed to be used together, and you need to enforce this constraint.
  • You want to provide a class library of products, and you want to reveal just their interfaces, not their implementations.

Structure:

image

Collaborations

Normally a single instance of a ConcreteFactory class is created at run-time. This concrete factory creates product objects having a particular implementation. To create different product objects, clients should use a different concrete factory.

AbstractFactory defers creation of product objects to its ConcreteFactory subclass.

Consequences

  • It isolates concrete classes.
  • It makes exchanging product families easy.
  • It promotes consistency among products.
  • Supporting new kinds of products is difficult.

Model:

image

AbstractFactory classes are often implemented with factory methods, but they can also be implemented using Prototype.

No comments: