策略模式
2015-06-26 14:52
141 查看
/** * 策略抽象类 规定策略名称 */ abstract class strategy { abstract function operating(); } /** * 第一个策略 */ class strategy_1 extends strategy { public function operating() { echo '第一个策略'; } } /** * 第二个策略 */ class strategy_2 extends strategy { public function operating() { echo'第二个策略'; } } /** * 策略生成器 */ class strategy_context { private $strategy; public function __construct($num) { //根据不同的条件生成不同的策略 if($num == 1) { $this->strategy = new strategy_1(); }else{ $this->strategy = new strategy_2(); } } public function operating() { return $this->strategy->operating(); } } $strategy_context = new strategy_context(1); $strategy_context->operating();
策略模式(strategy):
它定义了算法家族,分别将各个算法封装起来,让各个算法之间可以相互替换,此模式让算法的变化,不会影响到使用算法的客户端。
相关文章推荐
- Java学习-015-CSV 文件写入实例源代码
- Hbase中文官方教程
- MiniTwitterSimulate记住密码
- I/O复用poll
- Nmap扫描教程之Nmap基础知识
- 对js对象定义几种方式的理解
- http://selectorgadget.com/
- (LeetCode 53)Maximum Subarray
- 高性能优化Web前端
- spring AspectJ的Execution表达式-备忘笔记
- iOS三种视图切换 (UITabBarController,UINavigationController,模态窗口)
- Table中td的长字符串换行处理
- 关于cocos2dx的AnchorPoint
- Ruby更改gem source
- 经常遇到build.xml,是干什么用的?
- 开创学习的四核时代-iTOP-4412开发板开源硬件平台
- Unity3d_NGUI和UGUI的学习
- mysql 备份和恢复的两条命令
- 栈帧的学习好文
- xml 报表问题