Home > php教程 > php手册 > body text

PHP单元素设计模式的原理及应用解析

WBOY
Release: 2016-06-13 11:10:36
Original
1047 people have browsed it

某些应用程序资源是独占的,因为有且只有一个此类型的资源。例如,通过数据库句柄到数据库的连接是独占的。您希望在应用程序中共享数据库句柄,因为在保持连接打开或关闭时,它是一种开销,在获取单个页面的过程中更是如此。PHP单元素设计模式可以满足此要求。如果应用程序每次包含且仅包含一个对象,那么这个对象就是一个单元素(Singleton)。清单 1 中的代码显示了 PHP V5 中的一个数据库连接单元素。

PHP单元素设计模式案例

清单 1. Singleton.php

<ol class="dp-xml">
<li class="alt"><span><strong><font color="#006699"><span class="tag"><!--SP--><span class="tag-name">php</span></span></font></strong><span> </span></span></li>
<li><span>require_once("DB.php");  </span></li>
<li class="alt"><span> </span></li>
<li><span>class DatabaseConnection  </span></li>
<li class="alt"><span>{  </span></li>
<li><span>public static function get()  </span></li>
<li class="alt"><span>{  </span></li>
<li>
<span>static $</span><span class="attribute"><font color="#ff0000">db</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">null</font></span><span>;  </span>
</li>
<li class="alt">
<span>if ( $</span><span class="attribute"><font color="#ff0000">db</font></span><span> == null )  </span>
</li>
<li>
<span>$</span><span class="attribute"><font color="#ff0000">db</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">new</font></span><span> DatabaseConnection();  </span>
</li>
<li class="alt"><span>return $db;  </span></li>
<li><span>}  </span></li>
<li class="alt"><span> </span></li>
<li>
<span>private $</span><span class="attribute"><font color="#ff0000">_handle</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">null</font></span><span>;  </span>
</li>
<li class="alt"><span> </span></li>
<li><span>private function __construct()  </span></li>
<li class="alt"><span>{  </span></li>
<li>
<span>$</span><span class="attribute"><font color="#ff0000">dsn</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">'mysql://root:password@localhost/photos'</font></span><span>;  </span>
</li>
<li class="alt">
<span>$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span class="attribute"><font color="#ff0000">_handle</font></span><span> =& DB::Connect( $dsn, array() );  </span>
</li>
<li><span>}  </span></li>
<li class="alt"><span> </span></li>
<li><span>public function handle()  </span></li>
<li class="alt"><span>{  </span></li>
<li>
<span>return $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>_handle;  </span>
</li>
<li class="alt"><span>}  </span></li>
<li><span>}  </span></li>
<li class="alt"><span> </span></li>
<li>
<span>print( "</span><span class="attribute"><font color="#ff0000">Handle</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">".DatabaseConnection::get()->handle()."</font></span><span>n" );  </span>
</li>
<li class="alt">
<span>print( "</span><span class="attribute"><font color="#ff0000">Handle</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">".DatabaseConnection::get()->handle()."</font></span><span>n" );  </span>
</li>
<li>
<span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span>
</li>
</ol>
Copy after login

此代码显示名为 DatabaseConnection 的单个类。您不能创建自已的 DatabaseConnection,因为构造函数是专用的。但使用静态 get 方法,您可以获得且仅获得一个 DatabaseConnection 对象。在两次调用间,handle 方法返回的数据库句柄是相同的,这就是最好的证明。您可以在命令行中运行代码来观察这一点。

<ol class="dp-xml">
<li class="alt"><span><span>% php singleton.php  </span></span></li>
<li>
<span class="attribute">Handle</span><span> = </span><span class="attribute-value">Object</span><span> id #3  </span>
</li>
<li class="alt">
<span class="attribute">Handle</span><span> = </span><span class="attribute-value">Object</span><span> id #3  </span>
</li>
<li><span>% </span></li>
</ol>
Copy after login

返回的两个句柄是同一对象。如果您在PHP单元素设计模式的整个应用程序中使用数据库连接单元素,那么就可以在任何地方重用同一句柄。您可以使用全局变量存储数据库句柄,但是,该方法仅适用于较小的应用程序。在较大的应用程序中,应避免使用全局变量,并使用对象和方法访问资源。


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template