三個(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連接局域網(wǎng)MYSQL數(shù)據(jù)庫(kù)的簡(jiǎn)單實(shí)例
這篇文章介紹了PHP連接局域網(wǎng)MYSQL數(shù)據(jù)庫(kù)的簡(jiǎn)單實(shí)例,有需要的朋友可以參考一下2013-08-08
Yii框架操作cookie與session的方法實(shí)例詳解
這篇文章主要介紹了Yii框架操作cookie與session的方法,結(jié)合實(shí)例形式詳細(xì)分析了Yii針對(duì)cookie與session操作的常用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2019-09-09
thinkPHP訂單數(shù)字提醒功能的實(shí)現(xiàn)方法
這篇文章主要介紹了thinkPHP訂單數(shù)字提醒功能的實(shí)現(xiàn)方法,涉及thinkPHP數(shù)據(jù)庫(kù)查詢、遍歷及前臺(tái)顯示相關(guān)功能實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-12-12
PHP中的使用curl發(fā)送請(qǐng)求(GET請(qǐng)求和POST請(qǐng)求)
本篇文章主要介紹了PHP中的使用curl發(fā)送請(qǐng)求(GET請(qǐng)求和POST請(qǐng)求),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02
Django中datetime的處理方法(strftime/strptime)
這篇文章主要介紹了Django中datetime的處理方式(strftime/strptime),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-07-07

