Factory Design pattern in PHP - How to create and how to use this

Categories - Design Pattern in PHP Tags - Design pattern PHP   Maniruzzaman Akash   2 years ago   945   3 minutes   1

The Factory design pattern is a commonly used design pattern in object-oriented programming. It is used to create objects in a way that decouples the client code from the actual object creation. This allows for flexibility and ease of modification in the future, as the client code does not need to be changed if the type of object being created changes.

In PHP, the Factory design pattern can be implemented using a factory class that contains a static method for creating objects. This method takes in the desired object type as a parameter and returns an instance of that object.



For example, let’s say we have a class called Car with different subclasses for different types of cars (e.g. Sedan, SUV, etc.). We can create a factory class called CarFactory that has a static method for creating these car objects.

class CarFactory
{
    public static function createCar($type)
    {
        if ($type === 'sedan') {
            return new Sedan();
        } elseif ($type === 'suv') {
            return new SUV();
        } else {
            throw new Exception('Invalid car type');
        }
    }
}

In this example, the createCar() method takes in the desired car type as a parameter and returns an instance of the corresponding car object. This allows the client code to create car objects without having to know the specific implementation details of the different car classes.

Here is an example of how the client code can use the CarFactory class to create car objects:

$sedan = CarFactory::createCar('sedan');
$suv = CarFactory::createCar('suv');

The Factory design pattern allows for flexibility and ease of modification in the future. For example, if we want to add a new type of car (e.g. hatchback), we can simply add a new elseif statement in the createCar() method without having to change any of the client code.

class CarFactory
{
    public static function createCar($type)
    {
        if ($type === 'sedan') {
            return new Sedan();
        } elseif ($type === 'suv') {
            return new SUV();
        } elseif ($type === 'hatchback') {
            return new Hatchback();
        } else {
            throw new Exception('Invalid car type');
        }
    }
}

In summary, the Factory design pattern is a useful tool in object-oriented programming that allows for the creation of objects in a way that decouples the client code from the object creation. This allows for flexibility and ease of modification in the future.

Pros of using the factory design pattern

  1. It allows for the creation of objects without specifying their exact type, which can make the code more flexible and easier to modify.
  2. It can help to reduce tight coupling between different components of the code, since the factory can provide a consistent interface for creating objects.
  3. It can make the code easier to maintain and extend, since the factory can be used to centralize the logic for creating objects and make it easier to add new types of objects in the future.

Cons of using the factory design pattern

  1. It can make the code more complex, since it introduces an additional layer of abstraction.
  2. It can make it more difficult to debug the code, since the specific type of object being created is not always apparent from the code.
  3. It can make it more difficult to understand the code, since the factory pattern uses method names that may not be immediately clear to someone reading the code.

Overall, the factory design pattern can be a useful tool for managing the creation of objects in PHP, but it should be used carefully to avoid making the code too complex or difficult to understand.