Home>Article>Backend Development> PHP object-oriented development - prototype mode
The prototype pattern is a powerful transformation of the abstract factory pattern/content/10866786.html. Simply put, it combines several factory classes in the abstract factory pattern into a central control class, which is responsible for generating objects.
engineNorms=$engineNorms; $this->bodyNorms=$bodyNorms; $this->whellNorms=$whellNorms; } public function getEngineNorms(){ return clone $this->engineNorms; } public function getBodyNorms(){ return clone $this->bodyNorms; } public function getWhellNorms(){ return clone $this->whellNorms; } } $carFactory=new factory(new carEngine(),new carBody(),new carWhell()); $car['engine']=$carFactory->getEngineNorms()->engine(); $car['body']=$carFactory->getBodyNorms()->body(); $car['whell']=$carFactory->getWhellNorms()->whell(); print_r($car); $busFactory=new factory(new busEngine(),new busBody(),new busWhell()); $bus['engine']=$busFactory->getEngineNorms()->engine(); $bus['body']=$busFactory->getBodyNorms()->body(); $bus['whell']=$busFactory->getWhellNorms()->whell(); print_r($bus); ?>
The prototype mode reduces the amount of code, and when returning the object, you can add custom operations, which is very flexible and convenient. But it should be noted that the prototype mode uses the clone method. Please pay attention to the shallow copy problem caused by clone. That is, if the properties of the cloned object contain objects, then the clone will not get a new copy, but the same reference. .
The above is the content of PHP object-oriented development - prototype mode. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!