WEB开发笔记 www.chhua.com 每日练习 PHP设计模式——适配器模式

适配器模式一般用来更改后的API改进类,要让它与其他的现有类相兼容。

举例:
如果有两个模板引擎,一个是smarty 一个是 phptal,现在的程序是用的smarty模板而不是phptal,
如果想把smarty模板引擎改为phptal,又不想更改使用改模板的程序代码,怎么办?这时,就用到了PHP设计模式中的“适配器模式”。

看演示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<span style="color: #000000;font-weight: bold"><?php</span>
<span style="color: #666666;font-style: italic">/*
 *
 * */</span>
<span style="color: #666666;font-style: italic">//模仿一个Smarty类</span>
<span style="color: #000000;font-weight: bold">class</span> smarty <span style="color: #009900">{</span>
	<span style="color: #000000;font-weight: bold">private</span> <span style="color: #000088">$vars</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/array" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">array</span></a><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
 
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> assgin<span style="color: #009900">(</span><span style="color: #000088">$var</span><span style="color: #339933">,</span><span style="color: #000088">$value</span><span style="color: #009900">)</span><span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//传值</span>
		<span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">vars</span><span style="color: #009900">[</span><span style="color: #000088">$var</span><span style="color: #009900">]</span><span style="color: #339933">=</span><span style="color: #000088">$value</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> display<span style="color: #009900">(</span><span style="color: #000088">$tpl</span><span style="color: #009900">)</span><span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//加载输出</span>
		<span style="color: #b1b100">echo</span> <span style="color: #000088">$tpl</span><span style="color: #339933">.</span><span style="color: #0000ff">" html arr:<pre>"</span><span style="color: #339933">;</span>
		<a rel="noopener noreferrer nofollow" href="http://www.php.net/print_r" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">print_r</span></a><span style="color: #009900">(</span><span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">vars</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
<span style="color: #009900">}</span>
 
<span style="color: #000088">$smarty</span><span style="color: #339933">=</span><span style="color: #000000;font-weight: bold">new</span> smarty<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #000088">$smarty</span><span style="color: #339933">-></span><span style="color: #004000">assgin</span><span style="color: #009900">(</span><span style="color: #0000ff">"Name"</span><span style="color: #339933">,</span><span style="color: #0000ff">"WEB开发笔记"</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #000088">$smarty</span><span style="color: #339933">-></span><span style="color: #004000">assgin</span><span style="color: #009900">(</span><span style="color: #0000ff">"URL"</span><span style="color: #339933">,</span><span style="color: #0000ff">"http://www.chhua.com"</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #000088">$smarty</span><span style="color: #339933">-></span><span style="color: #004000">display</span><span style="color: #009900">(</span><span style="color: #0000ff">"index.tpl"</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #666666;font-style: italic">//调用正常。</span>
<span style="color: #000000;font-weight: bold">?></span>

假设现在出现了一个问题, smarty类不稳定,我们需要换成phptal,又不想去更改程序源码,怎么办?
以下,我们就写一个适配器.

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<span style="color: #000000;font-weight: bold"><?php</span>
 
