Simple factory mode of PHP design pattern

Time:2021-4-25

PHP simple factory

The factory class of PHP simple factory mode generally uses static methods to return different object instances by receiving different parameters.

That is to say, different objects are generated by passing parameters.

Simple factory

Official PHP advanced learning exchange community “click”The management has sorted out some materials, bat and other first-line large factories’ 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, swoole, swoft, Kafka, MySQL optimization, shell script, docker, micro service Nginx and other knowledge points advanced dry goods

A simple factory needs to have three roles:

  • Product interface class: used to define product specification;

  • Specific product implementation, such as concreateproducta, concreateproductb;

  • Simplefactory: used to generate specific products.

When using, you can generate the desired product by passing parameters to the simple factory class.

code: ICar.php : define product specifications:

Namespace YJC / simplefactory;

Interface ICAR
{
    Common 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.';
    }
}

Simple factory class simplefactory:

Namespace YJC / simplefactory;

Class simplefactory
{
    Public static function makecar ($){
         Switch ($){
             Case 'Benz':
                 Return to new Benz ();
                Rest;
            In this case, 'BMW':
                 Return to new BMW ();
                Rest;
            Default value:
                 Throw new exception ('type not supported ');
                Break through;
        }
    }
}

Parameter code:

$car = simplefactory:: makecar ('benz ');
$ car- > driver();

Advantages / disadvantages of simple factories

Advantages: the simple factory pattern can decide exactly the object of a specific class to be created according to the external given information. It is helpful to optimize the whole software architecture by clearly distinguishing their responsibilities and powers.

Disadvantages: it is obvious that the factory category centralizes the creation logic of all instances, which completely violates grspr’s highly cohesive principle of responsibility allocation.

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