Factory Pattern

The Factory Pattern is a creational design pattern that provides a way to encapsulate the instantiation logic of an object. It can be used in a variety of situations, but it is particularly useful when you need to create different types of objects at runtime. For example, you could use the Factory Pattern to create different types of database connections, files, or user sessions.

Let's say you are developing a web application that allows users to create different types of content, such as blog posts, articles, and images. You could use the Factory Pattern to create different types of content objects at runtime. This would allow you to decouple the client code from the concrete class of the content object to be created.

The following code shows an example of how to use the Factory Pattern in PHP to create different types of content objects:

<?php

// Define the Factory interface.
interface ContentFactoryInterface
{
    public function createContent($contentType);
}

// Define the ConcreteFactory class.
class ConcreteFactory implements ContentFactoryInterface
{
    public function createContent($contentType)
    {
        switch ($contentType) {
            case 'blog_post':
                return new BlogPost();
            case 'article':
                return new Article();
            case 'image':
                return new Image();
            default:
                throw new Exception('Invalid content type');
        }
    }
}

// Create a new instance of the Factory class.
$factory = new ConcreteFactory();

// Create a blog post object.
$blogPost = $factory->createContent('blog_post');

// Use the blog post object.
$blogPost->setTitle('My First Blog Post');
$blogPost->setContent('This is my first blog post.');
$blogPost->save();

This code would create a new blog post object and save it to the database. The blog post object would be created by the ConcreteFactory class, which would use the Factory Pattern to decouple the client code from the concrete class of the blog post object.

You might be asking well what's the difference between this and the Builder pattern? Well they're both creational design patterns, but they have different purposes. The Factory Pattern is used to create different types of objects at runtime, while the Builder Pattern is used to create complex objects step by step. It's a more general pattern, while the Builder Pattern is a more specialized pattern. The Factory Pattern can be used to create any type of object, while the Builder Pattern is only used to create complex objects.

To choose between the two consider the Builder pattern if you need to create a complex object in a step-by-step manner, when you need to provide default values for some of the object's attributes, or when you need to provide a fluent interface for creating objects. Whereas for the Factory pattern you'll want to stick to it if you have a superclass with multiple subclasses, when you want to centralize the creation of objects, or when you want to decouple the client code from the concrete classes that are being created.