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

所属分类: 网络编程 / PHP编程 阅读数: 2002
收藏 0 赞 0 分享

抽象策略(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()); // 改变缓存策略 
?>

 具体的大家可以多关注一下脚本之家以前发布的文章

更多精彩内容其他人还在看

php导出CSV抽象类实例

这篇文章主要介绍了php导出CSV抽象类及其用法示例,可实现循环导出功能,从而避免内存不足的问题,需要的朋友可以参考下
收藏 0 赞 0 分享

php实现的zip文件内容比较类

这篇文章主要介绍了php实现的zip文件内容比较类及其用法,可实现比较两个zip文件的内容,返回新增、删除、及相同的文件列表,是非常实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

php实现获取及设置用户访问页面语言类

这篇文章主要介绍了php实现获取及设置用户访问页面语言类,可实现获取/设置用户访问的页面语言,如果用户没有设置访问语言,则读取Accept-Language,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP+FFMPEG实现将视频自动转码成H264标准Mp4文件

最近做一个在线教学网的项目,需要实现上传任意格式视频自动为h264标准视频,使用html5播放。最终使用PHP+FFMPEG实现,在此将详细解决方案分享给大家!
收藏 0 赞 0 分享

PHP会话控制:Session与Cookie详解

这篇文章主要介绍了PHP会话控制:Session与Cookie详解,本文详细讲解了PHP中Session与Cookie的相关知识,涵盖面较广,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP实现手机号码中间四位用星号(*)隐藏的自定义函数分享

这篇文章主要介绍了PHP实现手机号码中间四位用星号(*)隐藏的自定义函数分享,这是一个比较常用的功能,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP获取mysql数据表的字段名称和详细信息的方法

这篇文章主要介绍了PHP获取mysql数据表的字段名称和详细信息的方法,本文同时还给出了获取数据表结构、列出数据库数据表等方法,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP中的output_buffering详细介绍

这篇文章主要介绍了PHP中的output_buffering详细介绍,本文讲解了output buffering的一些高级用法,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP错误Warning: Cannot modify header information - headers already sent by解决方法

这篇文章主要介绍了PHP错误Warning: Cannot modify header information - headers already sent by解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP常用编译参数中文说明

这篇文章主要介绍了PHP常用编译参数中文说明,本文用详细的中文注解了PHP编译参数的作用,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多