当前位置:首页 > PHP教程 > PHP高级教程

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

复制代码 代码如下:

<?php
/**
* 原型模式
*
* 用原型实例指定创建对象的种类.并且通过拷贝这个原型来创建新的对象
*
*/
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()."<br/>";
$objprototype2 = $objprototype;
echo $objprototype2->getid()."<br/>";
$objprototype3 = $objprototype->getclone();
echo $objprototype3->getid()."<br/>";


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!