策略模式(Strategy)

策略模式(Strategy)

策略模式定义

策略模式是把算法,封装起来。使得使用算法和使用算法环境分离开来,当算法发生改变时,咱们之须要修改客户端调用算法,和增长一个新的算法封装类。好比超市收银,收营员判断顾客是不是会员,当顾客不是会员时候,按照原价收取顾客购买商品费用,当顾客是会员的时候,满100减5元。php

策略模式的优势

  • 下降代码耦合度,
  • 增长代码重用性,当须要实现新的算法时候,只须要修改算法部分,而不须要对上下文环境作任何改动;
  • 增长代码可阅读性,避免使用if....else嵌套,形成难以理解的逻辑;

策略模式的缺点

  • 当策略过多的时候,会增长不少类文件;

代码实现

Cashier.phpgit

<?php


namespace App\Creational\Strategy;


class Cashier
{

    private $cutomer;

    public function setStrategy(CustomerAbstract $customer)
    {
        $this->cutomer = $customer;
    }

    public function getMoney($price)
    {
        return $this->cutomer->pay($price);
    }
}

CustomerAbstract.phpgithub

<?php


namespace App\Creational\Strategy;


abstract class CustomerAbstract
{
    abstract public function pay($price);
}

NormalCustomer.php算法

<?php


namespace App\Creational\Strategy;


class NormalCustomer extends CustomerAbstract
{

    public function pay($price)
    {
        return $price;
    }
}

VipCustomer.php微信

<?php


namespace App\Creational\Strategy;


class VipCustomer extends CustomerAbstract
{

    public function pay($price)
    {
        return $price - floor($price/100)*5;
    }

}

测试代码
StrategyTest.php测试

<?php

/**
 * 策略模式
 * Class StrategyTest
 */

class StrategyTest extends \PHPUnit\Framework\TestCase
{

    public function testCustomer()
    {
        $price = 100;

        $vipCutomer = new \App\Creational\Strategy\VipCustomer();
        $normalCustomer = new \App\Creational\Strategy\NormalCustomer();

        $cashier = new \App\Creational\Strategy\Cashier();

        $cashier->setStrategy($vipCutomer);
        $this->assertEquals(95, $cashier->getMoney($price));

        $cashier->setStrategy($normalCustomer);
        $this->assertEquals(100, $cashier->getMoney($price));
    }
}

微信扫描二维码,关注个人订阅号,回复 "电子书" 获取各种技术书籍

相关文章
相关标签/搜索