Select Page

Abstract Factory Design Pattern with Example in PHP

Abstract factory method is useful for creating products of related family. Create objects of product from sub-classes. A family could have multiple member and each member could have variation among them but they behave similarly.

For example: Let’s consider a furniture family consist of member like chair, table, sofa. Here member may have different type in looks. Suppose chair may have different types like: modern, victorian, art deco. Similarly table and sofa may have different type as well.

4 Steps for implementing Abstract Factory Design Pattern

I have divided the implementation into four easy steps:

Step 1: Create Factory Abstract Class

Factory class will produce multiple product through abstract method.

abstract class FurnitureFactory
{
    abstract public static function createChair(): ChairInterface;

    abstract public static function createTable(): TableInterface;

    abstract public static function createSofa(): SofaInterface;
}

Step 2: Create Factory Concrete Class

Factory concrete class will extends the Factory class so that it implement abstract method. Factory concrete class will implement each distinct product variation and responsible for creating object of product.


class ModernFurniture extends FurnitureFactory
{
    public static function createChair(): ChairInterface
    {
        return new ModernChair();
    }
    public static function createTable(): TableInterface
    {
        return new ModernTable();
    }
    public static function createSofa(): SofaInterface
    {
        return new ModernSofa();
    }
}

Step 3: Interface of each Distinct Product

Interface will be implemented by the product class so that each product have consistency in their behaviour. This way we don’t have to worry about the variations. Since all variations will implement same interface we can also use property and method of class same way.

interface ChairInterface
{
    public function legs(): int;

    public function sitting();
}

interface TableInterface
{
    public function legs(): int;

    public function hasDrawer(): bool;
}

interface SofaInterface
{
    public function seats(): int;
    public function hasDivan(): bool;
}

Step 4: Declare Product Concrete Class for each Product

Each product will have separate class by implementing interface.

class ModernChair implements ChairInterface
{
    public function legs(): int
    {
        return 4;
    }

    public function sitting()
    {
        return 'fine';
    }
}

class ModernTable implements TableInterface
{
    public function legs(): int
    {
        return 4;
    }

    public function hasDrawer(): bool
    {
        return true;
    }
}

class ModernSofa implements SofaInterface
{
    public function seats(): int
    {
        return 5;
    }

    public function hasDivan(): bool
    {
        return false;
    }
}

Now our product is ready to use by the client code

$modern_chair = ModernFurniture::createChair();
echo $modern_chair->legs();

From this Modern Furniture class will can create table & sofa as well and can show users as relevant product.

In future we need more variations for the furniture we can simple create another variation Factory Concrete class without touching any other code 🙂