WEB开发笔记 www.chhua.com 每日练习 PHP设计模式注册表模式

注册表模式其实是一个单例模式,注册表类提供静态方法(或单例对象的实例化方法)来让其它对象访问其中的数据(通常是对象)。整个系统中的每个对象都可以访问这些数据对象。
以前我也写过一个注册表类http://www.chhua.com/web-note1574,不过那一个不能进行多个类的注册,下面用数组对类进行了存储。

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
 
<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> webSite <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">$siteName</span><span style="color: #339933">;</span>
	<span style="color: #000000;font-weight: bold">private</span> <span style="color: #000088">$siteUrl</span><span style="color: #339933">;</span>
 
	<span style="color: #000000;font-weight: bold">function</span> __construct<span style="color: #009900">(</span><span style="color: #000088">$siteName</span><span style="color: #339933">,</span><span style="color: #000088">$siteUrl</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">siteName</span><span style="color: #339933">=</span><span style="color: #000088">$siteName</span><span style="color: #339933">;</span>
		<span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">siteUrl</span><span style="color: #339933">=</span><span style="color: #000088">$siteUrl</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
 
	<span style="color: #000000;font-weight: bold">function</span> getName<span style="color: #009900">(</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">siteName</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
 
	<span style="color: #000000;font-weight: bold">function</span> getUrl<span style="color: #009900">(</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">siteUrl</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> registry <span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//注册表类 单例模式</span>
	<span style="color: #000000;font-weight: bold">private</span> static <span style="color: #000088">$instance</span><span style="color: #339933">;</span>
	<span style="color: #000000;font-weight: bold">private</span> <span style="color: #000088">$values</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: #666666;font-style: italic">//用数组存放类名称</span>
 
	<span style="color: #000000;font-weight: bold">private</span> <span style="color: #000000;font-weight: bold">function</span> __construct<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #009900">{</span><span style="color: #009900">}</span><span style="color: #666666;font-style: italic">//这个用法决定了这个类不能直接实例化</span>
 
	static <span style="color: #000000;font-weight: bold">function</span> instance<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
		<span style="color: #b1b100">if</span> <span style="color: #009900">(</span><span style="color: #339933">!</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/isset" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">isset</span></a><span style="color: #009900">(</span><span style="color: #000000;font-weight: bold">self</span><span style="color: #339933">::</span><span style="color: #000088">$instance</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #009900">{</span><span style="color: #000000;font-weight: bold">self</span><span style="color: #339933">::</span><span style="color: #000088">$instance</span><span style="color: #339933">=</span><span style="color: #000000;font-weight: bold">new</span> <span style="color: #000000;font-weight: bold">self</span><span style="color: #009900">(</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: #000000;font-weight: bold">self</span><span style="color: #339933">::</span><span style="color: #000088">$instance</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
 
	<span style="color: #000000;font-weight: bold">function</span> get<span style="color: #009900">(</span><span style="color: #000088">$key</span><span style="color: #009900">)</span><span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//获取已经注册了的类</span>
		<span style="color: #b1b100">if</span> <span style="color: #009900">(</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/isset" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">isset</span></a><span style="color: #009900">(</span><span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">values</span><span style="color: #009900">[</span><span style="color: #000088">$key</span><span style="color: #009900">]</span><span style="color: #009900">)</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">values</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: #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: #000000;font-weight: bold">function</span> set<span style="color: #009900">(</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: #666666;font-style: italic">//注册类方法</span>
		<span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">values</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: #000088">$reg</span><span style="color: #339933">=</span>registry<span style="color: #339933">::</span><span style="color: #004000">instance</span><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #000088">$reg</span><span style="color: #339933">-></span><span style="color: #004000">set</span><span style="color: #009900">(</span><span style="color: #0000ff">"website"</span><span style="color: #339933">,</span><span style="color: #000000;font-weight: bold">new</span> webSite<span style="color: #009900">(</span><span style="color: #0000ff">"WEB开发笔记"</span><span style="color: #339933">,</span><span style="color: #0000ff">"www.chhua.com"</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">$website</span><span style="color: #339933">=</span><span style="color: #000088">$reg</span><span style="color: #339933">-></span><span style="color: #004000">get</span><span style="color: #009900">(</span><span style="color: #0000ff">"website"</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//获取类</span>
<span style="color: #b1b100">echo</span> <span style="color: #000088">$website</span><span style="color: #339933">-></span><span style="color: #004000">getName</span><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//输出WEB开发笔记</span>
<span style="color: #b1b100">echo</span> <span style="color: #000088">$website</span><span style="color: #339933">-></span><span style="color: #004000">getUrl</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>

注册表的作用是提供系统级别的对象访问功能。有的同学会说,这样是多此一举,不过小项目中的确没有必要对类进行注册,如果是大项目,还是非常有用的。