Software Design Pattern: Abstract Factory Pattern

Software Design Pattern: Abstract Factory Pattern

A creational design pattern called the Abstract Factory Pattern offers a way to create groups of connected or interdependent things without defining the particular classes for them. It is especially helpful when you want to make sure the things you make are compatible and fit into a certain family or group. The Abstract Factory Pattern encourages loose coupling and improves the maintainability of your code by enclosing the creation mechanism.

Example:

Let's look at a real-world illustration of the Abstract Factory Pattern: the production of furniture. Imagine you have been given the assignment of creating a software system for a furniture factory. The factory makes a variety of furniture, such as sofas, tables, and chairs. There are numerous variations of each style of furniture, including contemporary chairs, traditional chairs, and so forth.

Abstract Factory Pattern in Furniture Manufacturing

In the context of our furniture manufacturing scenario, we can identify the following components of the Abstract Factory Pattern:

  • Abstract Factory: This represents the interface for creating families of related furniture objects. In our example, this could be an AbstractFurnitureFactory interface.

  • Concrete Factories: These are classes that implement the Abstract Factory interface and are responsible for creating specific families of furniture objects. For instance, we might have a ModernFurnitureFactory and a TraditionalFurnitureFactory class.

  • Abstract Products: These are interfaces or abstract classes representing the individual furniture products, such as Chair, Table, and Sofa.

  • Concrete Products: These are the actual furniture objects that implement the Abstract Products. For example, a ModernChair class, a TraditionalChair class, and so on.

Code:

// Step 1: Abstract Factory
interface AbstractFurnitureFactory {
    Chair createChair();
    Table createTable();
    Sofa createSofa();
}

// Step 2: Concrete Factories
class ModernFurnitureFactory implements AbstractFurnitureFactory {
    @Override
    public Chair createChair() {
        return new ModernChair();
    }

    @Override
    public Table createTable() {
        return new ModernTable();
    }

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

class TraditionalFurnitureFactory implements AbstractFurnitureFactory {
    @Override
    public Chair createChair() {
        return new TraditionalChair();
    }

    @Override
    public Table createTable() {
        return new TraditionalTable();
    }

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

// Step 3: Abstract Products
interface Chair {
    void sit();
}

interface Table {
    void serve();
}

interface Sofa {
    void relax();
}

// Step 4: Concrete Products
class ModernChair implements Chair {
    @Override
    public void sit() {
        System.out.println("Sitting on a modern chair.");
    }
}

class TraditionalChair implements Chair {
    @Override
    public void sit() {
        System.out.println("Sitting on a traditional chair.");
    }
}

// Similar classes for Table and Sofa...

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

        Chair chair = factory.createChair();
        chair.sit(); // Output: Sitting on a modern chair.

        Table table = factory.createTable();
        table.serve(); // Output: Serving on a modern table.

        Sofa sofa = factory.createSofa();
        sofa.relax(); // Output: Relaxing on a modern sofa.
    }
}

In this example, we've built two concrete furniture factories (ModernFurnitureFactory and TraditionalFurnitureFactory), each of which produces items of furniture in line with its own design aesthetic. The FurnitureClient shows us how to generate and use furniture objects using the Abstract Factory Pattern without having to be familiar with their unique implementations.

Follow me on : Github Linkedin