Abstract Factory Pattern


This pattern is used when we wish to configure our application with one of many families or themes of products (objects) without forcing the client to program to a specific implementation of a product. It allows us to create a family of related objects without exposing the actual implementations of those objects. So, the client has to use only the abstract interface object referring to a particular implementation. This enables runtime configurability because the same abstract object can be paired with a different implementation of the same interface. This change takes place as per the client needs.

This kind of design enhances modifiability in the code as whenever the implementation needs to be changed, the programmer is not required to make the change everywhere in the code. As the client is aware only of the abstract interface object, which always remains the same irrespective of the implementation it is referring to. In totality, only one line change is required in the whole code to refer the abstract interface object to a different implementation.

A factory class is used to construct related objects (products of a particular family ). The pattern is called “Abstract Factory” because one more level of abstraction is provided to the user where the user is not available of the actual factory classes. The client application uses an abstract factory which is a general interface for each factory class in the system. Each factory class will implement the abstract factory interface to construct a set of related objects.

The application also contains the definitions of the objects constructed by the factory classes. These objects are of a particular product type.

For example, there is a specific product type called as “ProductA”. Similarly, we have another product type “ProductB”.

ProductA has many products {ProductA1,ProductA2,…..}.
ProductB has many products {ProductB1,ProductB2,…..}.

The client deals only with the abstract product objects or the types like ProductA or ProductB. The client is not aware of the actual creation or existence of specific objects of type ProductA or ProductB. So the implementation details are hidden and encapsulated into the abstract product objects.
ProductA1 and ProductB1 form a family of products. A particular factory class will be responsible for the creation of these product objects. Similarly, ProductA2 and ProductB2 form another family of product objects constructed by a different factory class. We need to remember that although the factory class construct these specific product objects, the client still deals only with abstract product objects ProductA and ProductB ).

As per the above description, the following classes together make up the design of AbstractFactory pattern.

  • AbstractFactory – An abstract class that declares the interface to create basic kinds of products
  • ConcreteFactoryOne – An implementation of the abstract factory class. This factory class is responsible to create specific product objects of one family. The class still gives the client only the abstract product objects referring to a particular implementation.
  • ConcreteFactoryTwo – An implementation of the abstract factory class. This factory class is responsible to create specific product objects of one family. The class still gives the client only the abstract product objects referring to a particular implementation.
  • ProductA – An abstract class that declares an interface for a product type. All the products of this type will implement this interface.
  • ProductA1 – An implementation of the “ProductA” type. This concrete object is constructed in the factory class.
  • ProductA2 – An implementation of the “ProductA” type. This concrete object is constructed in the factory class.
  • ProductB – An abstract class that declares an interface for a product type. All the products of this type will implement this interface.
  • ProductB1 – An implementation of the “ProductB” type. This concrete object is constructed in the factory class.
  • ProductB2 – An implementation of the “ProductB” type. This concrete object is constructed in the factory class.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: