php - Why use "getObject" method when writing a Factory class? -


typically factory class contains method getobject.

thereby

class factory {     private $type;      function __construct($type)     {         $this->type = $type;     }      function getobject()     {         //example brevity show use of $type variable         if ($this->type) $object = new $type();         return $object;     } } 

question: why not return object straight via constructor?

class factory {     function __construct($type)     {         if ($type) $object = new $type();         return $object;     }  } 

because can not return except own instance constructor. whole point of constructor set instance. whole point of factory abstract complex construction / setup logic user.

a factory class has static method like:

class foo {     public function __construct($x, $y) {         //     }      // factory method     public static function createfrompoint(point $point) {         return new self($point->x, $point->y);     } }  $foo = foo::createfrompoint(new point(1, 1)); // makes no sense shows concept 

Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -