ホームページ  >  記事  >  php教程  >  PHP デザイン パターン メディエーター パターン

PHP デザイン パターン メディエーター パターン

WBOY
WBOYオリジナル
2016-06-21 08:52:521416ブラウズ

メディエーター: メディエーターは、相互に直接対話することなく、同様のオブジェクトのコレクションへの変更を通信または仲介できるオブジェクトの開発に使用するように設計されていません
同様のプロパティを持ち、プロパティの同期を保つ必要がある、結合されていないオブジェクトを扱う場合、ベスト プラクティスは、メディエーター デザイン パターンに基づいたオブジェクトを使用することです。

<ol class="dp-c">
<li class="alt"><span><span><?php   </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



声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。