WEB开发笔记 www.chhua.com 每日练习 PHP面向对象编程——依赖倒置原则(DIP)

依赖倒置原则,听起来比较绕,下面通过一个简单的实例来说明一下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
63
64
<span style="color: #000000;font-weight: bold"><?php</span>
<span style="color: #666666;font-style: italic">/*
 * WEB开发笔记 www.chhua.com 每日练习 PHP面向对象编程——依赖倒置原则(DIP)
 * 
 * 依赖倒置原则,听起来比较绕,下面通过一个简单的实例来说明一下PHP面向对象编程的依赖倒置原则
 */</span>
 
<span style="color: #666666;font-style: italic">/*代码演示 ,例如,电脑的输入端口是键盘,现在就写一个这样的模仿类
 * */</span>
 
<span style="color: #000000;font-weight: bold">class</span> pc <span style="color: #009900">{</span>
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> In<span style="color: #009900">(</span><span style="color: #009900">)</span> <span style="color: #009900">{</span>
		<span style="color: #000088">$inputOb</span><span style="color: #339933">=</span><span style="color: #000000;font-weight: bold">new</span> keyboard<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
		<span style="color: #000088">$inputOb</span><span style="color: #339933">-></span><span style="color: #004000">input</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">class</span> keyboard <span style="color: #009900">{</span>
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> input<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
		<span style="color: #b1b100">print</span> <span style="color: #0000ff">"Keyboard Input"</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
<span style="color: #009900">}</span>
<span style="color: #666666;font-style: italic">/*
 * 以上代码 PC类的 IN() 方法就依赖于keyboard类,这样显然是不太好的,看以下的改造
 * 本文由WEB开发笔记(www.chhua.com)原创发表,转载请注明*/</span>
 
<span style="color: #000000;font-weight: bold">class</span> pc1 <span style="color: #009900">{</span>
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> In<span style="color: #009900">(</span>keyboard <span style="color: #000088">$inputOb</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
		<span style="color: #000088">$inputOb</span><span style="color: #339933">-></span><span style="color: #004000">input</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: #666666;font-style: italic">/*改进之后,显然好了很多,是将对象以参数的方式传给PC,并规定了对象的类型,可是,鼠标也能输入呀,所以不得不用上以下的方法,插入一个接口来实现 */</span>
 
<span style="color: #000000;font-weight: bold">interface</span> inputClass<span style="color: #009900">{</span>
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> input<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> keyboard1 implements inputClass<span style="color: #009900">{</span>
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> input<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
		<span style="color: #b1b100">print</span> <span style="color: #0000ff">"Keyboard Input"</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> mouse implements inputClass<span style="color: #009900">{</span>
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> input<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
		<span style="color: #b1b100">print</span> <span style="color: #0000ff">"Mouse Input"</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> pc2<span style="color: #009900">{</span>
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> In<span style="color: #009900">(</span>inputClass <span style="color: #000088">$inputOb</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
		<span style="color: #000088">$inputOb</span><span style="color: #339933">-></span><span style="color: #004000">input</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: #666666;font-style: italic">/*实例一个PC2 的对象*/</span>
 
<span style="color: #000088">$pc2</span><span style="color: #339933">=</span><span style="color: #000000;font-weight: bold">new</span> pc2<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #000088">$pc2</span><span style="color: #339933">-></span><span style="color: #004000">In</span><span style="color: #009900">(</span><span style="color: #000000;font-weight: bold">new</span> keyboard1<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//输出 "Keyboard Input";</span>
<span style="color: #000088">$pc2</span><span style="color: #339933">-></span><span style="color: #004000">In</span><span style="color: #009900">(</span><span style="color: #000000;font-weight: bold">new</span> mouse<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//输出  "Mouse Input";</span>
 
<span style="color: #666666;font-style: italic">/*以上小小的例子就说明了PHP面向对象编程的依赖倒置原则,呵呵,不能抠字眼哟,老外的东西就是要多理解才可以。*/</span>
<span style="color: #000000;font-weight: bold">?></span>