Jasir KT

Software Developer

Home

Abstract Factory Pattern

Published Sep 02, 2019

The Abstract Factory Pattern adds another layer of abstraction over the Factory Pattern. It has an Abstract Factory Interface which is implemented by several Factory classes. A Super Factory decides which Factory should be used based on the requirement. Abstract Factory Pattern is one the creational design patterns.

Participants

  • AbstractFactory
    An interface which for creating AbstractProduct objects.
  • ConcreteFactory
    Implements AbstractFactory and creates ConcreteProduct objects.
  • AbstractProduct
    An interface for type of products.
  • ConcreteProduct
    Implements AbstractProduct object. Here is where the actual product code is defined.
  • Client
    Uses methods provided by AbstractFactory and AbstractProduct interfaces to create objects.

UML Diagram

As you can see in the diagram, There is an AbstractFactory interface which is implemented by 2 factory classes. Client doesn’t access the concrete factory classes directly, instead uses the methods provided by AbstractFactory interface.

Example

abstract class Computer {
    public abstract void powerOn();
}

class PC extends Computer {

    public void powerOn() {
        System.out.print("Powering on PC...");
    }
}

class Server extends Computer {

    public void powerOn() {
        System.out.print("Powering on server...");
    }
}

interface AbstractComputerFactory {
    Computer createComputer();
}

class PCFactory implements AbstractComputerFactory {

    public Computer createComputer() {
        return new PC();
    }
}

class ServerFactory implements AbstractComputerFactory {

    public Computer createComputer() {
        return new Server();
    }
}

class ComputerFactory {
    public static Computer createComputer(String type) {
        AbstractComputerFactory factory;
        switch (type) {
            case "pc" : factory = new PCFactory(); break;
            case "server" : factory = new ServerFactory(); break;
            default: throw new IllegalArgumentException();
        }

        return factory.createComputer();
    }
}

class Main {
    public static void main(String[] args) {
        ComputerFactory.createComputer("pc").powerOn();
        ComputerFactory.createComputer("server").powerOn();
    }
}