abstract class Prototype
{
protected $name;
abstract public function copy();
public function setName($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
}
class ConcretePrototype extends Prototype
{
public function copy()
{
return clone $this;
}
}
$prototype = new ConcretePrototype();
$prototype->setName('Prototype');
$copyPrototype = $prototype->copy();
$copyPrototype->setName('Copy of Prototype');
var_dump($prototype->getName());
var_dump($copyPrototype->getName());
PHP代码实现原型模式
最新推荐文章于 2024-07-28 08:43:29 发布