WEB开发笔记 www.chhua.com 每日练习 PHP高级对象构建——工厂模式的使用

之前我写去关于工厂设计模式的文章(http://www.chhua.com/web-note772),实际上,工厂模式包含普通工厂模式和抽象工厂模式,但是,不管是什么工厂模式,它们都是有一个作用,那就是生成对象。

好了,那我们用下面最最简单的例子,再把PHP设计模式中的厂模式再演示一下。

我自己总结了一下,工厂模式的三个要素:
一、产品模型
二、产品实例
三、工厂车间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 
<span style="color: #000000;font-weight: bold"><?php</span>
abstract <span style="color: #000000;font-weight: bold">class</span> prModel <span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//产品模型</span>
	abstract <span style="color: #000000;font-weight: bold">function</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/link" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">link</span></a><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> webLink <span style="color: #000000;font-weight: bold">extends</span> prModel<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> <a rel="noopener noreferrer nofollow" href="http://www.php.net/link" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">link</span></a><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
		<span style="color: #b1b100">echo</span> <span style="color: #0000ff">"www.chhua.com"</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> gongchang <span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//工厂</span>
	static <span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> createLink <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> webLink<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: #000088">$weblink</span><span style="color: #339933">=</span>gongchang<span style="color: #339933">::</span><span style="color: #004000">createLink</span><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: #000088">$weblink</span><span style="color: #339933">-></span><span style="color: #004000">link</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>

以上方法,就简单的说明了工厂类的使用方法。关注面向对象,关注PHP,请 记住我的PHP进阶群(63417682)和我的网站www.chhua.com,