WEB开发笔记 www.chhua.com 每日练习 PHP设计模式——策略模式

策略模式在设计模式中非常重要,可能是现代面向对象设计中最重要的模式。
它用来创建可插入,可替换,可重用的组件。
为了完整简单的说明策略模式,下面的代码演示一个非常简单的例子。

代码演示:例子的实现是实现一个获取HTML代码的类,
类的结构,分为二部分,一部分是获取HTML,一部分是获取相关内容,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<span style="color: #000000;font-weight: bold"><?php</span>
<span style="color: #000000;font-weight: bold">class</span> HtmlDom<span style="color: #009900">{</span>
	<span style="color: #000000;font-weight: bold">private</span> <span style="color: #000088">$strObj</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//内容对象</span>
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> __construct<span style="color: #009900">(</span><span style="color: #000088">$strObj</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
		<span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">strObj</span><span style="color: #339933">=</span><span style="color: #000088">$strObj</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> getHtml<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
		<span style="color: #b1b100">return</span> <span style="color: #0000ff">"<html><body>"</span><span style="color: #339933">.</span><span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">strObj</span><span style="color: #339933">-></span><span style="color: #004000">getContent</span><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">.</span><span style="color: #0000ff">"</body></html>"</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
<span style="color: #009900">}</span>
 
<span style="color: #000000;font-weight: bold">interface</span> HtmlContent <span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//内容对象接口</span>
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> getContent<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #009900">}</span>
 
<span style="color: #000000;font-weight: bold">class</span> Hellworld implements HtmlContent<span style="color: #009900">{</span>
	<span style="color: #000000;font-weight: bold">private</span> <span style="color: #000088">$world</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//内容</span>
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> __construct<span style="color: #009900">(</span><span style="color: #000088">$world</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
		<span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">world</span><span style="color: #339933">=</span><span style="color: #000088">$world</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> getContent<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
		<span style="color: #b1b100">return</span> <span style="color: #0000ff">"Hello "</span><span style="color: #339933">.</span><span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">world</span><span style="color: #339933">.</span><span style="color: #0000ff">"!"</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
<span style="color: #009900">}</span>
 
<span style="color: #000088">$html</span><span style="color: #339933">=</span><span style="color: #000000;font-weight: bold">new</span> HtmlDom<span style="color: #009900">(</span><span style="color: #000000;font-weight: bold">new</span> Hellworld<span style="color: #009900">(</span><span style="color: #0000ff">"www.chhua.com"</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #b1b100">echo</span> <span style="color: #000088">$html</span><span style="color: #339933">-></span><span style="color: #004000">getHtml</span><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//输出 www.chhua.com</span>
 
<span style="color: #000000;font-weight: bold">?></span>

以上是策略模式的一个小小的实例,虽然这个例子微不足道,但这种设计模式在更为复杂的情况下支非常有用