Simple Factory Pattern

Purpose

SimpleFactory is a simple factory pattern.

It differs from the static factory because it is not static. Therefore, you can have multiple factories, differently parameterised, you can subclass it and you can mock it.

It has some advantages over a Static Factory but a Static Factory closes classes for modification, this technically doesn’t (if you needed to change the Simple Factory, you’d be adjusting the class its being called from).

Examples
UML


Code

Bicycle


namespace DesignPatterns\Creational\SimpleFactory;

class Bicycle
{
    public function driveTo(string $destination)
    {
    }
}

SimpleFactory


namespace DesignPatterns\Creational\SimpleFactory;

class SimpleFactory
{
    public function createBicycle(): Bicycle
    {
        return new Bicycle();
    }
}

Ready to bring your vision to life?

We believe in excellence, empathy, integrity, and transparency throughout the process. Our goal is to build fast, responsive websites that not only perform but also reflect your values and vision.