PHP策略模式包括哪些,核心代码是什么
Admin 2022-11-21 群英技术资讯 688 次浏览
这篇文章给大家分享的是PHP策略模式包括哪些,核心代码是什么。小编觉得挺实用的,因此分享给大家做个参考,文中的介绍得很详细,而要易于理解和学习,有需要的朋友可以参考,接下来就跟随小编一起了解看看吧。抽象策略(Strategy)角色:定义所有支持的算法的公共接口。通常是以一个接口或抽象来实现。Context使用这个接口来调用其ConcreteStrategy定义的算法。
具体策略(ConcreteStrategy)角色:以Strategy接口实现某具体算法。
环境(Context)角色:持有一个Strategy类的引用,用一个ConcreteStrategy对象来配置

核心代码
_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();
其他代码
";
}
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
";
// 文件缓存构造函数
}
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
";
// 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()); // 改变缓存策略
?>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
Laravel Elixir是一个API,该API集成了Gulp,为编译Laravel项目中的Less、Sass、CoffeeScript以及处理许多其他日常任务提供了一个简单的解决方案,从而减少编写上述繁琐任务的时间,有效提高编程效率。
很多新手对于任务调度不是很清楚,简单介绍一下,任务调度指的是根据一定的约束规定,将CPU分配给符合条件的任务使用。这篇文章给大家分享的是关于PHP实现协程任务调度的实例,感兴趣的朋友可以了解一下。
责任链模式是一种行为型模式,它包含了一些命令对象和一系列的处理对象。每一个处理对象决定它能处理哪些命令对象,它也知道如何将它不能处理的命令对象传递给该链中的下...
在本篇文章里小编给大家整理的是一篇关于PHP随机生成用户信息实例分析内容,对此有兴趣的朋友们可以测试参考下。
在开发过程中,有时候我们会需要在PHP函数在运行Linux系统代码,那么我们就会使用到这些函数,shell_exec函数,exec函数,system函数,passthru函数,popen函数,proc_open函数。下面我们就来详细了解下PHP执行linux命令可能会用到的六个函数。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008