WEB开发笔记 www.chhua.com 每日练习 PHP设计模式——无用的空对象模式

哎哟,今天发现一个PHP中的设计模式:空对象模式,下面说说我的想法。
真的好鸡肋,在百度和GG 上搜了很久空对象模式,均没有找到好的回答,我这种不弄个明白不罢休的破脾气,让我纠结了好久。最终发现了,主要是节省部分IF判断的语句。这种方法称之为“模式”真的有点小题大作。

下面用代码来演示一下,在编程中的应用。

1
2
3
4
5
6
7
8
9
10
11
<span style="color: #666666;font-style: italic">//www.chhua.com 建立一个类</span>
<span style="color: #000000;font-weight: bold">class</span> user <span style="color: #009900">{</span>
	static <span style="color: #000000;font-weight: bold">function</span> create<span style="color: #009900">(</span><span style="color: #009900">)</span> <span style="color: #009900">{</span>
		<span style="color: #b1b100">return</span> <span style="color: #009900;font-weight: bold">null</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
<span style="color: #009900">}</span>
<span style="color: #000088">$user</span><span style="color: #339933">=</span>user<span style="color: #339933">::</span><span style="color: #004000">create</span><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #000088">$user</span><span style="color: #339933">-></span><span style="color: #004000">login</span><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #666666;font-style: italic">/*以上方法在运行的时候肯定就会报错,
 * Fatal error: Call to a member function login() on a non-object in http://www.chhua.com/test.php on line 18
 */</span>

为了避免报错,我就就需要做以下处理

1
2
3
<span style="color: #b1b100">if</span> <span style="color: #009900">(</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/is_object" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">is_object</span></a><span style="color: #009900">(</span><span style="color: #000088">$user</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
	<span style="color: #000088">$user</span><span style="color: #339933">-></span><span style="color: #004000">login</span><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #009900">}</span>

为了不使用IF条件的判断,我们就用以下的方法,也就是传说中的空对象模式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<span style="color: #000000;font-weight: bold">interface</span> nullinterface <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> login<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> nullUser implements nullinterface <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> login<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #009900">{</span><span style="color: #009900">}</span>
<span style="color: #009900">}</span>
 
<span style="color: #000000;font-weight: bold">class</span> user <span style="color: #009900">{</span>
	static <span style="color: #000000;font-weight: bold">function</span> create<span style="color: #009900">(</span><span style="color: #009900">)</span> <span style="color: #009900">{</span>
		<span style="color: #b1b100">return</span> <span style="color: #000000;font-weight: bold">new</span> nullUser<span style="color: #009900">(</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: #000088">$user</span><span style="color: #339933">=</span>user<span style="color: #339933">::</span><span style="color: #004000">create</span><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #000088">$user</span><span style="color: #339933">-></span><span style="color: #004000">login</span><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//不再报错</span>

以上一个简单的实例说明了空对象模式的说明实例,大家可以自己扩展,自我感觉除了增强代码的健壮性之外没什么大的用处,希望不太明白的人也不要在这上面纠结了,没有意义。