<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>设计模式 &#8211; LPC影子技术分享</title>
	<atom:link href="https://www.mudbest.com/tag/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.mudbest.com</link>
	<description>行影不离,忘之却步.</description>
	<lastBuildDate>Mon, 15 Oct 2012 07:08:35 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.1</generator>
	<item>
		<title>PHP常用设计模式</title>
		<link>https://www.mudbest.com/php%e5%b8%b8%e7%94%a8%e8%ae%be%e8%ae%a1%e6%a8%a1%e5%bc%8f/</link>
		
		<dc:creator><![CDATA[hkshadow]]></dc:creator>
		<pubDate>Mon, 15 Oct 2012 06:42:51 +0000</pubDate>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[设计模式]]></category>
		<category><![CDATA[软件开发]]></category>
		<category><![CDATA[软件设计]]></category>
		<guid isPermaLink="false">http://www.mudbest.com/?p=1561</guid>

					<description><![CDATA[设计模式一书将设计模式引入软件社区，该书的作者是 Erich Gamma、Richard Helm、Ralph ... <a title="PHP常用设计模式" class="read-more" href="https://www.mudbest.com/php%e5%b8%b8%e7%94%a8%e8%ae%be%e8%ae%a1%e6%a8%a1%e5%bc%8f/" aria-label="阅读 PHP常用设计模式">阅读更多</a>]]></description>
										<content:encoded><![CDATA[<p>设计模式一书将设计模式引入软件社区，该书的作者是 Erich Gamma、Richard Helm、Ralph Johnson 和 John Vlissides Design（俗称 &#8220;四人帮&#8221;）。所介绍的设计模式背后的核心概念非常简单。经过多年的软件开发实践，Gamma 等人发现了某些具有固定设计的模式，就像建筑师设计房子和建筑物一样，可以为浴室的位置或厨房的构造方式开发模板。使用这些模板或者说设计模式意味着可以更快地设计更好的建筑物。同样的概念也适用于软件。<span id="more-1561"></span></p>
<p>设计模式不仅代表着更快开发健壮软件的有用方法，而且还提供了以友好的术语封装大型理念的方法。例如，您可以说您正在编写一个提供松散耦合的消息传递系统，也可以说你正在编写名称为观察者的模式。</p>
<p>用较小的示例展示模式的价值是非常困难的。这往往有些大材小用的意味，因为模式实际上是在大型代码库中发挥作用的。本文不展示大型应用程序，所以您需要思索的是在您自己的大型应用程序中应用示例原理的方法 —— 而不是本文演示的代码本身。这不是说您不应该在小应用程序中使用模式。很多良好的应用程序都以小应用程序为起点，逐渐发展到大型应用程序，所以没有理由不以此类扎实的编码实践为基础。</p>
<p>既然您已经了解了设计模式以及它们的有用之处，现在我们来看看 PHP V5 的五种常用模式。</p>
<p><span style="color: #0000ff;"><strong>工厂模式</strong></span><br />
最初在设计模式 一书中，许多设计模式都鼓励使用松散耦合。要理解这个概念，让我们最好谈一下许多开发人员从事大型系统的艰苦历程。在更改一个代码片段时，就会发生问题，系统其他部分 —— 您曾认为完全不相关的部分中也有可能出现级联破坏。</p>
<p>该问题在于紧密耦合。系统某个部分中的函数和类严重依赖于系统的其他部分中函数和类的行为和结构。您需要一组模式，使这些类能够相互通信，但不希望将它们紧密绑定在一起，以避免出现联锁。</p>
<p>在大型系统中，许多代码依赖于少数几个关键类。需要更改这些类时，可能会出现困难。例如，假设您有一个从文件读取的 User 类。您希望将其更改为从数据库读取的其他类，但是，所有的代码都引用从文件读取的原始类。这时候，使用工厂模式会很方便。</p>
<p>工厂模式 是一种类，它具有为您创建对象的某些方法。您可以使用工厂类创建对象，而不直接使用 new。这样，如果您想要更改所创建的对象类型，只需更改该工厂即可。使用该工厂的所有代码会自动更改。</p>
<p><span style="text-decoration: underline;">清单 1 显示工厂类的一个示列。</span><br />
等式的服务器端包括两个部分：数据库和一组 PHP 页面，这些页面允许您添加反馈、请求反馈列表并获取与特定反馈相关的文章。</p>
<p>Factory1.php</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
interface IUser {
	function getName();
}

class User implements IUser {
	public function __construct($id) {
	}
	
	public function getName() {
		return &quot;Jack&quot;;
	}
}

class UserFactory {
	public static function Create($id) {
		return new User ( $id );
	}
}

$uo = UserFactory::Create ( 1 );
echo ($uo-&gt;getName () . &quot;\n&quot;);
?&gt;
</pre>
<p>IUser 接口定义用户对象应执行什么操作。IUser 的实现称为 User，UserFactory 工厂类则创建 IUser 对象。此关系可以用图 1 中的 UML 表示。<br />
图1：</p>
<div style="background: url('http://www.mudbest.com/wp-content/uploads/2012/10/design-patterns.png') no-repeat 0px 0px; width: 229px; height: 86px;"></div>
<p>测试代码会向工厂请求 User 对象，并输出 getName 方法的结果。</p>
<p>有一种工厂模式的变体使用工厂方法。类中的这些公共静态方法构造该类型的对象。如果创建此类型的对象非常重要，此方法非常有用。例如，假设您需要先创建对象，然后设置许多属性。此版本的工厂模式会将该进程封装在单个位置中，这样，不用复制复杂的初始化代码，也不必将复制好的代码在在代码库中到处粘贴。</p>
<p><span style="text-decoration: underline;">清单 2 显示使用工厂方法的一个示例。</span></p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
interface IUser {
	function getName();
}

class User implements IUser {
	public static function Load($id) {
		return new User ( $id );
	}

	public static function Create() {
		return new User ( null );
	}

	public function __construct($id) {
	}

	public function getName() {
		return &quot;Jack&quot;;
	}
}

$uo = User::Load ( 1 );
echo ($uo-&gt;getName () . &quot;\n&quot;);
?&gt;
</pre>
<p>这段代码要简单得多。它仅有一个接口 IUser 和一个实现此接口的 User 类。User 类有两个创建对象的静态方法。此关系可用图 2 中的 UML 表示。</p>
<p>图 2. IUser 接口和带有工厂方法的 user 类<br />
图2：</p>
<div style="background: url('http://www.mudbest.com/wp-content/uploads/2012/10/design-patterns.png') no-repeat 0px -89px; width: 95px; height: 113px;"></div>
<p>如上所述，有时此类模式在规模较小的环境中似乎有些大材小用。不过，最好还是学习这种扎实的编码形式，以便应用于任意规模的项目中。</p>
<p><span style="color: #0000ff;"><strong>单元素模式</strong></span><br />
某些应用程序资源是独占的，因为有且只有一个此类型的资源。例如，通过数据库句柄到数据库的连接是独占的。您希望在应用程序中共享数据库句柄，因为在保持连接打开或关闭时，它是一种开销，在获取单个页面的过程中更是如此。</p>
<p>单元素模式可以满足此要求。如果应用程序每次包含且仅包含一个对象，那么这个对象就是一个单元素（Singleton）。清单 3 中的代码显示了 PHP V5 中的一个数据库连接单元素。</p>
<p><span style="text-decoration: underline;">清单 3. Singleton.php</span></p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
require_once (&quot;DB.php&quot;);

class DatabaseConnection {
	public static function get() {
		static $db = null;
		if ($db == null)
			$db = new DatabaseConnection ();
		return $db;
	}

	private $_handle = null;

	private function __construct() {
		$dsn = 'mysql://root:password@localhost/photos';
		$this-&gt;_handle = &amp; DB::Connect ( $dsn, array () );
	}

	public function handle() {
		return $this-&gt;_handle;
	}
}

print (&quot;Handle = &quot; . DatabaseConnection::get ()-&gt;handle () . &quot;\n&quot;) ;
print (&quot;Handle = &quot; . DatabaseConnection::get ()-&gt;handle () . &quot;\n&quot;) ;
?&gt;
</pre>
<p>此代码显示名为 DatabaseConnection 的单个类。您不能创建自已的 DatabaseConnection，因为构造函数是专用的。但使用静态 get 方法，您可以获得且仅获得一个 DatabaseConnection 对象。此代码的 UML 如图 3 所示。</p>
<p>图 3. 数据库连接单元素<br />
图3：</p>
<div style="background: url('http://www.mudbest.com/wp-content/uploads/2012/10/design-patterns.png') no-repeat 0px -203px; width: 165px; height: 41px;"></div>
<p>在两次调用间，handle 方法返回的数据库句柄是相同的，这就是最好的证明。返回的两个句柄是同一对象。如果您在整个应用程序中使用数据库连接单元素，那么就可以在任何地方重用同一句柄。</p>
<p>您可以使用全局变量存储数据库句柄，但是，该方法仅适用于较小的应用程序。在较大的应用程序中，应避免使用全局变量，并使用对象和方法访问资源。</p>
<p><span style="color: #0000ff;"><strong>观察者模式</strong></span><br />
观察者模式为您提供了避免组件之间紧密耦合的另一种方法。该模式非常简单：一个对象通过添加一个方法（该方法允许另一个对象，即观察者 注册自己）使本身变得可观察。当可观察的对象更改时，它会将消息发送到已注册的观察者。这些观察者使用该信息执行的操作与可观察的对象无关。结果是对象可以相互对话，而不必了解原因。</p>
<p>一个简单示例是系统中的用户列表。清单 4 中的代码显示一个用户列表，添加用户时，它将发送出一条消息。添加用户时，通过发送消息的日志观察者可以观察此列表。</p>
<p><span style="text-decoration: underline;">清单 4. Observer.php</span></p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
interface IObserver {
	function onChanged($sender, $args);
}

interface IObservable {
	function addObserver($observer);
}

class UserList implements IObservable {
	private $_observers = array ();

	public function addCustomer($name) {
		foreach ( $this-&gt;_observers as $obs )
			$obs-&gt;onChanged ( $this, $name );
	}

	public function addObserver($observer) {
		$this-&gt;_observers &#x5B;] = $observer;
	}
}

class UserListLogger implements IObserver {
	public function onChanged($sender, $args) {
		echo (&quot;'$args' added to user list\n&quot;);
	}
}

$ul = new UserList ();
$ul-&gt;addObserver ( new UserListLogger () );
$ul-&gt;addCustomer ( &quot;Jack&quot; );
?&gt;
</pre>
<p>此代码定义四个元素：两个接口和两个类。IObservable 接口定义可以被观察的对象，UserList 实现该接口，以便将本身注册为可观察。IObserver 列表定义要通过怎样的方法才能成为观察者，UserListLogger 实现 IObserver 接口。图 4 的 UML 中展示了这些元素。</p>
<p>图 4. 可观察的用户列表和用户列表事件日志程序<br />
图4：</p>
<div style="background: url('http://www.mudbest.com/wp-content/uploads/2012/10/design-patterns.png') no-repeat 0px -246px; width: 414px; height: 99px;"></div>
<p>测试代码创建 UserList，并将 UserListLogger 观察者添加到其中。然后添加一个消费者，并将这一更改通知 UserListLogger。</p>
<p>认识到 UserList 不知道日志程序将执行什么操作很关键。可能存在一个或多个执行其他操作的侦听程序。例如，您可能有一个向新用户发送消息的观察者，欢迎新用户使用该系统。这种方法的价值在于 UserList 忽略所有依赖它的对象，它主要关注在列表更改时维护用户列表并发送消息这一工作。</p>
<p>此模式不限于内存中的对象。它是在较大的应用程序中使用的数据库驱动的消息查询系统的基础。</p>
<p><span style="color: #0000ff;"><strong>命令链模式</strong></span><br />
命令链 模式以松散耦合主题为基础，发送消息、命令和请求，或通过一组处理程序发送任意内容。每个处理程序都会自行判断自己能否处理请求。如果可以，该请求被处理，进程停止。您可以为系统添加或移除处理程序，而不影响其他处理程序。清单 5 显示了此模式的一个示例。<br />
<span style="text-decoration: underline;">清单 5. Chain.php</span></p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
interface ICommand {
	function onCommand($name, $args);
}

class CommandChain {
	private $_commands = array ();

	public function addCommand($cmd) {
		$this-&gt;_commands &#x5B;] = $cmd;
	}

	public function runCommand($name, $args) {
		foreach ( $this-&gt;_commands as $cmd ) {
			if ($cmd-&gt;onCommand ( $name, $args ))
				return;
		}
	}
}

class UserCommand implements ICommand {
	public function onCommand($name, $args) {
		if ($name != 'addUser')
			return false;
		echo (&quot;UserCommand handling 'addUser'\n&quot;);
		return true;
	}
}

class MailCommand implements ICommand {
	public function onCommand($name, $args) {
		if ($name != 'mail')
			return false;
		echo (&quot;MailCommand handling 'mail'\n&quot;);
		return true;
	}
}

$cc = new CommandChain ();
$cc-&gt;addCommand ( new UserCommand () );
$cc-&gt;addCommand ( new MailCommand () );
$cc-&gt;runCommand ( 'addUser', null );
$cc-&gt;runCommand ( 'mail', null );
?&gt;
</pre>
<p>此代码定义维护 ICommand 对象列表的 CommandChain 类。两个类都可以实现 ICommand 接口 —— 一个对邮件的请求作出响应，另一个对添加用户作出响应。 图 5 给出了 UML。</p>
<p>图 5. 命令链及其相关命令<br />
图5：</p>
<div style="background: url('http://www.mudbest.com/wp-content/uploads/2012/10/design-patterns.png') no-repeat 0px -347px; width: 359px; height: 121px;"></div>
<p>代码首先创建 CommandChain 对象，并为它添加两个命令对象的实例。然后运行两个命令以查看谁对这些命令作出了响应。如果命令的名称匹配 UserCommand 或 MailCommand，则代码失败，不发生任何操作。为处理请求而创建可扩展的架构时，命令链模式很有价值，使用它可以解决许多问题。</p>
<p><span style="color: #0000ff;"><strong>策略模式</strong></span><br />
我们讲述的最后一个设计模式是策略 模式。在此模式中，算法是从复杂类提取的，因而可以方便地替换。例如，如果要更改搜索引擎中排列页的方法，则策略模式是一个不错的选择。思考一下搜索引擎的几个部分 —— 一部分遍历页面，一部分对每页排列，另一部分基于排列的结果排序。在复杂的示例中，这些部分都在同一个类中。通过使用策略模式，您可将排列部分放入另一个类中，以便更改页排列的方式，而不影响搜索引擎的其余代码。</p>
<p>作为一个较简单的示例，清单 6 显示了一个用户列表类，它提供了一个根据一组即插即用的策略查找一组用户的方法。<br />
<span style="text-decoration: underline;">清单 6. Strategy.php</span></p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
interface IStrategy {
	function filter($record);
}

class FindAfterStrategy implements IStrategy {
	private $_name;

	public function __construct($name) {
		$this-&gt;_name = $name;
	}

	public function filter($record) {
		return strcmp ( $this-&gt;_name, $record ) &lt;= 0;
	}
}

class RandomStrategy implements IStrategy {
	public function filter($record) {
		return rand ( 0, 1 ) &gt;= 0.5;
	}
}

class UserList {
	private $_list = array ();

	public function __construct($names) {
		if ($names != null) {
			foreach ( $names as $name ) {
				$this-&gt;_list &#x5B;] = $name;
			}
		}
	}

	public function add($name) {
		$this-&gt;_list &#x5B;] = $name;
	}

	public function find($filter) {
		$recs = array ();
		foreach ( $this-&gt;_list as $user ) {
			if ($filter-&gt;filter ( $user ))
				$recs &#x5B;] = $user;
		}
		return $recs;
	}
}

$ul = new UserList ( array (&quot;Andy&quot;, &quot;Jack&quot;, &quot;Lori&quot;, &quot;Megan&quot; ) );
$f1 = $ul-&gt;find ( new FindAfterStrategy ( &quot;J&quot; ) );
print_r ( $f1 );

$f2 = $ul-&gt;find ( new RandomStrategy () );
print_r ( $f2 );
?&gt;
</pre>
<p>此代码的 UML 如图 6 所示。<br />
图 6. 用户列表和用于选择用户的策略<br />
图6：</p>
<div style="background: url('http://www.mudbest.com/wp-content/uploads/2012/10/design-patterns.png') no-repeat 0px -470px; width: 309px; height: 138px;"></div>
<p>UserList 类是打包名称数组的一个包装器。它实现 find 方法，该方法利用几个策略之一来选择这些名称的子集。这些策略由 IStrategy 接口定义，该接口有两个实现：一个随机选择用户，另一个根据指定名称选择其后的所有名称。</p>
<p>测试代码为两个策略运行同一用户列表，并显示结果。在第一种情况中，策略查找排列在 J 后的任何名称，所以您将得到 Jack、Lori 和 Megan。第二个策略随机选取名称，每次会产生不同的结果。在这种情况下，结果为 Andy 和 Megan。</p>
<p>策略模式非常适合复杂数据管理系统或数据处理系统，二者在数据筛选、搜索或处理的方式方面需要较高的灵活性。</p>
<p><span style="color: #0000ff;"><strong>结束语</strong></span><br />
本文介绍的仅仅是 PHP 应用程序中使用的几种最常见的设计模式。在设计模式 一书中演示了更多的设计模式。不要因架构的神秘性而放弃。模式是一种绝妙的理念，适用于任何编程语言、任何技能水平。</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
