閱讀《深入php物件導向、模式與實踐》有感(三)

WBOY
發布: 2016-07-29 09:00:26
原創
1423 人瀏覽過

指令模式:

第一次接觸到這個指令模式的時候,感覺它很像一個簡化的mvc框架。從不同的路徑訪問,再由控制器來判斷要呼叫的特定php檔案。

閱讀《深入php物件導向、模式與實踐》有感(三)

<span><span><?php </span><span><em></em></span><span>class </span><span>CommandContext{   </span><span><em>//"</em></span><span><em>命令容器</em></span><span><em>"
</em></span><span><em></em></span><span>private </span><span>$params </span><span>= </span><span>array</span><span>();
</span><span>function </span><span>addParam(</span><span>$key</span><span>,</span><span>$val</span><span>){
</span><span>$this</span><span>-></span><span>params</span><span>[</span><span>$key</span><span>] = </span><span>$val</span><span>;
</span><span>    }
</span><span>function </span><span>getParam(</span><span>$key</span><span>){
</span><span>return </span><span>$this</span><span>-></span><span>params</span><span>[</span><span>$key</span><span>];
</span><span>    }
</span><span>}
</span><span>class </span><span>Controller{
</span><span>private </span><span>$cmdContext</span><span>;
</span><span>function </span><span>__construct(){
</span><span>$this</span><span>-></span><span>cmdContext </span><span>= </span><span>new </span><span>CommandContext();
</span><span>    }
</span><span><em>//
</em></span><span><em></em></span><span>function </span><span>getCmdContext(){
</span><span>return </span><span>$this</span><span>-></span><span>cmdContext</span><span>;
</span><span>    }
</span><span>function </span><span>process(){
</span><span>$action </span><span>= </span><span>$this</span><span>-></span><span>cmdContext</span><span>->getParam(</span><span>"action"</span><span>);   </span><span><em>//</em></span><span><em>通过</em></span><span><em>“</em></span><span><em>命令容器</em></span><span><em>”</em></span><span><em>获得命令
</em></span><span><em></em></span><span>$command </span><span>= CommandFactory::</span><span><em>getCommand</em></span><span>(</span><span>$action</span><span>);   </span><span><em>//</em></span><span><em>将</em></span><span><em>“</em></span><span><em>命令</em></span><span><em>”</em></span><span><em>传给</em></span><span><em>“</em></span><span><em>命令工厂</em></span><span><em>”</em></span><span><em>,得到</em></span><span><em>“</em></span><span><em>命令</em></span><span><em>”</em></span><span><em>所对应的子</em></span><span><em>command</em></span><span><em>类对象
</em></span><span><em></em></span><span>if</span><span>(</span><span>$command</span><span>->execute(</span><span>$this</span><span>-></span><span>cmdContext</span><span>)){</span><span><em>//</em></span><span><em>调用子类对象的</em></span><span><em>execute</em></span><span><em>方法并判断
</em></span><span><em></em></span><span><em>//</em></span><span><em>成功
</em></span><span><em></em></span><span><em>//</em></span><span><em>调用对应视图
</em></span><span><em></em></span><span>}</span><span>else</span><span>{
</span><span><em>//</em></span><span><em>失败
</em></span><span><em></em></span><span>}
</span><span>    }
</span><span>}
</span><span>class </span><span>CommandFactory{
</span><span>static function </span><span>getCommand(</span><span>$cmd</span><span>){
</span><span>$file </span><span>= </span><span>'commands/'</span><span>.</span><span>$cmd</span><span>.</span><span>'Command.php'</span><span>; </span><span><em>//</em></span><span><em>命令所对应的</em></span><span><em>php</em></span><span><em>文件路径
</em></span><span><em></em></span><span><em></em></span><span>if</span><span>(! </span><span><em>file_exists</em></span><span>(</span><span>$file</span><span>)){
</span><span>throw new </span><span>Exception(</span><span>"Could not find file </span><span>$file</span><span>"</span><span>);
</span><span>        }
</span><span>require_once</span><span>(</span><span>$file</span><span>);
</span><span>$class </span><span>= </span><span>$cmd</span><span>.</span><span>'Command'</span><span>;    </span><span><em>//</em></span><span><em>形成类名
</em></span><span><em></em></span><span>if</span><span>(! </span><span><em>class_exists</em></span><span>(</span><span>$class</span><span>)){
</span><span>throw new </span><span>Exception(</span><span>"Could not find class </span><span>$class</span><span>"</span><span>);
</span><span>        }
</span><span>$result </span><span>= </span><span>new </span><span>$class</span><span>();
</span><span>return </span><span>$result</span><span>;
</span><span>    }
</span><span>}
</span><span><em>//commands</em></span><span><em>文件夹内
</em></span><span>abstract class </span><span>Command{
</span><span>abstract function </span><span>execute(CommandContext </span><span>$commandContext</span><span>);
</span><span>}
</span><span>class </span><span>demoCommand </span><span>extends </span><span>Command{
</span><span>function </span><span>execute(CommandContext </span><span>$commandContext</span><span>){
</span><span>return </span><span>"ok"</span><span>;
</span><span>    }
</span><span>}
</span><span><em>//</em></span><span><em>使用代码
</em></span><span>$controller </span><span>= </span><span>new </span><span>Controller();
</span><span>$cmdContext </span><span>= </span><span>$controller</span><span>->getCmdContext();
</span><span>$cmdContext</span><span>->addParam(</span><span>"action"</span><span>,</span><span>"demo"</span><span>);
</span><span>$demo </span><span>= </span><span>$controller</span><span>->process();
</span><span>?></span></span></span>
登入後複製

以上就介紹了讀《深入php物件導向、模式與實作》有感(三),包括了方面的內容,希望對PHP教學有興趣的朋友有幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板