PHP设计模式——策略模式
来源:互联网 发布:文泰刻绘2002端口设置 编辑:程序博客网 时间:2024/06/06 09:14
WEB开发笔记 www.chhua.com 每日练习 PHP设计模式——策略模式
策略模式在设计模式中非常重要,可能是现代面向对象设计中最重要的模式。
它用来创建可插入,可替换,可重用的组件。
为了完整简单的说明策略模式,下面的代码演示一个非常简单的例子。
代码演示:例子的实现是实现一个获取HTML代码的类,
类的结构,分为二部分,一部分是获取HTML,一部分是获取相关内容,
1234567891011121314151617181920212223242526272829
<?phpclass HtmlDom{private $strObj;//内容对象public function __construct($strObj){$this->strObj=$strObj;}public function getHtml(){return "<html><body>".$this->strObj->getContent()."</body></html>";}} interface HtmlContent {//内容对象接口public function getContent();} class Hellworld implements HtmlContent{private $world;//内容public function __construct($world){$this->world=$world;}public function getContent(){return "Hello ".$this->world."!";}} $html=new HtmlDom(new Hellworld("www.chhua.com"));echo $html->getHtml();//输出 www.chhua.com ?>
以上是策略模式的一个小小的实例,虽然这个例子微不足道,但这种设计模式在更为复杂的情况下支非常有用
自由转载,转载请注明: 转载自WEB开发笔记 www.chhua.com
0 0
- PHP设计模式——策略模式
- php设计模式——策略模式
- PHP设计模式——策略模式
- PHP设计模式——策略模式
- 设计模式—策略模式
- 设计模式—策略模式
- 设计模式—策略模式
- 设计模式—策略模式
- 设计模式—策略模式
- 设计模式—策略模式
- 设计模式—策略模式
- 设计模式—策略模式
- 设计模式—策略模式
- 设计模式—策略模式
- 设计模式—策略模式
- 设计模式—策略模式
- PHP设计模式之——策略模式
- 【PHP设计模式】策略模式
- Ubuntu安装MySQL-python
- 深入研究 Runtime
- ORB SLAM2运行步骤/怎样跑通orbslam2
- 类加载器的工作原理
- Emmet:HTML/CSS代码快速编写神器
- PHP设计模式——策略模式
- 分享一个小工具:Excel表快速转换成JSON字符串
- 分治案例_leetcode_46. Permutations-全排列
- installation directory must be on a local hard drive
- Android实现两次按下返回按钮后退出应用程序
- stillOne Description
- 207. Course Schedule
- 数组转化为列表的一个陷阱
- PHP中call_user_func_array()函数的用法演示