Common PHP design pattern factory pattern

Recently, I participated in several interviews and felt deeply about the application of several common design patterns. In order to strengthen their own understanding, and also hope to give some junior developers some reference learning, I decided to start writing a few blog posts on common PHP design patterns. The working reasons will be updated from time to time. Thank you for your patience!

Today's article mainly describes the factory pattern of common PHP design patterns. Factory mode can be divided into simple factory mode, factory method mode, abstract factory mode, etc.

1. Simple factory mode

Introduction: the simple factory pattern, also known as the static factory method pattern, belongs to the class creation pattern in the design pattern. The official explanation is: define an interface for creating objects, and let subclasses decide which class to instantiate. The factory pattern delays the instantiation of a class to its subclasses. It is generally used when there are few extensions for specific products, and the extension cannot be realized without frequent modification and code modification.

Advantages and disadvantages: the advantage lies in the separation of object creation and object use, and the creation of objects is entrusted to a special factory class; The disadvantage is that the factory class is not flexible enough. When adding new products, the factory class code needs to be modified. Once there are many products, the factory class will become extremely complex.

Specific code examples:

<?php

class A
{
    function __construct()
    {
        echo "I am A class";
    }
}

class B
{
    function __construct()
    {
        echo "I am B class";
    }
}

class Factory
{
    public static function create($name)
    {
        if ($name == 'A') {
            return new A();
        } elseif ($name == 'B') {
            return new B();
        }
    }
}

$a = Factory::create('A');
$b = Factory::create('B');

2. Factory method mode

Introduction: the factory method pattern defines an abstract core factory class and defines the interface for creating product objects, and the creation of specific instances is delayed to the factory subclass. The advantage of this design method is that the core class only needs to focus on the interface definition of the factory class, and the specific instances are created by the specific factory subclasses. When a new product needs to be added, there is no need to modify the existing code, just add a new product class and the corresponding factory subclass, so as to improve the overall scalability.

Advantages and disadvantages: Based on its introduction, we can understand that the factory method pattern is a further abstraction based on the simple factory pattern, retains the advantages of the simple factory pattern and solves its disadvantages. In the factory method mode, the core class is only responsible for giving the interface that the specific factory must implement, and does not need to be responsible for the instantiation of the specific product class, so that the system can easily introduce new products without modifying the overall code.

Specific code examples:

<?php

interface Basic
{
    public function actionOne();

    public function actionTwo();
}

class A implements Basic
{
    public function actionOne()
    {
        echo "I am A Class - Method 1";
    }

    public function actionTwo()
    {
        echo "I am A Class - Method 2";
    }
}

class B implements Basic
{
    public function actionOne()
    {
        echo "I am B Class - Method 1";
    }

    public function actionTwo()
    {
        echo "I am B Class - Method 2";
    }
}

abstract class Factory
{
    abstract static function createBasic();
}

class A_Factory extends Factory
{
    public static function createBasic()
    {
        return new A();
    }
}

class B_Factory extends Factory
{
    public static function createBasic()
    {
        return new B();
    }
}

$a = A_Factory::createBasic();
$a->actionOne();
$a->actionTwo();

$b = B_Factory::createBasic();
$b->actionOne();
$b->actionTwo();

3. Abstract factory mode

Introduction: the abstract factory pattern provides an interface to create a series of related or interdependent objects without specifying their specific classes. This pattern is a further extension of the factory method pattern. In the factory method pattern, a specific factory is responsible for the processing of a class of specific products (one-to-one relationship), but when we need a factory to be responsible for generating multiple types of products, we need to abstract the whole factory class.

Take a specific life example:

  • Product structure: suppose an abstract class is a mobile phone, and its subclasses include Xiaomi mobile phone, Huawei mobile phone and glory mobile phone, then a product structure is formed between the abstract mobile phone and the mobile phone of a specific brand. The abstract mobile phone is the parent class and the mobile phone of a specific brand is its subclass.
  • Product group: product group refers to a group of products produced by the same factory and located in different product structures. For example, Huawei mobile phones and bracelets produced by Huawei manufacturers, Huawei mobile phones belong to the mobile phone product structure, while Huawei bracelets belong to the bracelet product structure.

Specific code examples:

<?php

interface Phone
{
    public function open();

    public function operate();
}

class HuaweiPhone implements Phone
{
    public function open()
    {
        echo "Turn on Huawei mobile phone";
    }

    public function operate()
    {
        echo "Operate Huawei mobile phone";
    }
}

interface Bracelet
{
    public function look();

    public function click();
}

class XiaomiBracelet implements Bracelet
{
    public function look()
    {
        echo "View Xiaomi Bracelet";
    }

    public function click()
    {
        echo "Click Xiaomi Bracelet";
    }
}

abstract class Factory
{
    abstract public static function createPhone();

    abstract public static function createBracelet();
}

class ProductFactory extends Factory{
    public static function createPhone()
    {
        return new HuaweiPhone();
    }

    public static function createBracelet()
    {
        return new XiaomiBracelet();
    }
}

$newPhone = ProductFactory::createPhone();
$newPhone->open();
$newPhone->operate();

$newBracelet = ProductFactory::createBracelet();
$newBracelet->look();
$newBracelet->click();

The above are my learning opinions on the factory model. I hope it will help some colleagues in understanding it. Thank you for your attention!

Keywords: PHP Design Pattern

Added by xploita on Sun, 19 Dec 2021 16:02:23 +0200