Overview

The Abstract Factory Pattern is known as a creational pattern. Abstract Factory patterns work around a super-factory which creates other factories.

It provides an interface for creating families of related or dependent objects without specifying their concrete classes.

It has a level of indirection that abstracts the creation of families of related or dependent objects without directly specifying their concrete classes.

The “factory” object has the responsibility for providing creative services for the entire platform family. Clients never create platform objects directly, they ask the factory to do that for them.

This mechanism makes exchanging product families easy because the specific class of the factory object appears only once in the application – where it is instantiated.

The application can wholesale replace the entire family of products simply by instantiating a different concrete instance of the abstract factory.

Let’s understand Problem and Solution with Example

There is a multi-cuisine restaurant. They have only one chef to cook Chinese food.

Now the restaurant owner wants to start Mexican, Italian Foods to be available in his restaurant.

To start this owner needs Mexican food cooked by Mexican Chef, Italian food cooked by Italian Chef but to save costs of all owner wants Chinese chef to cook Mexican and Italian Food.

Abstract Factory pattern provided a way to encapsulate a group of individual factories.

Implementation

We will solve the problem for owners of multi-cuisine restaurant by implementing Abstract Factory Pattern.

Illustration of Abstract Factory Pattern
Illustration of Abstract Factory Pattern
Step 1: Abstract Factory Design Pattern Super Classes and Sub Classes

To implement, We need to create basic classes and subclasses for food operations.

Step 2: Factory Class for Each subclass

First of all we need to create an Abstract Factory interface or an abstract class.

Notice that getChineseFood(), getMexicanFood() and getItalianFood() method is returning an instance of super class ChineseFood, MexicanFood and ItalianFood respectively. Now our factory classes will implement this interface and return their respective sub-class.

Lets write a client test method and see how to use the abstract factory to get the instance of sub-classes.

Output :
Chinese Food Order for ChefA Chinese Noodels with Red Sauce ordered Mexican Food Order for ChefB Mexican Nachos Crispy ordered ChineItalianse Food Order for ChefA Italian Pasta with White Sauce ordered Chinese Food Order for ChefB Chinese Rice Spicy ordered
Advantages of Abstract Factory Pattern
  1. It isolates the creation of objects from the client that needs them, giving the client only the possibility of accessing them through an interface, which makes the manipulation easier.

  2. It provides an approach to code for interface rather than implementation.

  3. Abstract Factory pattern is robust and avoid conditional logic of the Factory pattern.

  4. It promotes consistency between products. It is the concrete factory’s job to make sure that the right products are used together.

Drawbacks of Abstract Factory Pattern

Adding a new product requires extending the abstract interface which implies that all of its derived concrete classes also must change.

Leave a Reply

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