2018/04/16 Factory Pattern of PHP Design Pattern

Learning design patterns must know what it is for, everything must have a reason.

Standing on the shoulders of giants to learn, recommend two articles

  What is the php factory pattern? Why use the php factory pattern?

  Summary of usage examples of php factory pattern

  Factory pattern of PHP design pattern

--

Imagine a situation:

  Now we have created a new mysqli extension class in our program, which encapsulates a series of database operations.

  When I need it, I use new mysqli() to call my database wrapper method.

  However, one day, I found that mysqli was not working, and I wanted to use PDO.

  So......

  I have used the new mysqli class hundreds of times in my program, do I still need to change them one by one?

--

The embarrassment in ordinary times has been explained above, so we need to decouple this class.

It is only now that I feel the breadth and depth of "high cohesion and low coupling". No problem, a factory method does the trick.

The advantage of the factory pattern is in creating objects.

The advantage of the factory pattern is in creating objects. Establish a factory (a function or a class method) to create new objects, its task is to encapsulate the creation process of objects,

Creating an object is not the way to use new anymore. Instead, define a method that creates an object instance.

--

Speaking of this, I'm already confused anyway. In general, the purpose of the factory pattern is to encapsulate the process of creating a new object and only return the result of the object.

--

Below we use the code to achieve the following

1: Simple Factory Pattern

【Scope of application】

The factory class is responsible for creating fewer objects. You only need to know the parameters passed to the factory class during operation, and you don't need to care about how to create the object.

<?php
class A {}

class B {}

class Factoty
{
    // static method in simple factory - used to create A object
    static function createA()
    {
        return new A();
    }

    // static method in simple factory - used to create B object
    static function createB()
    {
        return new B();
    }
}

$A = Factoty::createA();
$B = Factoty::createB();

Here we hide the details of the instance object, and only need to modify the simple code to complete the object modification.

But he is not suitable for repeated addition of objects, only for fixed objects.

--

2: Factory Method Pattern

【Scope of application】

  When a class wants the object it creates to be specified by the subclass, in short, it hopes that the implementation of the parent class can be implemented by the inheritance of the subclass, so that different classes can be extended, which also reflects the polymorphism of the class.

// define an interface class
interface  people
{
    public function  create();
}

// Implemented by subclasses
class Factory implements people
{
    public function create()
    {
        return new A();
    }
}

This allows you to extend multiple subclasses all the time. And it has good scalability.

--

In fact, about the factory model, we must find his focus. What does he want to do?

The factory pattern is mainly to solve the high coupling generated in the creation of objects, so that they can be modularized and loosely combined.

As long as we know what he wants to do, the factory pattern can be implemented in many ways.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324486097&siteId=291194637