<?php
class Company
{
public $company;
public function setName($name)
{
$this->company = $name;
}
public function getName()
{
return $this->company;
}
}
Class Resume {
public $name;
public $age;
public $company;
public $timeArea;
public $sex;
public function __construct($name)
{
$this->name = $name;
$this->company = new Company();
}
public function setPersonalInfo($age, $name)
{
$this->age = $age;
$this->name = $name;
}
public function setWorkExperience($timeArea, $company)
{
$this->timeArea = $timeArea;
$this->company->setName($company);
}
public function display()
{
echo $this->name." ".$this->sex." ".$this->age."\n";
echo $this->timeArea." ".$this->company->getName()."\n";
}
public function __clone()
{
$this->company = clone $this->company;
}
}
$resume = new Resume("大鸟");
$resume->setPersonalInfo("男", 29);
$resume->setWorkExperience("1998-2000","xxx 公司");
$resume->display();
$resume2 = clone $resume;
$resume2->setPersonalInfo("男", 40);
$resume2->setWorkExperience("1998-2010","xx 公司");
$resume2->display();
总结
原型模式,用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。
原型模式其实就是从一个对象再创建另外一个可定制的对象,而且不需要知道任何创建的细节。
一般在初始化的信息不发生变化的情况下,克隆是最好的办法。既隐藏了对象创建的细节,又对性能是大大的提高。
|