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