T
T
thorii2016-11-02 17:20:05
PHP
thorii, 2016-11-02 17:20:05

Am I understanding the prototype pattern correctly?

class ResponseClassPrototype extends PrototypeAbstract implements Clonable {
 private $proto = null;

 public function __construct(Response $class) 
 {
   $this->proto = $class;
 }

 public function getClone() 
 {
   return clone $this->proto();
 };

 public function json() 
 {
   $response = $this->getClone();
   $response->setHeader('Content-type', 'application/json');

   return $response;
 }

//function html();
// ...

}

I suspect that both the factory and the prototype are mixed here. This is true? Is it even possible to do this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2016-11-02
@thorii

Have you read exactly what kind of pattern this is and how it differs from the factory?
In general, you can answer the question if you give an example of using this pattern. Well, also consider this point - in your example, using a prototype is not rational - cloning objects such as request is not very expensive.
ps the `getClone` method must be private. Everything else seems to be ok.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question