三個(gè)類概括PHP的五種設(shè)計(jì)模式
更新時(shí)間:2012年09月05日 23:26:13 作者:
PHP的五種設(shè)計(jì)模式主要包括工廠模式,單元素模式,觀察者模式,命令鏈模式,策略模式
工廠模式
單元素模式
觀察者模式
命令鏈模式
策略模式
class people {
private $name = '';
private $user = null;
private function __constract($name){/*此處private定義輔助實(shí)現(xiàn) 單元素模式*/
$this->name = $name;
}
public static function instance($name){/*此方法實(shí)現(xiàn) 工廠模式*/
static $object = null;/*此變量實(shí)現(xiàn) 單元素模式*/
if (is_null($object))
$object = new people($name);
return $object;
}
public function work_in($who=null)
{
if (is_null($who)) echo 'error';
else {
$this->user[] = $who;/*此數(shù)組變量實(shí)現(xiàn) 觀察者模式*/
echo $who->work();/*此方法調(diào)用實(shí)現(xiàn) 策略模式*/
}
}
public function on_action($which=''){
if (empty($which)) echo 'error';
else {
foreach ($this->user as $user)
$user->action($which);/*此方法調(diào)用實(shí)現(xiàn) 命令鏈模式*/
}
}
}
$people = people::instance('jack');
$people->work_in(new student);
$people->work_in(new teacher);
$people->on_action('eat');
class student {
function work(){
echo '<br/>我是學(xué)生,朝九晚五。';
}
function action($which){
if (method_exists($this, $which)) return $this->$which();
else echo 'you are wrong!';
}
function eat(){
echo '<br/>我是學(xué)生,只能吃套餐。';
}
}
class teacher {
function work(){
echo '<br/>我是老師,晚上備課最忙。';
}
function action($which){
if (method_exists($this, $which)) return $this->$which();
else echo 'i can not do it!';
}
function eat(){
echo '<br/>我是老師,可以每天吃大餐。';
}
}
單元素模式
觀察者模式
命令鏈模式
策略模式
復(fù)制代碼 代碼如下:
class people {
private $name = '';
private $user = null;
private function __constract($name){/*此處private定義輔助實(shí)現(xiàn) 單元素模式*/
$this->name = $name;
}
public static function instance($name){/*此方法實(shí)現(xiàn) 工廠模式*/
static $object = null;/*此變量實(shí)現(xiàn) 單元素模式*/
if (is_null($object))
$object = new people($name);
return $object;
}
public function work_in($who=null)
{
if (is_null($who)) echo 'error';
else {
$this->user[] = $who;/*此數(shù)組變量實(shí)現(xiàn) 觀察者模式*/
echo $who->work();/*此方法調(diào)用實(shí)現(xiàn) 策略模式*/
}
}
public function on_action($which=''){
if (empty($which)) echo 'error';
else {
foreach ($this->user as $user)
$user->action($which);/*此方法調(diào)用實(shí)現(xiàn) 命令鏈模式*/
}
}
}
$people = people::instance('jack');
$people->work_in(new student);
$people->work_in(new teacher);
$people->on_action('eat');
class student {
function work(){
echo '<br/>我是學(xué)生,朝九晚五。';
}
function action($which){
if (method_exists($this, $which)) return $this->$which();
else echo 'you are wrong!';
}
function eat(){
echo '<br/>我是學(xué)生,只能吃套餐。';
}
}
class teacher {
function work(){
echo '<br/>我是老師,晚上備課最忙。';
}
function action($which){
if (method_exists($this, $which)) return $this->$which();
else echo 'i can not do it!';
}
function eat(){
echo '<br/>我是老師,可以每天吃大餐。';
}
}
相關(guān)文章
PHP使用array_multisort對(duì)多個(gè)數(shù)組或多維數(shù)組進(jìn)行排序
這篇文章主要介紹了PHP使用array_multisort對(duì)多個(gè)數(shù)組或多維數(shù)組進(jìn)行排序,需要的朋友可以參考下2014-12-12PHP和javascript常用正則表達(dá)式及用法實(shí)例
這篇文章主要介紹了常用的PHP和javascript正則表達(dá)式及用法實(shí)例,精心收集的PHP和javascript正則表達(dá)式各10個(gè),需要的朋友可以參考下2014-07-07美圖秀秀web開放平臺(tái)--PHP流式上傳和表單上傳示例分享
最近需要開發(fā)一個(gè)頭像上傳的功能,找了很多都需要授權(quán)的,后來找到了美圖秀秀,功能非常好用。2014-06-06php opendir()列出目錄下所有文件的實(shí)例代碼
這篇文章主要介紹了php opendir()列出目錄下所有文件的實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-10-10