您的位置:首页 > 编程语言 > PHP开发

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设计模式-策略模式
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: