PHP策略模式包括哪些,核心代码是什么
Admin 2022-11-21 群英技术资讯 447 次浏览
抽象策略(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进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
php设计模式下拥有各种各样的模式,下文中小编将为大家列举出能够提供一个访问它的全局访问点Singleton(单例模式)的代码详解,有需要的程序员可以直接在下文中拷贝使用。
在laravel中,队列可以用于允许在表的前端进行删除操作,而在表的后端进行插入操作;队列是一种操作受限制的线性表,进行插入操作的称为队尾,删除操作的端被称为队头,队列可以将耗时任务的处理往后推延,能够提升web请求的响应速度。
这篇文章主要介绍了PHP编程之检查两个给定整数在不在范围内的内容,不知道朋友们对这方面感不感兴趣,如果有需要可以参考下。接下来的时间就请看看小编所整理的资料。
PHP查询IP地址归属地的步骤:1、开通IP地址归属地接口服务,获取接口请求Key;2、调用接口API发出请求,根据查询的IP地址,查询该IP所属的区域;3、自定义juheHttpRequest()获取请求接口返回的内容即可。
在laravel中,fill方法是一个给Eloquent实例赋值属性的方法,该方法可以理解为用于过滤前端传输过来的与模型中对应的多余字段;当调用该方法时,会先去检测当前Model的状态,根据fillable数组的设置,Model会处于不同的状态。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008