Abstract Factory Pattern

Abstract factory pattern is a creational pattern. It provides an interface using which a client can instantiate
a group of related Objects. This tutorial explains the Abstract factory pattern using a Car manufacturing algorithm.

Example
We design a framework that can build cars. We build two types of
cars, a Mazda and a Ferrari. The Car Builder also specifies the type of insurance and maintenance plan for the cars.
We use the abstract factory pattern to initialize the Car Object and related insurance and maintenance Objects
together

Lets look at the Car, Insurance and Maintenance Objects.

Lets look at how we would typically build cars without the abstract factory pattern

In the above example we created two cars. We created maintenance and insurance objects for those cars and
assigned them to the cars. Lets see the problems with this framework and how the Abstract Factory Pattern can be used
to improve the object creation.

  • The client that uses the Car objects also has to know how to create the maintenance
    and insurance objects. If a particular car changes its insurance plan, the client will have to change. Abstract
    Factory pattern can be used here to encapsulate what changes.
  • There is no way to ensure that a car uses only a particular type of Insurance plan.
    Abstract Factory pattern can be used here to instantiate a family of products
    together
    . i.e. we make sure that when we create a Ferrari, we also create a Basic Maintenance plan and a partial
    Insurance plan.

This the class diagram after changing the classes to use the Abstract Factory Pattern.

Abstract Factory Design Pattern

Lets see the classes

Client

Factories

We have removed the object creation process from the client and put it in a separate factory. The factory
creates the group of related objects and is abstract. It is extended by concrete factories to create the actual
products. The client holds a reference to the abstract factory and can access the products through its interfaces but
not the concrete implementations. The factory can be replaced without modifying the client.

The Client contains a reference to an abstract factory. The concrete
factory is injected into the client. All related objects are created by the concrete factory.

New Concrete Factories can be created or extended to create different class
of products.

The client has no idea which concrete product is created. It just works
with the reference to the Product interface.

What are the shortcomings of the abstract factor pattern?

  • The family of related products have to be created together. if you need a different maintenance plan for
    Ferrari, you need to create a new concrete factory or extend the existing one.
  • The pattern requires creation of more classes, and will create a profusion of classes. For simple systems
    this may not be required.

Except for the couple of situations described above, the abstract factory pattern is widely used and can make
the code more robust and adaptable to changes.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.