php 常见的五种设计模式
2013-04-20 19:28
471 查看
1 策略模式
<?php
interface IStrategy
{
function filter($rec);
}
class Finder implements IStrategy
{
private $name;
public function __construct($name)
{
$this->_name=$name;
}
public function filter($n)
{
return strcmp($this->_name,$n) <=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 Finder( "J" ) );
print_r( $f1 );
$f2 = $ul->find( new RandomStrategy() );
print_r( $f2 );
?>
2 命令链模式
<?php
interface ICommand
{
function onCommand($name,$args);
}
class CommandChain
{
private $_cmds=array();
public function addCmd($cmd)
{
$this->_cmds[]=$cmd;
}
public function runCmd($name,$args)
{
foreach($this->_cmds as $v)
{
if($v->onCommand($name,$args))
return ;
}
}
}
class UserCmd implements ICommand
{
public function onCommand($name,$args)
{
if($name=='user'){
echo 'here is test run';
return true;
}
return false;
}
}
class MailCmd implements ICommand
{
public function onCommand($name,$args)
{
if($name=='mail'){
echo 'send a mail';
return true;
}
return false;
}
}
$cmdControll=new CommandChain();
$cmdControll->addCmd(new UserCmd());
$cmdControll->addCmd(new MailCmd());
$cmdControll->runCmd('user','safdsaf');
3 观察模式
<?php
interface IStrategy
{
function filter($rec);
}
class Finder implements IStrategy
{
private $name;
public function __construct($name)
{
$this->_name=$name;
}
public function filter($n)
{
return strcmp($this->_name,$n) <=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 Finder( "J" ) );
print_r( $f1 );
$f2 = $ul->find( new RandomStrategy() );
print_r( $f2 );
?>
2 命令链模式
<?php
interface ICommand
{
function onCommand($name,$args);
}
class CommandChain
{
private $_cmds=array();
public function addCmd($cmd)
{
$this->_cmds[]=$cmd;
}
public function runCmd($name,$args)
{
foreach($this->_cmds as $v)
{
if($v->onCommand($name,$args))
return ;
}
}
}
class UserCmd implements ICommand
{
public function onCommand($name,$args)
{
if($name=='user'){
echo 'here is test run';
return true;
}
return false;
}
}
class MailCmd implements ICommand
{
public function onCommand($name,$args)
{
if($name=='mail'){
echo 'send a mail';
return true;
}
return false;
}
}
$cmdControll=new CommandChain();
$cmdControll->addCmd(new UserCmd());
$cmdControll->addCmd(new MailCmd());
$cmdControll->runCmd('user','safdsaf');
3 观察模式
相关文章推荐