php--class的策略模式
发布日期:2021-05-06 21:10:08 浏览次数:23 分类:精选文章

本文共 589 字,大约阅读时间需要 1 分钟。

getMessage(); } }}//业务类class calcContext{ //私有变量,保存策略类实例 private $_strategy = null; //构造函数,参数为策略类 public function __construct(calc2 $select) { $this->_strategy = $select; } //方法,设置策略类 public function setCalc(calc2 $select) { return $this->_strategy = $select; } //返回值,根据加载的策略类不同,动态调用接口方法 public function calcResult($num1,$num2) { return $this->_strategy->getValue($num1,$num2); }}$r = new calcContext(new addStrategy());echo $r->calcResult(100,99);echo "
";$r->setCalc(new mulStrategy());echo $r->calcResult(100,99);

 

上一篇:php--ajax异步读取数据的示例
下一篇:php--class的单例模式

发表评论

最新留言

关注你微信了!
[***.104.42.241]2025年03月31日 15时43分26秒