<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >Patterns</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="PHP 手册" HREF="index.html"><LINK REL="UP" TITLE="类与对象(PHP 5)" HREF="language.oop5.html"><LINK REL="PREVIOUS" TITLE="Object Iteration" HREF="language.oop5.iterations.html"><LINK REL="NEXT" TITLE="Magic Methods" HREF="language.oop5.magic.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"></HEAD ><BODY CLASS="sect1" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP 手册</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="language.oop5.iterations.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >章 19. 类与对象(PHP 5)</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="language.oop5.magic.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="language.oop5.patterns" >Patterns</A ></H1 ><P > Patterns are ways to describe best practices and good designs. They show a flexible solution to common programming problems. </P ><DIV CLASS="sect2" ><H2 CLASS="sect2" ><A NAME="language.oop5.patterns.factory" >Factory</A ></H2 ><P > The Factory pattern allows for the instantiation of objects at runtime. It is called a Factory Pattern since it is responsible for "manufacturing" an object. A Parameterized Factory receives the name of the class to instantiate as argument. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5989" ></A ><P ><B >例 19-25. Parameterized Factory Method</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#007700">class </font><font color="#0000BB">Example<br /></font><font color="#007700">{<br /> </font><font color="#FF8000">// The parameterized factory method<br /> </font><font color="#0000BB">public </font><font color="#007700">static function </font><font color="#0000BB">factory</font><font color="#007700">(</font><font color="#0000BB">$type</font><font color="#007700">)<br /> {<br /> if (include_once </font><font color="#DD0000">'Drivers/' </font><font color="#007700">. </font><font color="#0000BB">$type </font><font color="#007700">. </font><font color="#DD0000">'.php'</font><font color="#007700">) {<br /> </font><font color="#0000BB">$classname </font><font color="#007700">= </font><font color="#DD0000">'Driver_' </font><font color="#007700">. </font><font color="#0000BB">$type</font><font color="#007700">;<br /> return new </font><font color="#0000BB">$classname</font><font color="#007700">;<br /> } else {<br /> </font><font color="#0000BB">throw </font><font color="#007700">new </font><font color="#0000BB">Exception </font><font color="#007700">(</font><font color="#DD0000">'Driver not found'</font><font color="#007700">);<br /> }<br /> }<br />}<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P > Defining this method in a class allows drivers to be loaded on the fly. If the <TT CLASS="literal" >Example</TT > class was a database abstraction class, loading a <TT CLASS="literal" >MySQL</TT > and <TT CLASS="literal" >SQLite</TT > driver could be done as follows: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#FF8000">// Load a MySQL Driver<br /></font><font color="#0000BB">$mysql </font><font color="#007700">= </font><font color="#0000BB">Example</font><font color="#007700">::</font><font color="#0000BB">factory</font><font color="#007700">(</font><font color="#DD0000">'MySQL'</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// Load a SQLite Driver<br /></font><font color="#0000BB">$sqlite </font><font color="#007700">= </font><font color="#0000BB">Example</font><font color="#007700">::</font><font color="#0000BB">factory</font><font color="#007700">(</font><font color="#DD0000">'SQLite'</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ></DIV ><DIV CLASS="sect2" ><H2 CLASS="sect2" ><A NAME="language.oop5.patterns.singleton" >Singleton</A ></H2 ><P > The Singleton pattern applies to situations in which there needs to be a single instance of a class. The most common example of this is a database connection. Implementing this pattern allows a programmer to make this single instance easily accessible by many other objects. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN6000" ></A ><P ><B >例 19-26. Singleton Function</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#007700">class </font><font color="#0000BB">Example<br /></font><font color="#007700">{<br /> </font><font color="#FF8000">// Hold an instance of the class<br /> </font><font color="#0000BB">private </font><font color="#007700">static </font><font color="#0000BB">$instance</font><font color="#007700">;<br /> <br /> </font><font color="#FF8000">// A private constructor; prevents direct creation of object<br /> </font><font color="#0000BB">private </font><font color="#007700">function </font><font color="#0000BB">__construct</font><font color="#007700">() <br /> {<br /> echo </font><font color="#DD0000">'I am constructed'</font><font color="#007700">;<br /> }<br /><br /> </font><font color="#FF8000">// The singleton method<br /> </font><font color="#0000BB">public </font><font color="#007700">static function </font><font color="#0000BB">singleton</font><font color="#007700">() <br /> {<br /> if (!isset(</font><font color="#0000BB">self</font><font color="#007700">::</font><font color="#0000BB">$instance</font><font color="#007700">)) {<br /> </font><font color="#0000BB">$c </font><font color="#007700">= </font><font color="#0000BB">__CLASS__</font><font color="#007700">;<br /> </font><font color="#0000BB">self</font><font color="#007700">::</font><font color="#0000BB">$instance </font><font color="#007700">= new </font><font color="#0000BB">$c</font><font color="#007700">;<br /> }<br /><br /> return </font><font color="#0000BB">self</font><font color="#007700">::</font><font color="#0000BB">$instance</font><font color="#007700">;<br /> }<br /> <br /> </font><font color="#FF8000">// Example method<br /> </font><font color="#0000BB">public </font><font color="#007700">function </font><font color="#0000BB">bark</font><font color="#007700">()<br /> {<br /> echo </font><font color="#DD0000">'Woof!'</font><font color="#007700">;<br /> }<br /><br /> </font><font color="#FF8000">// Prevent users to clone the instance<br /> </font><font color="#0000BB">public </font><font color="#007700">function </font><font color="#0000BB">__clone</font><font color="#007700">()<br /> {<br /> </font><font color="#0000BB">trigger_error</font><font color="#007700">(</font><font color="#DD0000">'Clone is not allowed.'</font><font color="#007700">, </font><font color="#0000BB">E_USER_ERROR</font><font color="#007700">);<br /> }<br /><br />}<br /><br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P > This allows a single instance of the <TT CLASS="literal" >Example</TT > class to be retrieved. </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#FF8000">// This would fail because the constructor is private<br /></font><font color="#0000BB">$test </font><font color="#007700">= new </font><font color="#0000BB">Example</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// This will always retrieve a single instance of the class<br /></font><font color="#0000BB">$test </font><font color="#007700">= </font><font color="#0000BB">Example</font><font color="#007700">::</font><font color="#0000BB">singleton</font><font color="#007700">();<br /></font><font color="#0000BB">$test</font><font color="#007700">-></font><font color="#0000BB">bark</font><font color="#007700">();<br /><br /></font><font color="#FF8000">// This will issue an E_USER_ERROR.<br /></font><font color="#0000BB">$test_clone </font><font color="#007700">= </font><font color="#0000BB">clone $test</font><font color="#007700">;<br /><br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="language.oop5.iterations.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >起始页</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="language.oop5.magic.html" ACCESSKEY="N" >下一页</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Object Iteration</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="language.oop5.html" ACCESSKEY="U" >上一级</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Magic Methods</TD ></TR ></TABLE ></DIV ></BODY ></HTML >