首页  >  文章  >  php教程  >  PHP设计模式之解释器模式

PHP设计模式之解释器模式

WBOY
WBOY原创
2016-06-21 08:52:561107浏览

解释器: 解释器设计模式用于分析一个实体的关键元素,并且针对每个元素都提供自己的解释或相应的动作。
解释器设计模式最常用于PHP/HTML 模板系统。

<ol class="dp-c">
<li class="alt"><span><span><?php    </span></span></span></li>
<li><span>    <span class="keyword">class</span><span> User {   </span></span></li>
<li class="alt"><span>        <span class="keyword">protected</span><span> </span><span class="vars">$_username</span><span> = </span><span class="string">""</span><span>;   </span></span></li>
<li><span>        <span class="keyword">public</span><span> </span><span class="keyword">function</span><span> __construct(</span><span class="vars">$username</span><span>) {   </span></span></li>
<li class="alt"><span>            <span class="vars">$this</span><span>->_username = </span><span class="vars">$username</span><span>;   </span></span></li>
<li><span>        }  </span></li>
<li class="alt"><span>        <span class="keyword">public</span><span> </span><span class="keyword">function</span><span> getProfilePage() {   </span></span></li>
<li><span>            <span class="vars">$profile</span><span>  = </span><span class="string">"<h2>I like Never Again ! </h2>"</span><span>;   </span></span></li>
<li class="alt"><span>            <span class="vars">$profile</span><span> .= </span><span class="string">"I love all of their songs. My favorite CD: <br>"</span><span>;   </span></span></li>
<li><span>            <span class="vars">$profile</span><span> .= </span><span class="string">"{{myCD.getTitle}}!!"</span><span>;   </span></span></li>
<li class="alt"><span>               </span></li>
<li><span>            <span class="keyword">return</span><span> </span><span class="vars">$profile</span><span>;   </span></span></li>
<li class="alt"><span>        }   </span></li>
<li><span>    }   </span></li>
<li class="alt"><span>    <span class="keyword">class</span><span> userCD {   </span></span></li>
<li><span>        <span class="keyword">public</span><span> </span><span class="keyword">function</span><span> setUser(User </span><span class="vars">$user</span><span>) {   </span></span></li>
<li class="alt"><span>            <span class="vars">$this</span><span>->_user = </span><span class="vars">$user</span><span>;   </span></span></li>
<li><span>        }   </span></li>
<li class="alt"><span>        <span class="keyword">public</span><span> </span><span class="keyword">function</span><span> getTitle() {   </span></span></li>
<li><span>            <span class="vars">$title</span><span> = </span><span class="string">"Waste of a Rib"</span><span>;   </span></span></li>
<li class="alt"><span>            <span class="keyword">return</span><span> </span><span class="vars">$title</span><span>;   </span></span></li>
<li><span>        }   </span></li>
<li class="alt"><span>    }   </span></li>
<li><span>    <span class="keyword">class</span><span> userCDInterpreter {   </span></span></li>
<li class="alt"><span>           </span></li>
<li><span>        <span class="keyword">protected</span><span> </span><span class="vars">$_user</span><span> = NULL;   </span></span></li>
<li class="alt"><span>           </span></li>
<li><span>        <span class="keyword">public</span><span> </span><span class="keyword">function</span><span> setUser(User </span><span class="vars">$user</span><span>) {   </span></span></li>
<li class="alt"><span>            <span class="vars">$this</span><span>->_user = </span><span class="vars">$user</span><span>;   </span></span></li>
<li><span>        }   </span></li>
<li class="alt"><span>           </span></li>
<li><span>        <span class="keyword">public</span><span> </span><span class="keyword">function</span><span> getInterpreted() {   </span></span></li>
<li class="alt"><span>            <span class="vars">$profile</span><span> = </span><span class="vars">$this</span><span>->_user->getProfilePage();   </span></span></li>
<li><span>               </span></li>
<li class="alt"><span>            <span class="keyword">if</span><span> (preg_match_all(</span><span class="string">'/\{\{myCD\.(.*?)\}\}/'</span><span>, </span><span class="vars">$profile</span><span>, </span><span class="vars">$triggers</span><span>, PREG_SET_ORDER)) {   </span></span></li>
<li><span>                <span class="vars">$replacements</span><span> = </span><span class="keyword">array</span><span>();   </span></span></li>
<li class="alt"><span>                   </span></li>
<li><span>                <span class="keyword">foreach</span><span> (</span><span class="vars">$triggers</span><span> </span><span class="keyword">as</span><span> </span><span class="vars">$trigger</span><span>) {   </span></span></li>
<li class="alt"><span>                    <span class="vars">$replacements</span><span>[] = </span><span class="vars">$trigger</span><span>[1];   </span></span></li>
<li><span>                }   </span></li>
<li class="alt"><span>                   </span></li>
<li><span>                <span class="vars">$replacements</span><span> = </span><span class="func">array_unique</span><span>(</span><span class="vars">$replacements</span><span>);   </span></span></li>
<li class="alt"><span>                   </span></li>
<li><span>                <span class="vars">$myCD</span><span> = </span><span class="keyword">new</span><span> userCD();   </span></span></li>
<li class="alt"><span>                <span class="vars">$myCD</span><span>->setUser(</span><span class="vars">$this</span><span>->_user);   </span></span></li>
<li><span>                   </span></li>
<li class="alt"><span>                <span class="keyword">foreach</span><span> (</span><span class="vars">$replacements</span><span> </span><span class="keyword">as</span><span> </span><span class="vars">$replacement</span><span>) {   </span></span></li>
<li><span>                    <span class="vars">$profile</span><span> = </span><span class="func">str_replace</span><span>(</span><span class="string">"{{myCD.{$replacement}}}"</span><span>, call_user_func(</span><span class="keyword">array</span><span>(</span><span class="vars">$myCD</span><span>, </span><span class="vars">$replacement</span><span>)), </span><span class="vars">$profile</span><span>);   </span></span></li>
<li class="alt"><span>                }   </span></li>
<li><span>            }   </span></li>
<li class="alt"><span>               </span></li>
<li><span>            <span class="keyword">return</span><span> </span><span class="vars">$profile</span><span>;   </span></span></li>
<li class="alt"><span>        }   </span></li>
<li><span>           </span></li>
<li class="alt"><span>    }   </span></li>
<li><span>    <span class="vars">$username</span><span> = </span><span class="string">"aaron"</span><span>;   </span></span></li>
<li class="alt"><span>    <span class="vars">$user</span><span> = </span><span class="keyword">new</span><span> User(</span><span class="vars">$username</span><span>);   </span></span></li>
<li><span>    <span class="vars">$interpreter</span><span> = </span><span class="keyword">new</span><span> userCDInterpreter();   </span></span></li>
<li class="alt"><span>    <span class="vars">$interpreter</span><span>->setUser(</span><span class="vars">$user</span><span>);   </span></span></li>
<li><span>       </span></li>
<li class="alt"><span>    print <span class="string">"<h1>{$username}'s Profile</h1>"</span><span>;   </span></span></li>
<li><span>    print <span class="vars">$interpreter</span><span>->getInterpreted();   </span></span></li>
<li class="alt"><span>?> </span></li>
</ol>

 数据库脚本请参照:http://www.cxybl.com/html/wlbc/Php/2011_1126_9458.html



声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn