php设计模式 Strategy(策略模式)

yipeiwu_com5年前PHP代码库

抽象策略(Strategy)角色:定义所有支持的算法的公共接口。通常是以一个接口或抽象来实现。Context使用这个接口来调用其ConcreteStrategy定义的算法。

具体策略(ConcreteStrategy)角色:以Strategy接口实现某具体算法。

环境(Context)角色:持有一个Strategy类的引用,用一个ConcreteStrategy对象来配置

核心代码

<?php
interface Strategy { // 抽象策略角色,以接口实现
  public function algorithmInterface(); // 算法接口
}

class ConcreteStrategyA implements Strategy { // 具体策略角色A 
  public function algorithmInterface() {}
}

class ConcreteStrategyB implements Strategy { // 具体策略角色B 
  public function algorithmInterface() {}
}

class ConcreteStrategyC implements Strategy { // 具体策略角色C
  public function algorithmInterface() {}
}

class Context { // 环境角色
  private $_strategy;
  public function __construct(Strategy $strategy) {
    $this->_strategy = $strategy;
  } 
  public function contextInterface() {
    $this->_strategy->algorithmInterface();
  }
}

// client
$strategyA = new ConcreteStrategyA();
$context = new Context($strategyA);
$context->contextInterface();

$strategyB = new ConcreteStrategyB();
$context = new Context($strategyB);
$context->contextInterface();

$strategyC = new ConcreteStrategyC();
$context = new Context($strategyC);
$context->contextInterface();

其他代码

<?php 
/** 
* 策略模式(Strategy.php) 
* 
* 定义一系列算法,把它们一个个封装起来,并且使它们可相互替换,使用得算法的变化可独立于使用它的客户 
* 
*/ 

// ---以下是一系列算法的封闭---- 
interface CacheTable 
{ 
public function get($key); 
public function set($key,$value); 
public function del($key); 
} 

// 不使用缓存 
class NoCache implements CacheTable 
{ 
public function __construct(){ 
echo "Use NoCache<br/>"; 
} 

public function get($key) 
{ 
return false; 
} 

public function set($key,$value) 
{ 
return true; 
} 

public function del($key) 
{ 
return false; 
} 
} 

// 文件缓存 
class FileCache implements CacheTable 
{ 
public function __construct() 
{ 
echo "Use FileCache<br/>"; 
// 文件缓存构造函数 
} 

public function get($key) 
{ 
// 文件缓存的get方法实现 
} 

public function set($key,$value) 
{ 
// 文件缓存的set方法实现 
} 

public function del($key) 
{ 
// 文件缓存的del方法实现 
} 
} 

// TTServer 
class TTCache implements CacheTable 
{ 
public function __construct() 
{ 
echo "Use TTCache<br/>"; 
// TTServer缓存构造函数 
} 

public function get($key) 
{ 
// TTServer缓存的get方法实现 
} 

public function set($key,$value) 
{ 
// TTServer缓存的set方法实现 
} 

public function del($key) 
{ 
// TTServer缓存的del方法实现 
} 
} 

// -- 以下是使用不用缓存的策略 ------ 
class Model 
{ 
private $_cache; 
public function __construct() 
{ 
$this->_cache = new NoCache(); 
} 

public function setCache($cache) 
{ 
$this->_cache = $cache; 
} 
} 

class UserModel extends Model 
{ 
} 

class PorductModel extends Model 
{ 
public function __construct() 
{ 
$this->_cache = new TTCache(); 
} 
} 

// -- 实例一下 --- 
$mdlUser = new UserModel(); 
$mdlProduct = new PorductModel(); 
$mdlProduct->setCache(new FileCache()); // 改变缓存策略 
?>

 具体的大家可以多关注一下【宜配屋www.yipeiwu.com】以前发布的文章

相关文章

php上传图片并压缩的实现方法

本文实例讲解了php上传图片并压缩的实现方法,之前一篇《PHP实现图片上传并压缩》已经为大家进行了简单介绍,此次实现上传图片然后按照比例缩略图,指定缩略图的最大高度或者最大宽度,具体内容...

php对mongodb的扩展(初出茅庐)

我们的php mongodb也能做mysql、sqlserver能做的几乎所有功能,本文将详细介绍 一、操作符 操作符相信大家肯定都知道了,就是等于、大于、小于、不等于、大于等于、小于等...

php使用PDO获取结果集的方法

本文实例讲述了php使用PDO获取结果集的方法。分享给大家供大家参考,具体如下: fetch()方法 fetch()方法用于获取结果集的下一行,语法如下: mixed PDOStatem...

PHP中空字符串介绍0、null、empty和false之间的关系

PHP中空字符串介绍0、null、empty和false之间的关系

如果用错方法函数或是用少了,如果几个方法函数的逻辑顺序错了,很可能就是一个漏洞,而且不容易找出来。纠结啊~ 上网找找看哪位高人有总结出相关的结论出来,果然有!不过似乎不是很全面,我在这里...

PHP实现的随机IP函数【国内IP段】

本文实例讲述了PHP实现的随机IP函数。分享给大家供大家参考,具体如下: function get_rand_ip(){ $arr_1 = array("218","218","...