Heim  >  Artikel  >  Backend-Entwicklung  >  php设计模式 Prototype (原型模式)代码_PHP教程

php设计模式 Prototype (原型模式)代码_PHP教程

WBOY
WBOYOriginal
2016-07-21 15:28:11742Durchsuche

复制代码 代码如下:

/**
* 原型模式
*
* 用原型实例指定创建对象的种类.并且通过拷贝这个原型来创建新的对象
*
*/
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 /** * 原型模式 * * 用原型实例指定创建对象的种类.并且通过拷贝这个原型来创建新的对象 * */ abstract class Prototype...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn