php设计模式 Prototype (原型模式)代码
作者:bea
代码如下: <?php /** * 原型模式 * * 用原型实例指定创建对象的种类.并且通过拷贝这个原型来创建新的对象 * */ abstract class Prototype { private $_id = null; public function __construct($id) { $this->_id = $id; } public function getID() { return $this->_id;
代码如下:
<?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/>";
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- php设计模式 Builder(建造者模式)
- php设计模式 DAO(数据访问对象模式)
- php设计模式 Decorator(装饰模式)
- php设计模式 Delegation(委托模式)
- php设计模式 Facade(外观模式)
- php设计模式 Factory(工厂模式)
- php设计模式 Interpreter(解释器模式)
- php设计模式 Strategy(策略模式)
- php设计模式 Observer(观察者模式)
- php设计模式 Singleton(单例模式)
- php设计模式 Command(命令模式)
- php设计模式 Template (模板模式)
- php设计模式 Proxy (代理模式)
- php设计模式 Composite (组合模式)
- php设计模式 State (状态模式)
- php设计模式 Bridge (桥接模式)
- php设计模式 Chain Of Responsibility (职责链模式)
- php设计模式 FlyWeight (享元模式)
- php设计模式 Mediator (中介者模式)