Thilan Dissanayaka Software Architecture Apr 26

Abstract Factory Pattern explained simply

When you want to create families of related objects without specifying their concrete classes,
the Abstract Factory Pattern is your best friend.


What is the Abstract Factory Pattern?

  • Provides an interface for creating families of related or dependent objects.
  • Does not specify their concrete classes.
  • Helps ensure that products from the same family are used together.

In short:

A factory of factories!


Real-Life Analogy

Imagine a company that manufactures furniture:

  • Modern Furniture Factory → produces Modern Chair, Modern Sofa.
  • Victorian Furniture Factory → produces Victorian Chair, Victorian Sofa.

The client can request a family of furniture without worrying about how it’s made.


Structure

  • Abstract Factory: Interface for creating related products.
  • Concrete Factories: Implementations that create concrete products.
  • Abstract Products: Interfaces for different types of products.
  • Concrete Products: Specific implementations of products.

Example in Java

Abstract Product Interfaces

public interface Chair {
    void sitOn();
}

public interface Sofa {
    void lieOn();
}

Concrete Products

public class ModernChair implements Chair {
    @Override
    public void sitOn() {
        System.out.println("Sitting on a Modern Chair.");
    }
}

public class VictorianChair implements Chair {
    @Override
    public void sitOn() {
        System.out.println("Sitting on a Victorian Chair.");
    }
}

public class ModernSofa implements Sofa {
    @Override
    public void lieOn() {
        System.out.println("Lying on a Modern Sofa.");
    }
}

public class VictorianSofa implements Sofa {
    @Override
    public void lieOn() {
        System.out.println("Lying on a Victorian Sofa.");
    }
}

Abstract Factory

public interface FurnitureFactory {
    Chair createChair();
    Sofa createSofa();
}

Concrete Factories

public class ModernFurnitureFactory implements FurnitureFactory {
    @Override
    public Chair createChair() {
        return new ModernChair();
    }

    @Override
    public Sofa createSofa() {
        return new ModernSofa();
    }
}

public class VictorianFurnitureFactory implements FurnitureFactory {
    @Override
    public Chair createChair() {
        return new VictorianChair();
    }

    @Override
    public Sofa createSofa() {
        return new VictorianSofa();
    }
}

Using the Abstract Factory

public class MainProgram {
    public static void main(String[] args) {
        FurnitureFactory factory = new ModernFurnitureFactory();

        Chair chair = factory.createChair();
        Sofa sofa = factory.createSofa();

        chair.sitOn();
        sofa.lieOn();

        // Switch to a different family
        factory = new VictorianFurnitureFactory();

        Chair victorianChair = factory.createChair();
        Sofa victorianSofa = factory.createSofa();

        victorianChair.sitOn();
        victorianSofa.lieOn();
    }
}

Output:

Sitting on a Modern Chair.
Lying on a Modern Sofa.
Sitting on a Victorian Chair.
Lying on a Victorian Sofa.

Why Use Abstract Factory Pattern?

  • Consistency: Ensures products from the same family are compatible.
  • Scalability: Add new families easily without changing client code.
  • Decoupling: The client doesn't know or care about the specific classes.

Real-World Use Cases

  • Cross-platform GUI toolkits (Windows, Mac, Linux UIs).
  • Theme-based applications (Light Theme Factory, Dark Theme Factory).
  • Game development (different worlds: Ice World Factory, Fire World Factory).

Abstract Factory vs Factory Method

Factory Pattern Abstract Factory Pattern
Creates one product Creates families of products
Single factory class Multiple related factories
Simpler and smaller Bigger and more abstract

Summary

The Abstract Factory Pattern is all about producing families of related objects without coupling your code to their concrete classes.
It offers flexibility, consistency, and makes your codebase ready for easy scaling and future changes.


ALSO READ
Abstract Factory Pattern explained simply
Apr 26 Software Architecture

When you want to create **families of related objects** without specifying their concrete classes, the **Abstract Factory Pattern** is your best friend. --- ## What is the Abstract Factory....

 OWASP Top 10 explained - 2021
Mar 03 Web App Hacking

The Open Worldwide Application Security Project (OWASP) is a nonprofit foundation focused on improving the security of software. It provides free, vendor-neutral tools, resources, and standards that....

Introduction to Edge Computing
Mar 23 Computing Concepts

Edge computing is a distributed computing paradigm where computation and data storage are performed closer to the location where it is needed. Instead of relying solely on a centralized data center,....

Adapter Pattern explained simply
Apr 26 Software Architecture

Ever needed to connect two incompatible interfaces without changing their source code? That’s exactly where the **Adapter Pattern** shines! The Adapter Pattern is a structural design pattern....

Building a Web3 CLI Tool for the Ballerina Language: From Idea to Reality
Apr 26 WSO2

🚀 Excited to finally share my journey of building a web3 CLI tool for Ballerina! This tool bridges the gap between Ethereum smart contracts and the Ballerina programming language by automatically....

Reverse TCP shell with Metasploit
Mar 23 Web App Hacking

Metasploit is an awesome tool which is. It can automate the exploitation process, generate shellcodes, use it as a listener, etc. I hope to start a tutorial series on the Metasploit framework and its....