一個框架中如果包含的類比較多,或者功能比較復雜的情況下,可以通過一個較輔助類為一些常用的情況提供簡單的接口。這樣客戶在使用這個框架的時候既可以比較簡單的應付常見的場景,又可以使用框架中的內實現符合自己要求的功能。這就好比買電腦的時候,即可以買品牌機,又可以自己買配件組裝。下面就拿買電腦來舉例。
假設電腦由顯示器、主板、CPU、內存和顯卡組成。當然,實際遠遠不止這些。每個設備都有許多的參數需要選擇,我們給它們分別定義一個類去完成選擇的工作。
<code class="language-objc"><span class="c1">//選擇顯示器</span> <span class="k">@interface</span> <span class="nc">Display</span> : <span class="bp">NSObject</span> <span class="p">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="nf">chooseDisplay:</span><span class="p">(</span><span class="bp">NSString</span> <span class="o">*</span><span class="p">)</span><span class="nv">type</span><span class="p">;</span> <span class="k">@end</span> <span class="k">@implementation</span> <span class="nc">Display</span> <span class="p">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="nf">chooseDisplay:</span><span class="p">(</span><span class="bp">NSString</span> <span class="o">*</span><span class="p">)</span><span class="nv">type</span> <span class="p">{</span> <span class="n">NSLog</span><span class="p">(</span><span class="s">@"選擇顯示器:%@"</span><span class="p">,</span> <span class="n">type</span><span class="p">);</span> <span class="p">}</span> <span class="k">@end</span> <span class="c1">//選擇主板</span> <span class="k">@interface</span> <span class="nc">MainBoard</span> : <span class="bp">NSObject</span> <span class="p">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="nf">chooseMainBoard:</span><span class="p">(</span><span class="bp">NSString</span> <span class="o">*</span><span class="p">)</span><span class="nv">type</span><span class="p">;</span> <span class="k">@end</span> <span class="k">@implementation</span> <span class="nc">MainBoard</span> <span class="p">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="nf">chooseMainBoard:</span><span class="p">(</span><span class="bp">NSString</span> <span class="o">*</span><span class="p">)</span><span class="nv">type</span> <span class="p">{</span> <span class="n">NSLog</span><span class="p">(</span><span class="s">@"選擇主板:%@"</span><span class="p">,</span> <span class="n">type</span><span class="p">);</span> <span class="p">}</span> <span class="k">@end</span> <span class="c1">//選擇CPU</span> <span class="k">@interface</span> <span class="nc">CPU</span> : <span class="bp">NSObject</span> <span class="p">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="nf">chooseCPU:</span><span class="p">(</span><span class="bp">NSString</span> <span class="o">*</span><span class="p">)</span><span class="nv">type</span><span class="p">;</span> <span class="k">@end</span> <span class="k">@implementation</span> <span class="nc">CPU</span> <span class="p">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="nf">chooseCPU:</span><span class="p">(</span><span class="bp">NSString</span> <span class="o">*</span><span class="p">)</span><span class="nv">type</span> <span class="p">{</span> <span class="n">NSLog</span><span class="p">(</span><span class="s">@"選擇CPU:%@"</span><span class="p">,</span> <span class="n">type</span><span class="p">);</span> <span class="p">}</span> <span class="k">@end</span> <span class="c1">//客戶代碼</span> <span class="k">@interface</span> <span class="nc">Client</span> : <span class="bp">NSObject</span> <span class="p">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="nf">assebleComputer</span><span class="p">;</span> <span class="k">@end</span> <span class="k">@implementation</span> <span class="nc">Client</span> <span class="p">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="nf">assebleComputer</span> <span class="p">{</span> <span class="n">Display</span> <span class="o">*</span><span class="n">display</span> <span class="o">=</span> <span class="p">[</span><span class="n">Display</span> <span class="n">new</span><span class="p">];</span> <span class="p">[</span><span class="n">display</span> <span class="nl">chooseDisplay</span><span class="p">:</span> <span class="s">@"AOC"</span><span class="p">];</span> <span class="n">MainBoard</span> <span class="o">*</span><span class="n">mainBoard</span> <span class="o">=</span> <span class="p">[</span><span class="n">MainBoard</span> <span class="n">new</span><span class="p">];</span> <span class="p">[</span><span class="n">mainBoard</span> <span class="nl">chooseMainBoard</span><span class="p">:</span> <span class="s">@"華碩"</span><span class="p">];</span> <span class="n">CPU</span> <span class="o">*</span><span class="n">cpu</span> <span class="o">=</span> <span class="p">[</span><span class="n">CPU</span> <span class="n">new</span><span class="p">];</span> <span class="p">[</span><span class="n">cpu</span> <span class="nl">chooseCPU</span><span class="p">:</span> <span class="s">@"i7處理器"</span><span class="p">]</span> <span class="p">}</span> <span class="k">@end</span> </code>
這時的輸出結果應該是:
<code class="language-bash">選擇顯示器:AOC 選擇主板:華碩 選擇CPU:i7處理器 </code>
類圖:
我們可以利用這幾個選取的類去組裝任意配置的電腦。但是如果遇到小白用戶,他們可能從來沒有聽說過什麼是處理器,什麼是主板。這時我們可能希望有一個簡單一點地方式去做這件事。
<code class="language-objc"><span class="k">@interface</span> <span class="nc">MacBookPro</span> : <span class="bp">NSObject</span> <span class="p">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="nf">chooseDevice</span><span class="p">;</span> <span class="k">@end</span> <span class="k">@implementation</span> <span class="nc">MacBookPro</span> <span class="p">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="nf">chooseDevice</span> <span class="p">{</span> <span class="n">Display</span> <span class="o">*</span><span class="n">display</span> <span class="o">=</span> <span class="p">[</span><span class="n">Display</span> <span class="n">new</span><span class="p">];</span> <span class="p">[</span><span class="n">display</span> <span class="nl">chooseDisplay</span><span class="p">:</span> <span class="s">@"三星"</span><span class="p">];</span> <span class="n">MainBoard</span> <span class="o">*</span><span class="n">mainBoard</span> <span class="o">=</span> <span class="p">[</span><span class="n">MainBoard</span> <span class="n">new</span><span class="p">];</span> <span class="p">[</span><span class="n">mainBoard</span> <span class="nl">chooseMainBoard</span><span class="p">:</span> <span class="s">@"華碩"</span><span class="p">];</span> <span class="n">CPU</span> <span class="o">*</span><span class="n">cpu</span> <span class="o">=</span> <span class="p">[</span><span class="n">CPU</span> <span class="n">new</span><span class="p">];</span> <span class="p">[</span><span class="n">cpu</span> <span class="nl">chooseCPU</span><span class="p">:</span> <span class="s">@"i5處理器"</span><span class="p">]</span> <span class="p">}</span> <span class="k">@end</span> </code>
這時客戶的代碼為:
<code class="language-objc"><span class="k">@interface</span> <span class="nc">Client</span> : <span class="bp">NSObject</span> <span class="p">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="nf">assebleComputer</span><span class="p">;</span> <span class="k">@end</span> <span class="k">@implementation</span> <span class="nc">Client</span> <span class="p">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="nf">assembleComputer</span> <span class="p">{</span> <span class="n">MacBookPro</span> <span class="o">*</span><span class="n">computer</span> <span class="o">=</span> <span class="p">[</span><span class="n">MacBookPro</span> <span class="n">new</span><span class="p">];</span> <span class="p">[</span><span class="n">computer</span> <span class="n">chooseDevice</span><span class="p">];</span> <span class="p">}</span> <span class="k">@end</span> </code>
輸出結果為:
<code class="language-bash">選擇顯示器:三星 選擇主板:華碩 選擇CPU:i5處理器 </code>
使用MacBookPro
這個類的好處就是客戶可以不去了解電腦的組成的情況下,也可以得到一台完整的電腦。這樣就可以同時滿足小白用戶和專業用戶了。下面是它們之間的類圖。