《PHP設(shè)計(jì)模式介紹》第十二章 裝飾器模式(3)_PHP教程
推薦:《PHP設(shè)計(jì)模式介紹》第十一章 代理模式因?yàn)槟硞(gè)對(duì)象消耗太多資源,而且你的代碼并不是每個(gè)邏輯路徑都需要此對(duì)象, 你曾有過(guò)延遲創(chuàng)建對(duì)象的想法嗎 ( if和else就是不同的兩條邏輯路徑) ? 你有想過(guò)限制訪問(wèn)某個(gè)對(duì)象,也就是說(shuō),提供一組方法
作為開始,我們建立一個(gè)普通的可以被擴(kuò)展產(chǎn)生具體的特定裝飾器的WidgetDecorator類。至少WidgetDecorator類應(yīng)該能夠在它的構(gòu)造函數(shù)中接受一個(gè)組件,并復(fù)制公共方法paint。
| class WidgetDecorator { var $widget; function WidgetDecorator(&$widget) { $this->widget =& $widget; } function paint() { return $this->widget->paint(); } } |
為建立一個(gè)標(biāo)簽(lable),需要傳入lable的內(nèi)容,以及原始的組件:
| class Labeled extends WidgetDecorator { var $label; function Labeled($label, &$widget) { $this->label = $label; $this->WidgetDecorator($widget); } } |
有標(biāo)簽的組件也需要復(fù)制paint方法,并將標(biāo)簽信息增加到輸出中:
| class Labeled extends WidgetDecorator { var $label; function Labeled($label, &$widget) { $this->label = $label; $this->WidgetDecorator($widget); } function paint() { return ‘<b>’.$this->label.’:</b> ‘.$this->widget->paint(); } } |
你可以用一個(gè)測(cè)試檢驗(yàn)它:
| class WidgetTestCase extends UnitTestCase { function testLabeled() { $text =& new Labeled( ‘Email’ ,new TextInput(‘email’)); $output = $text->paint(); 208 The Decorator Pattern $this->assertWantedPattern(‘~^<b>Email:</b> <input~i’, $output); } } |
我們已經(jīng)看到TextInput和Labeled類的能力,你可以裝配一個(gè)類整體來(lái)管理表單(form)。
FormHandler類有一個(gè)靜態(tài)的build方法從表單的各種元素創(chuàng)建一個(gè)部件的數(shù)組。
| class FormHandlerTestCase extends UnitTestCase { function testBuild() { $this->assertIsA($form = FormHandler::build(new Post), ‘Array’); $this->assertEqual(3, count($form)); $this->assertIsA($form[1], ‘Labeled’); $this->assertWantedPattern(‘~email~i’, $form[2]->paint()); } } |
實(shí)現(xiàn)FormHandler 的代碼:
| class FormHandler { function build() { return array( new Labeled(‘First Name’, new TextInput(‘fname’)) ,new Labeled(‘Last Name’, new TextInput(‘lname’)) ,new Labeled(‘Email’, new TextInput(‘email’)) ); } } |
現(xiàn)在,這段代碼并不能工作—沒(méi)有通過(guò)$_post提交的數(shù)據(jù)。因?yàn)檫@段代碼必須要使用一個(gè)MockObject對(duì)象 (參見第6章)測(cè)試,現(xiàn)在我們可以將$_post數(shù)據(jù)包裝在一個(gè)類似哈希的對(duì)象中—與Registry(參見第五章)類似,或者模仿WACT的DataSource從Specification pattern
| class Post { var $store = array(); function get($key) { if (array_key_exists($key, $this->store)) return $this->store[$key]; } function set($key, $val) { $this->store[$key] = $val; } } |
想更方便的話,你可以使用Factory模式或者自動(dòng)填充的方法來(lái)從$_POST里面提取關(guān)鍵字。
| class Post { // ... function &autoFill() { $ret =& new Post; foreach($_POST as $key => $value) { $ret->set($key, $value); } return $ret; } } |
使用這個(gè)Post類,你可以編輯你的FormHandler::build() 方法,默認(rèn)使用已經(jīng)存在的$_post數(shù)據(jù):
| class FormHandler { function build(&$post) { return array( new Labeled(‘First Name’ , new TextInput(‘fname’, $post->get(‘fname’))) ,new Labeled(‘Last Name’ , new TextInput(‘lname’, $post->get(‘lname’))) ,new Labeled(‘Email’ , new TextInput(‘email’, $post->get(‘email’))) ); } } |
現(xiàn)在你可以創(chuàng)建一個(gè)php腳本使用FormHandler類來(lái)產(chǎn)生HTML表單:
| <form action=”formpage.php” method=”post”> <?php $post =& Post::autoFill(); $form = FormHandler::build($post); foreach($form as $widget) { echo $widget->paint(), “<br>\n”; } ?> <input type=”submit” value=”Submit”> </form> |
現(xiàn)在,你已經(jīng)擁有了一個(gè)提交給它自身并且能保持posted數(shù)據(jù)的表單處理(form handler) 類。
分享:《PHP設(shè)計(jì)模式介紹》第十章 規(guī)范模式在一個(gè)應(yīng)用軟件的成型過(guò)程中,一些意想不到的商業(yè)邏輯到處出現(xiàn)。比如,基于價(jià)格的考慮,這個(gè)任務(wù)必須減少項(xiàng)目;而那個(gè)任務(wù)也因?yàn)殇N售稅而必須選擇合適的比率;而其它的任務(wù)也必須因?yàn)槠渌奶貏e
- PHPNOW安裝Memcached擴(kuò)展方法詳解
- php記錄頁(yè)面代碼執(zhí)行時(shí)間
- PHP中獎(jiǎng)概率的抽獎(jiǎng)算法程序代碼
- apache設(shè)置靜態(tài)文件緩存方法介紹
- php對(duì)圖像的各種處理函數(shù)代碼小結(jié)
- PHP 關(guān)于訪問(wèn)控制的和運(yùn)算符優(yōu)先級(jí)介紹
- 關(guān)于PHP語(yǔ)言構(gòu)造器介紹
- php/js獲取客戶端mac地址的實(shí)現(xiàn)代碼
- php5.5新數(shù)組函數(shù)array_column使用
- PHP preg_match的匹配多國(guó)語(yǔ)言的技巧
- php 中序列化和json使用介紹
- php采集文章中的圖片獲取替換到本地
PHP教程Rss訂閱編程教程搜索
PHP教程推薦
- PHP中使用XML-RPC構(gòu)造Web Service簡(jiǎn)單入門
- 淺析關(guān)于cookie和session
- 關(guān)于php 接口問(wèn)題(php接口主要也就是運(yùn)用curl,curl函數(shù))
- 怎樣把握技巧開發(fā)PHP網(wǎng)站
- 用PHP5的SimpleXML解析XML文檔
- 解析PHP安裝十大問(wèn)題
- 在PHP中使用MVC模式進(jìn)行開發(fā)
- 淺談PHP開發(fā)團(tuán)隊(duì)的管理之道
- Apache實(shí)現(xiàn)Web Server負(fù)載均衡詳解(不考慮Session版)
- PHP技術(shù)進(jìn)階 用PHP處理多個(gè)同名復(fù)選框
- 相關(guān)鏈接:
- 教程說(shuō)明:
PHP教程-《PHP設(shè)計(jì)模式介紹》第十二章 裝飾器模式(3)
。