Design Pattern: Abstract Factory Pattern

Summary

Pattern NameAbstract Factory Pattern
Pattern TypeCreational Pattern
ScopeObject
Other NamesKit
TaglineFactory of Factory
Use casesWhen we need a factory to generate
multiple factory pattern implementations
Related PatternsFactory
Facade
Flyweight
Prototype
Difficulty LevelMedium
Implementations

Definition

Using the Factory pattern we generate objects based on some criteria. In Abstract factory pattern, we use multiple factory pattern implementations and generate objects based on that.

Abstract factory pattern is also called Factory of Factory, so before reading about Abstract factory pattern, take a look at the Factory pattern.

Implementation

We need to implement multiple factory patterns and then implement a parent factory for those factory implementations.

There are 5 elements in the implementation:

  1. Abstract Factory Interface: interface that is implemented by concrete abstract factory classes.
  2. Concrete Abstract Factory Classes: classes that return instances of the concrete class.
  3. General Interface: interface implemented by concrete classes.
  4. Concrete Classes: classes implementing factory interface and containing the actual operations. Getting these classes is the final purpose of this implementation.
  5. Factory Producer: returns instances of concrete factory based on some pre-decided condition. Factory Producer directly uses only Abstract Factory Interface and Concrete Abstract Factory Classes.

Here is a diagram to represent the Abstract Factory Pattern implementation.

Here is how to implement Abstract Factory Pattern:

  1. First step is to create the classes that we want to use finally. These concrete classes are the main utility classes.
  2. Create an interface to standardize the utility classes.
  3. Implemented the interface to the utility classes.
  4. Create an interface for Abstract Factory and implement how the factory classes will be used to generate objects.
  5. Create multiple classes (as required) and implement Abstract factory and Use this parent factory to generate objects from the desired classes.
  6. Create a factory producer class, implement the condition to generate and return the factory class based on some conditions.

Examples

Example #1: Transport

Let’s take the example of a transport system. Take a look at the implementation.

Transport Factory Interface

 interface Transport

    function start()

    function stop()

    function repair()

end interface

Transport Classes Implementing Transport Interface

class Bicycle implements Transport
    
    function start()
        print "Bicycle Started"
    end function

    function stop()
        print "Bicycle Stopped"
    end function

    function repair()
        print "Bicycle Repair"
    end function

end class

class Motorcycle implements Transport

    function start()
        print "Motorcycle Started"
    end function

    function stop()
        print "Motorcycle Stopped"
    end function

    function repair()
        print "Motorcycle Repair"
    end function

end class

class Car implements Transport
    
    function start()
        print "Car Started"
    end function

    function stop()
        print "Car Stopped"
    end function

    function repair()
        print "Car Repair"
    end function

end class

class Truck implements Transport
    
    function start()
        print "Truck Started"
    end function

    function stop()
        print "Truck Stopped"
    end function

    function repair()
        print "Truck Repair"
    end function

end class

Abstract Transport Factory Interface

interface AbstractTransportFactory

   function getTransport(type: String): Transport

end interface

Two Wheel Transport Factory Class

class TwoWheelTransportFactory implements AbstractTransportFactory
    
   fuction getTransport(String type): Transport

        if (type == "bicycle")
            return new Bicycle()
        end if

        if (type == "motorcycle")
            return new Motorcycle()
        end if

        return null

    end function

end class

Four Wheel Transport Factory Class

class FourWheelTransportFactory implements AbstractTransportFactory

    function getTransport(type: String): Transport

        if (type == "car")
            return new Car()
        end if

        if (type == "truck")
            return new Truck()
        end if

        return null
    
    end function

end class

Factory Producer Class

class FactoryProducer

   static function getFactory(int numberOfWheels): AbstractTransportFactory

        if (numberOfWheels == 2)
            return new TwoWheelTransportFactory()
        end if

        if (numberOfWheels == 4)
            return new FourWheelTransportFactory()
        end if

        return null

    end function

end class

Demo

var transportFactory1: AbstractTransportFactory = FactoryProducer.getFactory(2)
Transport transport1 = transportFactory1.getTransport("bicycle")

transport1.start()

var transportFactory2: AbstractTransportFactory = FactoryProducer.getFactory(4)
Transport transport2 = transportFactory2.getTransport("truck")

transport2.start()

Output

Bicycle Started
Truck Started

Code Implementations

Use the following links to check Abstract Factory pattern implementation in specific programming languages.

Leave a Comment