Skip to content

Week 5 - Factory Design Pattern

The Factory Design Pattern

Classification

  • Creational Design Pattern

Pattern Definition

Factory Method

  • Define an interface for creating an object, but let sub-classes decide which class to instantiate. Factory Method lets a class defer instantiation to sub-classes.

Abstract Factory

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

Representations

Mermaid Graph

Factory Method
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
classDiagram
    Product <|-- ConcreteProduct : implements
    Creator <|-- ConcreteCreator : extends
    ConcreteCreator --> ConcreteProduct
    class Product{
      <<interface>>
    }
    class ConcreteProduct{
    }    
    class Creator{
      <<abstract>>
      +factoryMethod(): Product
      +otherOperations(): void
    }
    class ConcreteCreator{
      +factoryMethod(): ConcreteProduct
    }

Abstract Factory

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
classDiagram
    AbstractProductA <|-- ConcreteProductA1 : implements
    AbstractProductA <|-- ConcreteProductA2 : implements
    AbstractProductB <|-- ConcreteProductB1 : implements
    AbstractProductB <|-- ConcreteProductB2 : implements
    AbstractFactory <|-- ConcreteFactory1 : implements
    AbstractFactory <|-- ConcreteFactory2 : implements
    Client --> AbstractFactory
    Client --> AbstractProductA
    Client --> AbstractProductB
    ConcreteFactory1 --> ConcreteProductA1
    ConcreteFactory1 --> ConcreteProductB1
    ConcreteFactory2 --> ConcreteProductA2
    ConcreteFactory2 --> ConcreteProductB2    
    class AbstractProductA{
      <<interface>>
    }
    class ConcreteProductA1{ 
    }
    class ConcreteProductA2{ 
    }
    class AbstractProductB{
      <<interface>>
    }
    class ConcreteProductB1{
    }
    class ConcreteProductB2{  
    }    
    class AbstractFactory{
      <<interface>>
      +createProductA(): AbstractProductA
      +createProductB(): AbstractProductB
    }
    class ConcreteFactory1{
      +createProductA(): ConcreteProductA1
      +createProductB(): ConcreteProductB1
    }
    class ConcreteFactory2{
      +createProductA(): ConcreteProductA2
      +createProductB(): ConcreteProductB2
    }

UML

Factory Method
Abstract Factory

Usage

Factory Method

  • The factory method design pattern is used when we have a superclass with multiple sub-classes and based on input, we need to return one of the sub-class. This pattern takes out the responsibility of the instantiation of a class from the client program to the factory class.
  • Super class in factory design pattern can be an interface, abstract class or a normal java class

Abstract Factory

  • Similar to the Factory Method pattern, BUT the Abstract Factory pattern, gets rid of the factory method’s switch/if-else block and uses a class for each product sub-class. Then an Abstract Factory class that will return the sub-class based on the input factory class.

Real World Usages

  • Nearly all frameworks that allow you to do things like swap out the database, cache, or other backend being used for data storage and retrieval. Spring, laravel, etc.

Application to my Real World Application

  • Use a factory method pattern to build different processors for my pipelines depending on user input. Maybe I want a stateless pipeline, or maybe I want a stateful pipeline. Maybe I’d like a pipeline that gets interrupted if an error occurs (Interruptable pipeline).

Java Code Example

Main Take-Aways from the textbook

  • All factory patterns promote loose coupling by reducing the dependency of your application on concrete classes.
  • The intent of Factory Method is to allow a class to defer instantiation to its subclasses.
  • The intent of Abstract Factory is to create families of related objects without having to depend on their concrete classes.
  • Factories are a powerful technique for coding to abstractions, not concrete classes.
  • The Dependency Inversion Principle guides us to avoid dependencies on concrete types and to strive for abstractions.

Main OO Principles of Factory Pattern

Factory Method

Abstract Factory