WEB开发笔记 www.chhua.com 每日练习 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
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
50
51
52
53
54
55
56
57
58
59
60
61
62
<span style="color: #000000;font-weight: bold"><?php</span>
<span style="color: #666666;font-style: italic">/*
 * 
 * */</span>
 
<span style="color: #000000;font-weight: bold">class</span> printStr<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: #000088">$msg</span><span style="color: #339933">;</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">$msg</span><span style="color: #339933">=</span><span style="color: #0000ff">""</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">msg</span><span style="color: #339933">=</span><span style="color: #000088">$msg</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> getMessage<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">"This getMessage Str Function"</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> addNum<span style="color: #009900">(</span><span style="color: #000088">$num1</span><span style="color: #339933">,</span><span style="color: #000088">$num2</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
		<span style="color: #b1b100">return</span> <span style="color: #000088">$num1</span><span style="color: #339933">+</span><span style="color: #000088">$num2</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
<span style="color: #009900">}</span> 
 
<span style="color: #000000;font-weight: bold">class</span> partentZhuanshi <span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//父类</span>
	<span style="color: #000000;font-weight: bold">private</span> <span style="color: #000088">$printClass</span><span style="color: #339933">;</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">$printClass</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">printClass</span><span style="color: #339933">=</span><span style="color: #000088">$printClass</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> addNum<span style="color: #009900">(</span><span style="color: #000088">$num1</span><span style="color: #339933">,</span><span style="color: #000088">$num2</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
		<span style="color: #b1b100">return</span> <span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">printClass</span><span style="color: #339933">-></span><span style="color: #004000">addNum</span><span style="color: #009900">(</span><span style="color: #000088">$num1</span><span style="color: #339933">,</span><span style="color: #000088">$num2</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//简单的一个委托</span>
	<span style="color: #009900">}</span>
<span style="color: #009900">}</span>
 
<span style="color: #000000;font-weight: bold">class</span> zhuangshi <span style="color: #000000;font-weight: bold">extends</span> partentZhuanshi<span style="color: #009900">{</span>
	<span style="color: #000000;font-weight: bold">private</span> <span style="color: #000088">$printClass</span><span style="color: #339933">;</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">$printClass</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">printClass</span><span style="color: #339933">=</span><span style="color: #000088">$printClass</span><span style="color: #339933">;</span>
		<span style="color: #b1b100">if</span> <span style="color: #009900">(</span><span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">printClass</span><span style="color: #339933">-></span><span style="color: #004000">msg</span><span style="color: #339933">==</span><span style="color: #0000ff">""</span><span style="color: #009900">)</span><span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//装饰添加一个异常处理</span>
			<span style="color: #b1b100">throw</span> <span style="color: #000000;font-weight: bold">new</span> Exception<span style="color: #009900">(</span><span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">printClass</span><span style="color: #339933">-></span><span style="color: #004000">getMessage</span><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
		<span style="color: #009900">}</span>
	<span style="color: #009900">}</span>
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> addNum<span style="color: #009900">(</span><span style="color: #000088">$num1</span><span style="color: #339933">,</span><span style="color: #000088">$num2</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
		<span style="color: #000088">$total</span><span style="color: #339933">=</span><span style="color: #000088">$num1</span><span style="color: #339933">+</span><span style="color: #000088">$num2</span><span style="color: #339933">;</span>
		<span style="color: #b1b100">if</span> <span style="color: #009900">(</span><span style="color: #000088">$total</span><span style="color: #339933"><</span><span style="color: #cc66cc">10</span><span style="color: #009900">)</span><span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//装饰添加一个异常处理</span>
			<span style="color: #b1b100">throw</span> <span style="color: #000000;font-weight: bold">new</span> Exception<span style="color: #009900">(</span><span style="color: #000088">$num1</span><span style="color: #339933">.</span><span style="color: #0000ff">" add "</span><span style="color: #339933">.</span><span style="color: #000088">$num2</span><span style="color: #339933">.</span><span style="color: #0000ff">" < 10"</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
		<span style="color: #009900">}</span>
		<span style="color: #b1b100">return</span> <span style="color: #000088">$total</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
<span style="color: #009900">}</span>
 
try <span style="color: #009900">{</span>
	<span style="color: #000088">$zhuanshi</span><span style="color: #339933">=</span><span style="color: #000000;font-weight: bold">new</span> zhuangshi<span style="color: #009900">(</span><span style="color: #000000;font-weight: bold">new</span> printStr<span style="color: #009900">(</span><span style="color: #0000ff">"此处如果为空,则抛出异常"</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #009900">}</span>
catch <span style="color: #009900">(</span>Exception <span style="color: #000088">$e</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
	<span style="color: #b1b100">echo</span> <span style="color: #0000ff">"异常一:"</span><span style="color: #339933">.</span><span style="color: #000088">$e</span><span style="color: #339933">-></span><span style="color: #004000">getMessage</span><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #009900">}</span>
 
try <span style="color: #009900">{</span>
	<span style="color: #b1b100">echo</span> <span style="color: #000088">$zhuanshi</span><span style="color: #339933">-></span><span style="color: #004000">addNum</span><span style="color: #009900">(</span><span style="color: #cc66cc">10</span><span style="color: #339933">,</span><span style="color: #cc66cc">2</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//两个数据的和小于10抛出异常</span>
<span style="color: #009900">}</span>
catch <span style="color: #009900">(</span>Exception <span style="color: #000088">$e</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
	<span style="color: #b1b100">echo</span> <span style="color: #0000ff">"异常二:"</span><span style="color: #339933">.</span><span style="color: #000088">$e</span><span style="color: #339933">-></span><span style="color: #004000">getMessage</span><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">?></span>

以上一个简单的实例说明了,策略模式用于更改和替换行为,装饰模式则是添加和装饰行为.