php設(shè)計模式 Prototype (原型模式)代碼
更新時間:2011年06月26日 10:33:15 作者:
用原型實例指定創(chuàng)建對象的種類.并且通過拷貝這個原型來創(chuàng)建新的對象
復(fù)制代碼 代碼如下:
<?php
/**
* 原型模式
*
* 用原型實例指定創(chuàng)建對象的種類.并且通過拷貝這個原型來創(chuàng)建新的對象
*
*/
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/>";
相關(guān)文章
php函數(shù)serialize()與unserialize()用法實例
這篇文章主要介紹了php函數(shù)serialize()與unserialize()用法,以實例形式詳細(xì)講述了php函數(shù)serialize()與unserialize()的適用情況與使用方法,具有很好的參考借鑒價值,需要的朋友可以參考下2014-11-11php array_chunk()函數(shù)用法與注意事項
這篇文章主要介紹了php array_chunk()函數(shù)用法與注意事項,結(jié)合實例形式分析了php數(shù)組分割函數(shù)array_chunk()相關(guān)功能、用法及操作注意事項,需要的朋友可以參考下2019-07-07