手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>网络编程>Php编程>列表

更好的构造开发模板 五种PHP设计模式

来源:互联网 作者:west263.com 时间:2008-02-23
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!


  清单 5. Chain.php

<?php
interface ICommand
{
 function onCommand( $name, $args );
}

class CommandChain
{
 private $_commands = array();

 public function addCommand( $cmd )
 {
  $this->_commands []= $cmd;
 }

 public function runCommand( $name, $args )
 {
  foreach( $this->_commands as $cmd )
  {
   if ( $cmd->onCommand( $name, $args ) )
    return;
  }
 }
}

class UserCommand implements ICommand
{
 public function onCommand( $name, $args )
 {
  if ( $name != 'addUser' ) return false;
  echo( "UserCommand handling 'addUser'\n" );
  return true;
 }
}

class MailCommand implements ICommand
{
 public function onCommand( $name, $args )
 {
  if ( $name != 'mail' ) return false;
  echo( "MailCommand handling 'mail'\n" );
  return true;
 }
}

$cc = new CommandChain();
$cc->addCommand( new UserCommand() );
$cc->addCommand( new MailCommand() );
$cc->runCommand( 'addUser', null );
$cc->runCommand( 'mail', null );
?>

  此代码定义维护 ICommand 对象列表的 CommandChain 类。两个类都可以实现 ICommand 接口 —— 一个对邮件的请求作出响应,另一个对添加用户作出响应。 图 5 给出了 UML。


图 5. 命令链及其相关命令

  如果您运行包含某些测试代码的脚本,则会得到以下输出:

% php chain.php
UserCommand handling 'addUser'
MailCommand handling 'mail'
%

  代码首先创建 CommandChain 对象,并为它添加两个命令对象的实例。然后运行两个命令以查看谁对这些命令作出了响应。如果命令的名称匹配 UserCommand 或 MailCommand,则代码失败,不发生任何操作。 为处理请求而创建可扩展的架构时,命令链模式很有价值,使用它可以解决许多问题。

  策略模式

  我们讲述的最后一个设计模式是策略 模式。在此模式中,算法是从复杂类提取的,因而可以方便地替换。例如,如果要更改搜索引擎中排列页的方法,则策略模式是一个不错的选择。思考一下搜索引擎的几个部分 —— 一部分遍历页面,一部分对每页排列,另一部分基于排列的结果排序。在复杂的示例中,这些部分都在同一个类中。通过使用策略模式,您可将排列部分放入另一个类中,以便更改页排列的方式,而不影响搜索引擎的其余代码。

  作为一个较简单的示例,清单 6 显示了一个用户列表类,它提供了一个根据一组即插即用的策略查找一组用户的方法。

  清单 6. Strategy.php

<?php
interface IStrategy
{
 function filter( $record );
}

class FindAfterStrategy implements IStrategy
{
 private $_name;

 public function __construct( $name )
 {
  $this->_name = $name;
 }

 public function filter( $record )
 {
  return strcmp( $this->_name, $record ) <= 0;
 }
}

class RandomStrategy implements IStrategy
{
 public function filter( $record )
 {
  return rand( 0, 1 ) >= 0.5;
 }
}

class UserList
{
 private $_list = array();

 public function __construct( $names )
 {
  if ( $names != null )
  {
   foreach( $names as $name )
   {
    $this->_list []= $name;
   }
  }
 }

 public function add( $name )
 {
  $this->_list []= $name;
 }

 public function find( $filter )
 {
  $recs = array();
  foreach( $this->_list as $user )
  {
   if ( $filter->filter( $user ) )
    $recs []= $user;
  }
  return $recs;
 }
}

$ul = new UserList( array( "Andy", "Jack", "Lori", "Megan" ) );
$f1 = $ul->find( new FindAfterStrategy( "J" ) );
print_r( $f1 );

$f2 = $ul->find( new RandomStrategy() );
print_r( $f2 );
?>


图 6. 用户列表和用于选择用户的策略

  UserList 类是打包名称数组的一个包装器。它实现 find 方法,该方法利用几个策略之一来选择这些名称的子集。这些策略由 IStrategy 接口定义,该接口有两个实现:一个随机选择用户,另一个根据指定名称选择其后的所有名称。运行测试代码时,将得到以下输出:

% php strategy.php
Array
(
[0] => Jack
[1] => Lori
[2] => Megan
)
Array
(
[0] => Andy
[1] => Megan
)
%

  测试代码为两个策略运行同一用户列表,并显示结果。在第一种情况中,策略查找排列在 J 后的任何名称,所以您将得到 Jack、Lori 和 Megan。第二个策略随机选取名称,每次会产生不同的结果。在这种情况下,结果为 Andy 和 Megan。

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!