The factory method of PHP design pattern

Time:2021-4-19

The PHP factory method provides a factory class for each product. Create different product instances through different factory instances.

Compared with a simple factory, creating objects is no longer left to a class: split the simple factory, each product is implemented by a special simple factory, and each simple factory implements the factory interface class. In this way, any product can be added in the same hierarchy.

Factory method UML diagram:

Official PHP advanced learning exchange community “click”The management has sorted out some materials, bat and other advanced knowledge systems are ready (relevant learning materials and written questions), and not limited to: distributed architecture, high scalability, high performance, high concurrency, server performance tuning, tp6, laravel, yii2, redis, spool, swoft, Kafka, MySQL optimization, shell script, docker, microservice, nginx and other advanced knowledge points Advanced dry goods

A simple factory needs four roles

  • Product interface class: used to define product specifications;

  • Specific product implementation, such as concreateproducta, concreateproductb;

  • Abstract factory class ifactory: used to standardize factories;

  • Simple factories created for specific products, such as concreatefactory A and concreatefactory B.

code: ICar.php : define product specifications:

namespace Yjc\SimpleFactory;

interface ICar
{
    public function driver();
}

Specific product realization:

namespace Yjc\SimpleFactory;

class Benz implements ICar
{
    public function driver()
    {
        echo 'benz driver.';
    }
}

class Bmw implements ICar
{
    public function driver()
    {
        echo 'bmw driver.';
    }
}

Abstract factory class ifactory:

namespace Yjc\Factory;

interface IFactory
{
    public static function makeCar();
}

Specific factory implementation:

namespace Yjc\Factory;

class FactoryBenz implements IFactory
{
    public static function makeCar()
    {
        return new Benz();
    }
}

class FactoryBmw implements IFactory
{
    public static function makeCar()
    {
        return new Bmw();
    }
}

Test:

$car = Factory\FactoryBenz::makeCar();
$car->driver();

Thinking: if we don’t use factory mode to implement our example, maybe the code will be reduced a lot – we just need to implement the existing car instead of polymorphism. But in terms of maintainability and extensibility, it is very poor (you can imagine the classes to be affected after adding a car). Therefore, in order to improve scalability and maintainability, it is worth writing more code, especially in complex projects.

  • advantage:
  1. The factory method mode can reduce the burden of the factory class, and let a certain class / thing be produced by a factory; (corresponding to the disadvantage of simple factory 1)

  2. At the same time, adding a certain kind of “thing” does not need to modify the factory class, just need to add the factory that produces this kind of “thing”, so that the factory class conforms to the open closed principle.

  • Disadvantages:
  1. Compared with a simple factory, the implementation is slightly more complex.

  2. It is more complex to deal with some cases that can form product family.

For defect 2, we can use [abstract factory] to implement it.

PHP Internet architect’s growth path * “design patterns” Ultimate Guide

PHP Internet architect 50K growth Guide + general outline of industry problem solving (continuous update)

Interview 10 companies, get 9 offers, PHP interview questions in 2020

If you like my article and want to exchange and study with more senior developers, you can get more technical consultation and guidance related to the interview of large factories. Welcome to join usOur group – click here

If the content is good, I hope you can support and encourage me to like it. Welcome to communicate with me; In addition, if you have any questions and want to see the content can be put forward in the comments

Recommended Today

Large scale distributed storage system: Principle Analysis and architecture practice.pdf

Focus on “Java back end technology stack” Reply to “interview” for full interview information Distributed storage system, which stores data in multiple independent devices. Traditional network storage system uses centralized storage server to store all data. Storage server becomes the bottleneck of system performance and the focus of reliability and security, which can not meet […]