策略模式php学习笔记

一、策略模式
策略模式(Strategy)给我的感觉就是对if-else、switch-case这类多分支结构的解耦,把每一种情况封装成一种策略,实现控制器代码的简化。
这种设计模式是对行为、算法的包装,把同一个抽象方法指派给不同的子类对象实现。
工厂模式更像是对对象的管理,而策略模式是对行为的管理
适用场景:

1、 多个类只区别在表现行为不同,可以使用Strategy模式,在运行时动态选择具体要执行的行为。

2、 需要在不同情况下使用不同的策略(算法),或者策略还可能在未来用其它方式来实现。

3、 对客户隐藏具体策略(算法)的实现细节,彼此完全独立。

4、客户端必须知道所有的策略类,并自行决定使用哪一个策略类,策略模式只适用于客户端知道所有的算法或行为的情况。

5、 策略模式造成很多的策略类,每个具体策略类都会产生一个新类。

有时候可以通过把依赖于环境的状态保存到客户端里面,可以使用享元模式来减少对象的数量。

<?php
/**
 * Created by PhpStorm.
 * User: Jiang
 * Date: 2015/5/16
 * Time: 21:46
 */
 
/**抽象策略角色
 * Interface RotateItem
 */
interface RotateItem
{
    function inertiaRotate();
    function unInertisRotate();
}
 
/**具体策略角色——X产品
 * Class XItem
 */
class XItem implements RotateItem
{
    function inertiaRotate()
    {
        echo "我是X产品,我惯性旋转了。<br/>";
    }
 
    function unInertisRotate()
    {
        echo "我是X产品,我非惯性旋转了。<br/>";
    }
}
 
/**具体策略角色——Y产品
 * Class YItem
 */
class YItem implements RotateItem
{
    function inertiaRotate()
    {
        echo "我是Y产品,我<span style='color: #ff0000;'>不能</span>惯性旋转。<br/>";
    }
 
    function unInertisRotate()
    {
        echo "我是Y产品,我非惯性旋转了。<br/>";
    }
}
 
/**具体策略角色——XY产品
 * Class XYItem
 */
class XYItem implements RotateItem
{
    function inertiaRotate()
    {
        echo "我是XY产品,我惯性旋转。<br/>";
    }
 
    function unInertisRotate()
    {
        echo "我是XY产品,我非惯性旋转了。<br/>";
    }
}
 
class contextStrategy
{
    private $item;
 
    function getItem($item_name)
    {
        try
        {
            $class=new ReflectionClass($item_name);
            $this->item=$class->newInstance();
        }
        catch(ReflectionException $e)
        {
            $this->item="";
        }
    }
 
    function inertiaRotate()
    {
        $this->item->inertiaRotate();
    }
 
    function unInertisRotate()
    {
        $this->item->unInertisRotate();
    }
}
<?php
/**
 * Created by PhpStorm.
 * User: Jiang
 * Date: 2015/5/16
 * Time: 21:46
 */
 
header("Content-Type:text/html;charset=utf-8");
 
require_once "./Strategy/Strategy.php";
 
$strategy=new contextStrategy();
 
echo "<span style='color: #ff0000;'>X产品</span><hr/>";
$strategy->getItem('XItem');
$strategy->inertiaRotate();
$strategy->unInertisRotate();
 
echo "<span style='color: #ff0000;'>Y产品</span><hr/>";
$strategy->getItem('YItem');
$strategy->inertiaRotate();
$strategy->unInertisRotate();
 
echo "<span style='color: #ff0000;'>XY产品</span><hr/>";
$strategy->getItem('XYItem');
$strategy->inertiaRotate();
$strategy->unInertisRotate();

猜你喜欢

转载自blog.csdn.net/wangzhae/article/details/107329801