命令模式:
第一次接触到这个命令模式的时候,感觉它很像一个简化的mvc框架。从不同的路径访问,再由控制器来判断所要调用的具体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教程有兴趣的朋友有所帮助。