原型模式是创建型模式的一种,最大的优点是不用每次创建对象,利用克隆减少系统开销。
创建抽象类
abstract class cloneMe{
public $name;
public $picture;
abstract function __clone();
}
创建克隆方法
class Person extends cloneMe
{
public function __construct()
{
$this->picture='cloneMan';
$this->name ='orignal';
}
public function display()
{
echo 'this picture is'.$this->picture."<br/>";
echo $this->name."<br/>";
}
function __clone() {
;
}
}
$worker = new Person();
$worker->display();
$slacker = clone $worker;
$slacker->name = 'cloned';
$slacker->display();