首頁 > php教程 > php手册 > 主體

PHP设计模式之中介者模式

WBOY
發布: 2016-06-21 08:52:52
原創
1414 人瀏覽過

中介者: 中介者设计莫用于开发一个对象,这个对象能够在类似对象相互之间不直接交互的情况下传送或调节对这些对象的集合的修改 
处理具有类似属性并且属性需要保持同步的非耦合对象时,最佳的做法是使用基于中介者设计模式的对象。

<ol class="dp-c">
<li class="alt"><span><span><?php    </span></span></span></li>
<li><span>    <span class="comment">/** </span> </span></li>
<li class="alt"><span><span class="comment">     * 测试用例描述:示例代码不仅允许乐队进入和管理他们的音乐合集,而且还允许乐队更新他们的配置文件,修改乐队相关信息以及更新其CD信息 </span> </span></li>
<li><span><span class="comment">     *        现在,艺术家可上传MP3集合并从Web站点撤下CD。 因此, Web站点需要保持相对应的CD和MP3彼此同步。 </span> </span></li>
<li class="alt"><span><span class="comment">     */</span><span>   </span></span></li>
<li><span>    <span class="comment">//CD类  </span><span> </span></span></li>
<li class="alt"><span>    <span class="keyword">class</span><span> CD {   </span></span></li>
<li><span>           </span></li>
<li class="alt"><span>        <span class="keyword">public</span><span> </span><span class="vars">$band</span><span>  = </span><span class="string">''</span><span>;   </span></span></li>
<li><span>        <span class="keyword">public</span><span> </span><span class="vars">$title</span><span> = </span><span class="string">''</span><span>;   </span></span></li>
<li class="alt"><span>        <span class="keyword">protected</span><span> </span><span class="vars">$_mediator</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> __construct(MusicContainerMediator </span><span class="vars">$mediator</span><span> = NULL) {   </span></span></li>
<li><span>            <span class="vars">$this</span><span>->_mediator = </span><span class="vars">$mediator</span><span>;   </span></span></li>
<li class="alt"><span>        }   </span></li>
<li><span>           </span></li>
<li class="alt"><span>        <span class="keyword">public</span><span> </span><span class="keyword">function</span><span> save() {   </span></span></li>
<li><span>            <span class="comment">//具体实现待定  </span><span> </span></span></li>
<li class="alt"><span>            var_dump(<span class="vars">$this</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> changeBandName(</span><span class="vars">$bandname</span><span>) {   </span></span></li>
<li class="alt"><span>            <span class="keyword">if</span><span> ( ! </span><span class="func">is_null</span><span>(</span><span class="vars">$this</span><span>->_mediator)) {   </span></span></li>
<li><span>                <span class="vars">$this</span><span>->_mediator->change(</span><span class="vars">$this</span><span>, </span><span class="keyword">array</span><span>(</span><span class="string">"band"</span><span> => </span><span class="vars">$bandname</span><span>));   </span></span></li>
<li class="alt"><span>            }   </span></li>
<li><span>            <span class="vars">$this</span><span>->band = </span><span class="vars">$bandname</span><span>;   </span></span></li>
<li class="alt"><span>            <span class="vars">$this</span><span>->save();   </span></span></li>
<li><span>        }   </span></li>
<li class="alt"><span>    }   </span></li>
<li><span>       </span></li>
<li class="alt"><span>    <span class="comment">//MP3Archive类  </span><span> </span></span></li>
<li><span>    <span class="keyword">class</span><span> MP3Archive {   </span></span></li>
<li class="alt"><span>           </span></li>
<li><span>        <span class="keyword">protected</span><span> </span><span class="vars">$_mediator</span><span>;   </span></span></li>
<li class="alt"><span>           </span></li>
<li><span>        <span class="keyword">public</span><span> </span><span class="keyword">function</span><span> __construct(MusicContainerMediator </span><span class="vars">$mediator</span><span> = NULL) {   </span></span></li>
<li class="alt"><span>            <span class="vars">$this</span><span>->_mediator = </span><span class="vars">$mediator</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> save() {   </span></span></li>
<li class="alt"><span>            <span class="comment">//具体实现待定  </span><span> </span></span></li>
<li><span>            var_dump(<span class="vars">$this</span><span>);   </span></span></li>
<li class="alt"><span>        }   </span></li>
<li><span>           </span></li>
<li class="alt"><span>        <span class="keyword">public</span><span> </span><span class="keyword">function</span><span> changeBandName(</span><span class="vars">$bandname</span><span>) {   </span></span></li>
<li><span>            <span class="keyword">if</span><span> ( ! </span><span class="func">is_null</span><span>(</span><span class="vars">$this</span><span>->_mediator)) {   </span></span></li>
<li class="alt"><span>                <span class="vars">$this</span><span>->_mediator->change(</span><span class="vars">$this</span><span>, </span><span class="keyword">array</span><span>(</span><span class="string">"band"</span><span> => </span><span class="vars">$bandname</span><span>));   </span></span></li>
<li><span>            }   </span></li>
<li class="alt"><span>            <span class="vars">$this</span><span>->band = </span><span class="vars">$bandname</span><span>;   </span></span></li>
<li><span>            <span class="vars">$this</span><span>->save();   </span></span></li>
<li class="alt"><span>        }   </span></li>
<li><span>    }        </span></li>
<li class="alt"><span>    <span class="comment">//中介者类  </span><span> </span></span></li>
<li><span>    <span class="keyword">class</span><span> MusicContainerMediator {   </span></span></li>
<li class="alt"><span>           </span></li>
<li><span>        <span class="keyword">protected</span><span> </span><span class="vars">$_containers</span><span> = </span><span class="keyword">array</span><span>();   </span></span></li>
<li class="alt"><span>           </span></li>
<li><span>        <span class="keyword">public</span><span> </span><span class="keyword">function</span><span> __construct() {   </span></span></li>
<li class="alt"><span>            <span class="vars">$this</span><span>->_containers[] = </span><span class="string">"CD"</span><span>;   </span></span></li>
<li><span>            <span class="vars">$this</span><span>->_containers[] = </span><span class="string">"MP3Archive"</span><span>;   </span></span></li>
<li class="alt"><span>        }   </span></li>
<li><span>           </span></li>
<li class="alt"><span>        <span class="keyword">public</span><span> </span><span class="keyword">function</span><span> change(</span><span class="vars">$originalObject</span><span>, </span><span class="vars">$newValue</span><span>) {   </span></span></li>
<li><span>            <span class="vars">$title</span><span> = </span><span class="vars">$originalObject</span><span>->title;   </span></span></li>
<li class="alt"><span>            <span class="vars">$band</span><span>  = </span><span class="vars">$originalObject</span><span>->band;   </span></span></li>
<li><span>               </span></li>
<li class="alt"><span>            <span class="keyword">foreach</span><span> (</span><span class="vars">$this</span><span>->_containers </span><span class="keyword">as</span><span> </span><span class="vars">$container</span><span>) {   </span></span></li>
<li><span>                <span class="keyword">if</span><span> ( ! (</span><span class="vars">$originalObject</span><span> instanceof </span><span class="vars">$container</span><span>)) {   </span></span></li>
<li class="alt"><span>                    <span class="vars">$object</span><span> = </span><span class="keyword">new</span><span> </span><span class="vars">$container</span><span>;   </span></span></li>
<li><span>                    <span class="vars">$object</span><span>->title = </span><span class="vars">$title</span><span>;   </span></span></li>
<li class="alt"><span>                    <span class="vars">$object</span><span>->band  = </span><span class="vars">$band</span><span>;   </span></span></li>
<li><span>                       </span></li>
<li class="alt"><span>                    <span class="keyword">foreach</span><span> (</span><span class="vars">$newValue</span><span> </span><span class="keyword">as</span><span> </span><span class="vars">$key</span><span> => </span><span class="vars">$val</span><span>) {   </span></span></li>
<li><span>                        <span class="vars">$object</span><span>-></span><span class="vars">$key</span><span> = </span><span class="vars">$val</span><span>;   </span></span></li>
<li class="alt"><span>                    }   </span></li>
<li><span>                       </span></li>
<li class="alt"><span>                    <span class="vars">$object</span><span>->save();   </span></span></li>
<li><span>                }   </span></li>
<li class="alt"><span>            }   </span></li>
<li><span>        }   </span></li>
<li class="alt"><span>    }   </span></li>
<li><span>       </span></li>
<li class="alt"><span>    <span class="comment">//测试实例  </span><span> </span></span></li>
<li><span>    <span class="vars">$titleFromDB</span><span> = </span><span class="string">"Waste of a Rib"</span><span>;   </span></span></li>
<li class="alt"><span>    <span class="vars">$bandFromDB</span><span>  = </span><span class="string">"Never Again"</span><span>;   </span></span></li>
<li><span>    <span class="vars">$mediator</span><span> = </span><span class="keyword">new</span><span> MusicContainerMediator();   </span></span></li>
<li class="alt"><span>    <span class="vars">$cd</span><span> = </span><span class="keyword">new</span><span> CD(</span><span class="vars">$mediator</span><span>);   </span></span></li>
<li><span>    <span class="vars">$cd</span><span>->title = </span><span class="vars">$titleFromDB</span><span>;   </span></span></li>
<li class="alt"><span>    <span class="vars">$cd</span><span>->band  = </span><span class="vars">$bandFromDB</span><span>;   </span></span></li>
<li><span>    <span class="vars">$cd</span><span>->changeBandName(</span><span class="string">"Maybe Once More"</span><span>);   </span></span></li>
<li class="alt"><span>?> </span></li>
</ol>
登入後複製

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



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