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
 
<span style="color: #000000;font-weight: bold"><?php</span>
<span style="color: #000000;font-weight: bold">class</span> Registry <span style="color: #009900">{</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">$request</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: #666666;font-style: italic">//单例类,通过这个方法返回实例</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> getRequest<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//返回注册的内容类</span>
		<span style="color: #b1b100">return</span> <span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">request</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
 
	<span style="color: #000000;font-weight: bold">function</span> setRequest<span style="color: #009900">(</span>request <span style="color: #000088">$request</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">request</span><span style="color: #339933">=</span><span style="color: #000088">$request</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> request<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">$webname</span><span style="color: #339933">=</span><span style="color: #0000ff">"WEB开发笔记"</span><span style="color: #339933">;</span>
	<span style="color: #000000;font-weight: bold">private</span> <span style="color: #000088">$url</span><span style="color: #339933">=</span><span style="color: #0000ff">"www.chhua.com"</span><span style="color: #339933">;</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">echo</span> <span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">url</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//输出www.chhua.com</span>
	<span style="color: #009900">}</span>
<span style="color: #009900">}</span><span style="color: #666666;font-style: italic">//被注册的空类</span>
 
<span style="color: #666666;font-style: italic">//使用</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">setRequest</span><span style="color: #009900">(</span><span style="color: #000000;font-weight: bold">new</span> request<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
 
<span style="color: #000088">$request</span><span style="color: #339933">=</span><span style="color: #000088">$reg</span><span style="color: #339933">-></span><span style="color: #004000">getRequest</span><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #000088">$request</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">//输出www.chhua.com</span>
<span style="color: #000000;font-weight: bold">?></span>

注册表的作用是提供系统级别的对象访问功能。