php设计模式-策略模式
2016-06-27 12:04
603 查看
概念:定义一系列的算法,把每一个算法封装起来,并且使它们可相互替换。本模式使得算法可独立于使用它的客户而变化,也成为政策模式(Policy)。
Define a family of algorithms,encapsulate each one,and make them interchangeable.Strategy Lets the algorithmvary independently from clients that use it.
应用场景:
1、一个网站针对不同地域的用户跳转到不同的商品类目,并且所有广告位展示不同的广告。
2、我们到达一个目的地,选择汽车、地铁、自行车或步行等不同策略,虽然结果相同,单资源不同。
模型组成:
Context(环境类):用一个ConcreteStrategy对象来配置,维护一个对Strategy对象的引用。可定义一个接口来让Strategy访问它的数据。
Strategy(抽象策略类):定义所有支持的算法的公共接口,Context使用这个接口来调用某ConcreteStrategy定义的算法。
ConcreteStrategy(具体策略类):以Strategy接口实现某具体算法。
下面以出行策略为例。
\Libs\Strategy.php
<?php
namespace Libs;
interface Strategy
{
public function Algorithm();
}
\Libs\Strategy
AirStrategy.php
<?php
namespace Libs\Strategy;
use Libs\Strategy;
class AirStrategy implements Strategy
{
public function Algorithm()
{
echo "Air\n";
}
}
TrainStrategy.php
<?php
namespace Libs\Strategy;
use Libs\Strategy;
class TrainStrategy implements Strategy
{
public function Algorithm()
{
echo "Train\n";
}
}
BusStrategy.php
<?php
namespace Libs\Strategy;
use Libs\Strategy;
class BusStrategy implements Strategy
{
public function Algorithm()
{
echo "Bus\n";
}
}
PersonStrategy.php
<?php
namespace Libs;
class PersonStrategy
{
private $_strategy = null;
public function __construct(Strategy $strategy)
{
$this->_strategy = $strategy;
}
public function setStrategy(Strategy $strategy)
{
$this->_strategy = $strategy;
}
public function goStrategy()
{
return $this->_strategy->Algorithm();
}
}
调用$person = new Libs\PersonStrategy(new Libs\Strategy\AirStrategy());
$person->goStrategy();
从以上代码中我们可以看出,客户端必须知道所有的策略类,并自行决定使用哪一个策略类,本模式的潜在缺点就是,一个客户要选择一个合适的Strategy,就必须知道这些Strategy到底有何不同。此时不得不向客户暴露具体的实现问题。因此仅当这些不同行为变体与客户相关的行为时,才需要使用策略模式。
推荐阅读:
策略模式 PHP版
PHP设计模式-策略模式
Define a family of algorithms,encapsulate each one,and make them interchangeable.Strategy Lets the algorithmvary independently from clients that use it.
应用场景:
1、一个网站针对不同地域的用户跳转到不同的商品类目,并且所有广告位展示不同的广告。
2、我们到达一个目的地,选择汽车、地铁、自行车或步行等不同策略,虽然结果相同,单资源不同。
模型组成:
Context(环境类):用一个ConcreteStrategy对象来配置,维护一个对Strategy对象的引用。可定义一个接口来让Strategy访问它的数据。
Strategy(抽象策略类):定义所有支持的算法的公共接口,Context使用这个接口来调用某ConcreteStrategy定义的算法。
ConcreteStrategy(具体策略类):以Strategy接口实现某具体算法。
下面以出行策略为例。
\Libs\Strategy.php
<?php
namespace Libs;
interface Strategy
{
public function Algorithm();
}
\Libs\Strategy
AirStrategy.php
<?php
namespace Libs\Strategy;
use Libs\Strategy;
class AirStrategy implements Strategy
{
public function Algorithm()
{
echo "Air\n";
}
}
TrainStrategy.php
<?php
namespace Libs\Strategy;
use Libs\Strategy;
class TrainStrategy implements Strategy
{
public function Algorithm()
{
echo "Train\n";
}
}
BusStrategy.php
<?php
namespace Libs\Strategy;
use Libs\Strategy;
class BusStrategy implements Strategy
{
public function Algorithm()
{
echo "Bus\n";
}
}
PersonStrategy.php
<?php
namespace Libs;
class PersonStrategy
{
private $_strategy = null;
public function __construct(Strategy $strategy)
{
$this->_strategy = $strategy;
}
public function setStrategy(Strategy $strategy)
{
$this->_strategy = $strategy;
}
public function goStrategy()
{
return $this->_strategy->Algorithm();
}
}
调用$person = new Libs\PersonStrategy(new Libs\Strategy\AirStrategy());
$person->goStrategy();
从以上代码中我们可以看出,客户端必须知道所有的策略类,并自行决定使用哪一个策略类,本模式的潜在缺点就是,一个客户要选择一个合适的Strategy,就必须知道这些Strategy到底有何不同。此时不得不向客户暴露具体的实现问题。因此仅当这些不同行为变体与客户相关的行为时,才需要使用策略模式。
推荐阅读:
策略模式 PHP版
PHP设计模式-策略模式
相关文章推荐
- Sftp和ftp over ssh2的区别
- thinkphp 导出exl功能
- Robi改造计划RTP---状态更新:tensorflow已通,ROS中
- php socket客户端及服务器端应用实例
- PHP Cookies
- PHP文件上传
- thinkphp中URL带参数跳转
- 几种常用的php字符串函数
- PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep
- php操作mysql数据库的函数(天龙八部)
- PHPstorm端口配置问题
- php书写效率问题
- php学习笔记-会话控制简单介绍session和cookie(一)
- stp
- thinkphp3.23整合phpexcel
- Laravel 实例教程 —— 如何在Laravel 5.1中进行自定义包开发
- Laravel 5.1 定义事件、事件监听器以及触发事件实例教程
- JHTP练习题及课题_第五章_控制语句Part 2-逻辑运算符
- php 读取表结构自动生成php类
- 获取内容的一个curl类(采集专用)