Simple factory pattern of PHP design pattern * interpretation

"PHP" Simple Factory

The factory class of the PHP simple factory pattern generally uses static methods to return different object instances through the difference of the received parameters.

That is, when using, pass parameters to determine the generation of different objects.

Simple factory :

Official PHP advanced learning exchange community "click" management to organize some materials, BAT and other first-line companies have advanced knowledge systems (relevant learning materials and written interview questions) and are not limited to: distributed architecture, high scalability, high Performance, high concurrency, server performance tuning, TP6, laravel, YII2, Redis, Swoole, Swoft, Kafka, Mysql optimization, shell scripts, Docker, microservices, Nginx and other knowledge points, advanced advanced dry goods

The simple factory needs to have 3 roles:

  • Product interface class: used to define product specifications;

  • Specific product realization, such as ConcreateProductA, ConcreateProductB;

  • Simple factory class SimpleFactory: used to generate specific products.

When using, pass parameters to the simple factory class to generate the desired product.

Code: ICar.php: Define product specifications:

命名空间 Yjc \ SimpleFactory ;

接口 ICar
{
    公共 功能 驱动程序();
}

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.';
    }
}

SimpleFactory class SimpleFactory:

命名空间 Yjc \ SimpleFactory ;

类 SimpleFactory
{
    公共 静态 功能 makeCar($型){
         开关($型){
             案 '奔驰':
                 返回 新 奔驰();
                休息 ;
            情况下, 'BMW' :
                 返回 新 宝马();
                休息 ;
            默认值:
                 抛出 new \ Exception('不支持类型!');
                打破;
        }
    }
}

Parameter code:

$车 = SimpleFactory :: makeCar( '奔驰');
$ car- > driver();

Advantages/disadvantages of simple factories :

Advantages: The simple factory pattern can determine exactly which objects of a specific class should be created based on externally given information. The respective responsibilities and powers are clearly distinguished, which is conducive to the optimization of the entire software architecture.

Disadvantages: Obviously, the factory category concentrates the creation logic of all instances, which completely violates GRSPR's principle of high cohesion.

The Growth Path of PHP Internet Architect * The Ultimate Guide to "Design Patterns"

PHP Internet Architect 50K Growth Guide + Industry Problem Solving Guide (Continuous Update)

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

★If you like my article and want to communicate and learn with more senior developers, get more technical consultation and guidance related to interviews with major companies. Welcome to join our group-click here .

Guess you like

Origin blog.csdn.net/weixin_43814458/article/details/108112827