Home  >  Article  >  Backend Development  >  PHP design pattern Prototype (prototype mode) code_PHP tutorial

PHP design pattern Prototype (prototype mode) code_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:28:11739browse

复制代码 代码如下:

/**
* Prototype mode
*
* Use a prototype instance to specify the type of object to create. And create a new object by copying this prototype
*
*/
abstract class Prototype
{
private $_id = null;
public function __construct($id)
{
$this->_id = $id;
}
public function getID()
{
return $this->_id;
}
public function __clone() // magic function
{
$this->_id += 1;
}
public function getClone()
{
return clone $this;
}
}
class ConcretePrototype extends Prototype
{
}
//
$objPrototype = new ConcretePrototype(0);
$objPrototype1 = clone $objPrototype;
echo $objPrototype1->getID()."
";
$objPrototype2 = $objPrototype;
echo $objPrototype2->getID()."
";
$objPrototype3 = $objPrototype->getClone();
echo $objPrototype3->getID()."
";

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/323625.htmlTechArticle复制代码 代码如下: ?php /*** Prototype pattern * * Use a prototype instance to specify the type of object to be created. And create a new object by copying this prototype **/ abstract class Prototype...
Statement:
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