PHP原型模式Prototype Pattern的使用介绍(php实现原理)怎么可以错过

随心笔谈12个月前发布 admin
87 0

<?php
// 原型接口
interface Prototype
{
public function clone();
}
// 具体原型类
class ConcretePrototype implements Prototype
{
private $name;
public function __construct($name)
{
$this->name=$name;
}
public function clone()
{
return new ConcretePrototype($this->name);
}
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name=$name;
}
}
// 客户端代码
$prototype=new ConcretePrototype(“Prototype”);
$clone=$prototype->clone();
echo $clone->getName(); // 输出 “Prototype”

© 版权声明

相关文章