|
 |
栏目导栏 |
|
| |
|
|
|
|
 |
资料搜索 |
|
| |
|
|
|
|
 |
热门文章 |
|
| |
|
|
|
|
 |
最新文章 |
|
| |
|
|
|
| |
| |
|
|
|
|
命令链模式 7f2Linux联盟 7f2Linux联盟 命令链 模式以松散耦合主题为基础,发送消息、命令和请求,或通过一组处理程序发送任意内容。每个处理程序都会自行判断自己能否处理请求。如果可以,该请求被处理,进程停止。您可以为系统添加或移除处理程序,而不影响其他处理程序。清单 5 显示了此模式的一个示例。 7f2Linux联盟 7f2Linux联盟 清单 5. Chain.php 7f2Linux联盟 7f2Linux联盟 <?php 7f2Linux联盟 interface ICommand 7f2Linux联盟 { 7f2Linux联盟 function onCommand( $name, $args ); 7f2Linux联盟 } 7f2Linux联盟 7f2Linux联盟 class CommandChain 7f2Linux联盟 { 7f2Linux联盟 private $_commands = array(); 7f2Linux联盟 7f2Linux联盟 public function addCommand( $cmd ) 7f2Linux联盟 { 7f2Linux联盟 $this->_commands []= $cmd; 7f2Linux联盟 } 7f2Linux联盟 7f2Linux联盟 public function runCommand( $name, $args ) 7f2Linux联盟 { 7f2Linux联盟 foreach( $this->_commands as $cmd ) 7f2Linux联盟 { 7f2Linux联盟 if ( $cmd->onCommand( $name, $args ) ) 7f2Linux联盟 return; 7f2Linux联盟 } 7f2Linux联盟 } 7f2Linux联盟 } 7f2Linux联盟 7f2Linux联盟 class UserCommand implements ICommand 7f2Linux联盟 { 7f2Linux联盟 public function onCommand( $name, $args ) 7f2Linux联盟 { 7f2Linux联盟 if ( $name != 'addUser' ) return false; 7f2Linux联盟 echo( "UserCommand handling 'addUser'\n" ); 7f2Linux联盟 return true; 7f2Linux联盟 } 7f2Linux联盟 } 7f2Linux联盟 7f2Linux联盟 class MailCommand implements ICommand 7f2Linux联盟 { 7f2Linux联盟 public function onCommand( $name, $args ) 7f2Linux联盟 { 7f2Linux联盟 if ( $name != 'mail' ) return false; 7f2Linux联盟 echo( "MailCommand handling 'mail'\n" ); 7f2Linux联盟 return true; 7f2Linux联盟 } 7f2Linux联盟 } 7f2Linux联盟 7f2Linux联盟 $cc = new CommandChain(); 7f2Linux联盟 $cc->addCommand( new UserCommand() ); 7f2Linux联盟 $cc->addCommand( new MailCommand() ); 7f2Linux联盟 $cc->runCommand( 'addUser', null ); 7f2Linux联盟 $cc->runCommand( 'mail', null ); 7f2Linux联盟 ?> 7f2Linux联盟 7f2Linux联盟 此代码定义维护 ICommand 对象列表的 CommandChain 类。两个类都可以实现 ICommand 接口 —— 一个对邮件的请求作出响应,另一个对添加用户作出响应。 图 5 给出了 UML。 7f2Linux联盟 7f2Linux联盟 图 5. 命令链及其相关命令 7f2Linux联盟 7f2Linux联盟 如果您运行包含某些测试代码的脚本,则会得到以下输出: 7f2Linux联盟 7f2Linux联盟 % php chain.php 7f2Linux联盟 UserCommand handling 'addUser' 7f2Linux联盟 MailCommand handling 'mail' 7f2Linux联盟 % 7f2Linux联盟 7f2Linux联盟 代码首先创建 CommandChain 对象,并为它添加两个命令对象的实例。然后运行两个命令以查看谁对这些命令作出了响应。如果命令的名称匹配 UserCommand 或 MailCommand,则代码失败,不发生任何操作。 为处理请求而创建可扩展的架构时,命令链模式很有价值,使用它可以解决许多问题。 7f2Linux联盟 7f2Linux联盟 策略模式 7f2Linux联盟 7f2Linux联盟 我们讲述的最后一个设计模式是策略 模式。在此模式中,算法是从复杂类提取的,因而可以方便地替换。例如,如果要更改搜索引擎中排列页的方法,则策略模式是一个不错的选择。思考一下搜索引擎的几个部分 —— 一部分遍历页面,一部分对每页排列,另一部分基于排列的结果排序。在复杂的示例中,这些部分都在同一个类中。通过使用策略模式,您可将排列部分放入另一个类中,以便更改页排列的方式,而不影响搜索引擎的其余代码。 7f2Linux联盟 7f2Linux联盟 作为一个较简单的示例,清单 6 显示了一个用户列表类,它提供了一个根据一组即插即用的策略查找一组用户的方法。 7f2Linux联盟 7f2Linux联盟 清单 6. Strategy.php 7f2Linux联盟 7f2Linux联盟 <?php 7f2Linux联盟 interface IStrategy 7f2Linux联盟 { 7f2Linux联盟 function filter( $record ); 7f2Linux联盟 } 7f2Linux联盟 7f2Linux联盟 class FindAfterStrategy implements IStrategy 7f2Linux联盟 { 7f2Linux联盟 private $_name; 7f2Linux联盟 7f2Linux联盟 public function __construct( $name ) 7f2Linux联盟 { 7f2Linux联盟 $this->_name = $name; 7f2Linux联盟 } 7f2Linux联盟 7f2Linux联盟 public function filter( $record ) 7f2Linux联盟 { 7f2Linux联盟 return strcmp( $this->_name, $record ) <= 0; 7f2Linux联盟 } 7f2Linux联盟 } 7f2Linux联盟 7f2Linux联盟 class RandomStrategy implements IStrategy 7f2Linux联盟 { 7f2Linux联盟 public function filter( $record ) 7f2Linux联盟 { 7f2Linux联盟 return rand( 0, 1 ) >= 0.5; 7f2Linux联盟 } 7f2Linux联盟 } 7f2Linux联盟 7f2Linux联盟 class UserList 7f2Linux联盟 { 7f2Linux联盟 private $_list = array(); 7f2Linux联盟 7f2Linux联盟 public function __construct( $names ) 7f2Linux联盟 { 7f2Linux联盟 if ( $names != null ) 7f2Linux联盟 { 7f2Linux联盟 foreach( $names as $name ) 7f2Linux联盟 { 7f2Linux联盟 $this->_list []= $name; 7f2Linux联盟 } 7f2Linux联盟 } 7f2Linux联盟 } 7f2Linux联盟 7f2Linux联盟 public function add( $name ) 7f2Linux联盟 { 7f2Linux联盟 $this->_list []= $name; 7f2Linux联盟 } 7f2Linux联盟 7f2Linux联盟 public function find( $filter ) 7f2Linux联盟 { 7f2Linux联盟 $recs = array(); 7f2Linux联盟 foreach( $this->_list as $user ) 7f2Linux联盟 { 7f2Linux联盟 if ( $filter->filter( $user ) ) 7f2Linux联盟 $recs []= $user; 7f2Linux联盟 } 7f2Linux联盟 return $recs; 7f2Linux联盟 } 7f2Linux联盟 } 7f2Linux联盟 7f2Linux联盟 $ul = new UserList( array( "Andy", "Jack", "Lori", "Megan" ) ); 7f2Linux联盟 $f1 = $ul->find( new FindAfterStrategy( "J" ) ); 7f2Linux联盟 print_r( $f1 ); 7f2Linux联盟 7f2Linux联盟 $f2 = $ul->find( new RandomStrategy() ); 7f2Linux联盟 print_r( $f2 ); 7f2Linux联盟 ?> 7f2Linux联盟 7f2Linux联盟 7f2Linux联盟 UserList 类是打包名称数组的一个包装器。它实现 find 方法,该方法利用几个策略之一来选择这些名称的子集。这些策略由 IStrategy 接口定义,该接口有两个实现:一个随机选择用户,另一个根据指定名称选择其后的所有名称。运行测试代码时,将得到以下输出: 7f2Linux联盟 7f2Linux联盟 % php strategy.php 7f2Linux联盟 Array 7f2Linux联盟 ( 7f2Linux联盟 [0] => Jack 7f2Linux联盟 [1] => Lori 7f2Linux联盟 [2] => Megan 7f2Linux联盟 ) 7f2Linux联盟 Array 7f2Linux联盟 ( 7f2Linux联盟 [0] => Andy 7f2Linux联盟 [1] => Megan 7f2Linux联盟 ) 7f2Linux联盟 % 7f2Linux联盟 7f2Linux联盟 测试代码为两个策略运行同一用户列表,并显示结果。在第一种情况中,策略查找排列在 J 后的任何名称,所以您将得到 Jack、Lori 和 Megan。第二个策略随机选取名称,每次会产生不同的结果。在这种情况下,结果为 Andy 和 Megan。 7f2Linux联盟 7f2Linux联盟 策略模式非常适合复杂数据管理系统或数据处理系统,二者在数据筛选、搜索或处理的方式方面需要较高的灵活性。 7f2Linux联盟 7f2Linux联盟 结束语 7f2Linux联盟 7f2Linux联盟 本文介绍的仅仅是 PHP 应用程序中使用的几种最常见的设计模式。在设计模式 一书中演示了更多的设计模式。不要因架构的神秘性而放弃。模式是一种绝妙的理念,适用于任何编程语言、任何技能水平。
Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论 |
|
|
|
|
|