What is a Factory Design Pattern in PHP?

后端 未结 9 939
清歌不尽
清歌不尽 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 10:54

    Factory design pattern is very good when you are dealing with multiple resources and want to implement high level abstraction.

    Let's break this into different section.

    Suppose you have to implement abstraction and the user of your class doesn't need to care about what you've implemented in class definition.

    He/She just need to worry about the use of your class methods.

    e.g. You have two databases for your project

    class MySQLConn {
    
            public function __construct() {
                    echo "MySQL Database Connection" . PHP_EOL;
            }
    
            public function select() {
                    echo "Your mysql select query execute here" . PHP_EOL;
            }
    
    }
    
    class OracleConn {
    
            public function __construct() {
                    echo "Oracle Database Connection" . PHP_EOL;
            }
    
            public function select() {
                    echo "Your oracle select query execute here" . PHP_EOL;
            }
    
    }
    

    Your Factory class would take care of the creation of object for database connection.

    class DBFactory {
    
            public static function getConn($dbtype) {
    
                    switch($dbtype) {
                            case "MySQL":
                                    $dbobj = new MySQLConn();
                                    break;
                            case "Oracle":
                                    $dbobj = new OracleConn();
                                    break;
                            default:
                                    $dbobj = new MySQLConn();
                                    break;
                    }
    
                    return $dbobj;
            }
    
    }
    

    User just need to pass the name of the database type

    $dbconn1 = DBFactory::getConn("MySQL");
    $dbconn1->select();
    

    Output:

    MySQL Database Connection
    Your mysql select query execute here
    

    In future you may have different database then you don't need to change the entire code only need to pass the new database type and other code will run without making any changes.

    $dbconn2 = DBFactory::getConn("Oracle");
    $dbconn2->select();
    

    Output:

    Oracle Database Connection
    Your oracle select query execute here
    

    Hope this will help.

    0 讨论(0)
  • 2020-11-27 10:56

    Factory Design Pattern (Factory Pattern) is for loose coupling. Like the meaning of factory, data to a factory (produce data) to final user. By this way, the factory break the tight coupling between source of data and process of data.

    0 讨论(0)
  • 2020-11-27 10:56

    The classic approach to instantiate an object is:

    $Object=new ClassName();
    

    PHP has the ability to dynamically create an object from variable name using the following syntax:

    $Object=new $classname;
    

    where variable $classname contains the name of class one wants to instantiate.

    So classic object factoring would look like:

    function getInstance($classname)
    {
      if($classname==='Customer')
      {
        $Object=new Customer();
      }
      elseif($classname==='Product')
      {
        $Object=new Product();
      }
      return $Object;
    }
    

    and if you call getInstance('Product') function this factory will create and return Product object. Otherwise if you call getInstance('Customer') function this factory will create and return Customer type object (created from Customer() class).

    There's no need for that any more, one can send 'Product' or 'Customer' (exact names of existing classes) as a value of variable for dynamic instantiation:

    $classname='Product';
    $Object1=new $classname; //this will instantiate new Product()
    
    $classname='Customer';
    $Object2=new $classname; //this will instantiate new Customer()
    
    0 讨论(0)
  • 2020-11-27 10:59

    Like a real life factory, it creates something and returns it.

    Imagine something like this

    $joe = new Joe();
    $joe->say('hello');
    

    or a factory method

    Joe::Factory()->say('hello');
    

    The implementation of the factory method will create a new instance and return it.

    0 讨论(0)
  • 2020-11-27 10:59

    For the record, in easy words, a factory like @Pindatjuh said, returns a object.

    So, what's the difference with a constructor? (that does the same)

    1. a constructor uses his own instance.
    2. Something i want to so something more advanced and i don't want to bloat the object (or add dependencies).
    3. Constructor is called when each instance is created. Sometimes you don't want that.

      For example, let's say that every time i creates an object of the class Account, i read from the database a file and use it as a template.

    Using constructor:

    class Account {
          var $user;
          var $pwd;
          var ...
          public __construct() {
             // here i read from the file
             // and many other stuff
          }
    }
    

    Using factory:

    class Account {
          var $user;
          var $pwd;
          var ...
    }
    class AccountFactory {
          public static Create() {
             $obj=new Account();
             // here we read the file and more stuff.
             return $obj;
          }
    
    0 讨论(0)
  • 2020-11-27 11:02

    In general a "factory" produces something: in the case of Object-Orientated-Programming, a "factory design pattern" produces objects.

    It doesn't matter if it's in PHP, C# or any other Object-Orientated language.

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