<span style="color: #666666;font-style: italic">//模仿一个phptal类</span>
<span style="color: #000000;font-weight: bold">class</span> phptal<span style="color: #009900">{</span>
	<span style="color: #000000;font-weight: bold">private</span> <span style="color: #000088">$vars</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/array" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">array</span></a><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
 
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> set<span style="color: #009900">(</span><span style="color: #000088">$var</span><span style="color: #339933">,</span><span style="color: #000088">$value</span><span style="color: #009900">)</span><span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//传值</span>
		<span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">vars</span><span style="color: #009900">[</span><span style="color: #000088">$var</span><span style="color: #009900">]</span><span style="color: #339933">=</span><span style="color: #000088">$value</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> fetch<span style="color: #009900">(</span><span style="color: #000088">$tpl</span><span style="color: #009900">)</span><span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//加载输出</span>
		<span style="color: #b1b100">echo</span> <span style="color: #000088">$tpl</span><span style="color: #339933">.</span><span style="color: #0000ff">" html arr:<pre>"</span><span style="color: #339933">;</span>
		<a rel="noopener noreferrer nofollow" href="http://www.php.net/print_r" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">print_r</span></a><span style="color: #009900">(</span><span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">vars</span><span style="color: #009900">)</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> setAll<span style="color: #009900">(</span><span style="color: #000088">$vars</span><span style="color: #009900">)</span><span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//传入数组的时候</span>
		<span style="color: #b1b100">foreach</span> <span style="color: #009900">(</span><span style="color: #000088">$vars</span> <span style="color: #b1b100">as</span> <span style="color: #000088">$key</span><span style="color: #339933">=></span><span style="color: #000088">$value</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">vars</span><span style="color: #009900">[</span><span style="color: #000088">$key</span><span style="color: #009900">]</span><span style="color: #339933">=</span><span style="color: #000088">$value</span><span style="color: #339933">;</span>
		<span style="color: #009900">}</span>
	<span style="color: #009900">}</span>
<span style="color: #009900">}</span>
 
<span style="color: #666666;font-style: italic">//写一个接口</span>
<span style="color: #000000;font-weight: bold">interface</span> smartyInterface <span style="color: #009900">{</span>
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> assgin<span style="color: #009900">(</span><span style="color: #000088">$var</span><span style="color: #339933">,</span><span style="color: #000088">$value</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> display<span style="color: #009900">(</span><span style="color: #000088">$tpl</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #009900">}</span>
 
<span style="color: #666666;font-style: italic">//smarty 适配器 API </span>
<span style="color: #000000;font-weight: bold">class</span> smarty implements smartyInterface <span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//smarty 适配器 API </span>
	<span style="color: #000000;font-weight: bold">private</span> <span style="color: #000088">$vars</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/array" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">array</span></a><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
 
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> assgin<span style="color: #009900">(</span><span style="color: #000088">$var</span><span style="color: #339933">,</span><span style="color: #000088">$value</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">vars</span><span style="color: #009900">[</span><span style="color: #000088">$var</span><span style="color: #009900">]</span><span style="color: #339933">=</span><span style="color: #000088">$value</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//在PHPTAL对象存在前存值</span>
	<span style="color: #009900">}</span>
 
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> display<span style="color: #009900">(</span><span style="color: #000088">$tpl</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
		<span style="color: #000088">$phptal</span><span style="color: #339933">=</span><span style="color: #000000;font-weight: bold">new</span> phptal<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
		<span style="color: #000088">$phptal</span><span style="color: #339933">-></span><span style="color: #004000">setAll</span><span style="color: #009900">(</span><span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">vars</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
		<span style="color: #000088">$phptal</span><span style="color: #339933">-></span><span style="color: #004000">fetch</span><span style="color: #009900">(</span><span style="color: #000088">$tpl</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
<span style="color: #009900">}</span>
 
<span style="color: #000088">$smarty</span><span style="color: #339933">=</span><span style="color: #000000;font-weight: bold">new</span> smarty<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #000088">$smarty</span><span style="color: #339933">-></span><span style="color: #004000">assgin</span><span style="color: #009900">(</span><span style="color: #0000ff">"Name"</span><span style="color: #339933">,</span><span style="color: #0000ff">"WEB开发笔记"</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #000088">$smarty</span><span style="color: #339933">-></span><span style="color: #004000">assgin</span><span style="color: #009900">(</span><span style="color: #0000ff">"URL"</span><span style="color: #339933">,</span><span style="color: #0000ff">"http://www.chhua.com"</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #000088">$smarty</span><span style="color: #339933">-></span><span style="color: #004000">display</span><span style="color: #009900">(</span><span style="color: #0000ff">"index.tpl"</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #666666;font-style: italic">/*通过智适配器运行正常*/</span>
<span style="color: #000000;font-weight: bold">?></span>