What is a Factory Design Pattern in PHP?

后端 未结 9 946
清歌不尽
清歌不尽 2020-11-27 10:15

This confuses me, in the most simplest terms what does it do? Pretend you are explaining to your mother or someone almost please.

相关标签:
9条回答
  • 2020-11-27 11:07

    A factory just generates an object or objects.

    You may have a factory that builds a MySQL connection.

    http://en.wikipedia.org/wiki/Factory_method_pattern

    0 讨论(0)
  • 2020-11-27 11:07

    This answer is in relation to other post in which Daniel White said to use factory for creating MySQL connection using factory pattern.

    For MySQL connection I would rather use singleton pattern as you want to use same connection for accessing the database not create another one.

    0 讨论(0)
  • 2020-11-27 11:18

    A factory creates an object. So, if you wanted to build

     class A{
        public $classb;
        public $classc;
        public function __construct($classb, $classc)
        {
             $this->classb = $classb;
             $this->classc = $classc;
        }
      }
    

    You wouldn't want to rely on having to do the following code everytime you create the object

    $obj = new ClassA(new ClassB, new Class C);
    

    That is where the factory would come in. We define a factory to take care of that for us:

    class Factory{
        public function build()
        {
            $classc = $this->buildC();
            $classb = $this->buildB();
            return $this->buildA($classb, $classc);
    
        }
    
        public function buildA($classb, $classc)
        {
            return new ClassA($classb, $classc);
        }
    
        public function buildB()
        {
            return new ClassB;
        }
    
        public function buildC()
        {
            return new ClassC;
        }
    }
    

    Now all we have to do is

    $factory = new Factory;
    $obj     = $factory->build();
    

    The real advantage is when you want to change the class. Lets say we wanted to pass in a different ClassC:

    class Factory_New extends Factory{
        public function buildC(){
            return new ClassD;
        }
    }
    

    or a new ClassB:

    class Factory_New2 extends Factory{
        public function buildB(){
            return new ClassE;
        }
    }
    

    Now we can use inheritance to easily modify how the class is created, to put in a different set of classes.

    A good example might be this user class:

    class User{
        public $data;
        public function __construct($data)
        {
            $this->data = $data;
        }
    }
    

    In this class $data is the class we use to store our data. Now for this class, lets say we use a Session to store our data. The factory would look like this:

    class Factory{
        public function build()
        {
            $data = $this->buildData();
            return $this->buildUser($data);
        }
    
        public function buildData()
        {
            return SessionObject();
        }
    
        public function buildUser($data)
        {
            return User($data);
        }
    }
    

    Now, lets say instead we want to store all of our data in the database, it is really simple to change it:

    class Factory_New extends Factory{
        public function buildData()
        {
            return DatabaseObject();
        }
    }
    

    Factories are a design pattern we use to control how we put objects together, and using correct factory patterns allows us to create the customized objects we need.

    0 讨论(0)
提交回复
热议问题