Home > Backend Development > PHP Tutorial > PHP object-oriented development - prototype mode

PHP object-oriented development - prototype mode

黄舟
Release: 2023-03-04 12:36:02
Original
1820 people have browsed it

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.

<?php
//生产引擎的标准
interface engineNorms{
	function engine();
}

class carEngine implements engineNorms{

	public function engine(){
		return &#39;汽车引擎&#39;;
	}

}

class busEngine implements engineNorms{
	
	public function engine(){
		return &#39;巴士车引擎&#39;;
	}
	
}

//生产车身的标准
interface bodyNorms{
	function body();
}

class carBody implements bodyNorms{

	public function body(){
		return &#39;汽车车身&#39;;
	}

}

class busBody implements bodyNorms{
	
	public function body(){
		return &#39;巴士车车身&#39;;
	}
	
}

//生产车轮的标准
interface whellNorms{
	function whell();
}

class carWhell implements whellNorms{

	public function whell(){
		return &#39;汽车轮子&#39;;
	}

}

class busWhell implements whellNorms{
	
	public function whell(){
		return &#39;巴士车轮子&#39;;
	}
	
}

//原型工厂
class factory{

	private $engineNorms;
	private $bodyNorms;
	private $whellNorms;
	
	public function __construct(engineNorms $engineNorms,bodyNorms $bodyNorms,whellNorms $whellNorms){
		$this->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[&#39;engine&#39;]=$carFactory->getEngineNorms()->engine();
$car[&#39;body&#39;]=$carFactory->getBodyNorms()->body();
$car[&#39;whell&#39;]=$carFactory->getWhellNorms()->whell();
print_r($car);

$busFactory=new factory(new busEngine(),new busBody(),new busWhell());
$bus[&#39;engine&#39;]=$busFactory->getEngineNorms()->engine();
$bus[&#39;body&#39;]=$busFactory->getBodyNorms()->body();
$bus[&#39;whell&#39;]=$busFactory->getWhellNorms()->whell();
print_r($bus);
?>
Copy after login

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 (www.php.cn)!


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template