php-策略模式实现

php-策略模式实现

概述

策略模式(Strategy Pattern):定义一系列算法,将每一个算法封装起来,并让它们可以相互替换。策略模式让算法独立于使用它的客户而变化,也称为政策模式(Policy)。

模式结构

  1. MotorcycleProduce-摩托组装接口:建立摩托组装标准工艺
  2. MotocycleProduct -摩托车产品本身
  3. MotorcycleScooterProduce-踏板摩托车组装者:负责踏板摩托车的组装工作
  4. MotorcycleScooterProduceNew-踏板摩托车组装者:负责踏板摩托车的组装工作
  5. MotorScooter-策略类,用于指定踏板摩托车生产工艺

图例

在这里插入图片描述

代码范例

<?php
/**
 * +----------------------------------------------------------------------+
 * php23种设计模式实现-策略模式
 * Author:微信公众号:yuantanphp
 * 获取更多资源,技术答疑,项目合作请关注微信公众号:yuantanphp
 * +----------------------------------------------------------------------+
 */
//抽象类-定义生产摩托车流水线标准
interface MotorcycleProduce
{
    //发动机方法
    public  function addEngine();
    //车身方法
    public  function addBody();
    //车轮方法
    public  function addWhell();
    //获取摩托
    public  function getMotor();
}
//摩托车产品本身
class MotocycleProduct{
    private $motor = [
        "engine"=>"",
        "body"=>"",
        "whell"=>"",
    ];
    //新增发动机零部件
    public function addEngine($engine){
        $this->motor["engine"] = $engine;
    }
    public function addBody($body){
        $this->motor["body"] = $body;
    }
    public function addWhell($whell){
        $this->motor["whell"] = $whell;
    }
    //获取完整摩托对象
    public function getMotor(){
        return $this->motor;
    }
}
/**
 * 踏板摩托组装
 */
class MotorcycleScooterProduce implements MotorcycleProduce
{
    public function __construct()
    {
        $this->motor = new MotocycleProduct();
    }
    public function addEngine()
    {
        $this->motor->addEngine("踏板摩托-发动机已装好");
    }
    public function addBody(){
        $this->motor->addBody("踏板摩托-车身已装好");
    }
    public function addWhell(){
        $this->motor->addWhell("踏板摩托-车轮已装好");
    }
    public function getMotor(){
        return $this->motor->getMotor();
    }
}
/**
 * 跨骑摩托组装
 */
class MotorcycleScooterProduceNew implements MotorcycleProduce
{
    public function __construct()
    {
        $this->motor = new MotocycleProduct();
    }
    public function addEngine()
    {
        $this->motor->addEngine("新工艺-踏板摩托-发动机已装好");
    }
    public function addBody(){
        $this->motor->addBody("新工艺-踏板摩托-车身已装好");
    }
    public function addWhell(){
        $this->motor->addWhell("新工艺-踏板摩托-车轮已装好");
    }
    public function getMotor(){
        return $this->motor->getMotor();
    }
}
class MotorScooter{
    private $strategy;
    function __construct(MotorcycleProduce $s){
        $this->strategy = $s;
    }
    function getMotor(){
        $this->strategy->addEngine();
        $this->strategy->addBody();
        $this->strategy->addWhell();
        return $this->strategy->getMotor();
    }
}
$motorcycleScooterProduce = new MotorcycleScooterProduce();
$motorScooter = new MotorScooter($motorcycleScooterProduce);
$motor = $motorScooter->getMotor();
var_dump($motor);
echo "<br>";
$motorcycleScooterProduceNew = new MotorcycleScooterProduceNew();
$motorScooter = new MotorScooter($motorcycleScooterProduceNew);
$motor = $motorScooter->getMotor();
var_dump($motor);

模式分析

  • 策略模式相对容易理解。就是准备一组实现方式,并将每个实现方式组装起来,使得它们可以转换

  • 策略模式由客户决定该使用的策略。策略模式实现对解决同一问题的多种实现方式的封装,至于使用何种实现策略应该有客户决定,以此可以提高系统的灵活性。但需要客户理解各种实现方式的异同。以便选择合适的实现方法。因此需要客户在使用前对各种方法有所了解。

优缺点

策略模式的优点

  • 策略模式提供了对“开闭原则”的完美支持,用户可以在不修改原有系统的基础上选择算法或行为,也可以灵活地增加新的算法或行为。
  • 避免使用多重条件IF语句。提高系统的灵活性
  • 替换继承关系的办法
  • 灵活管理多实现方式

策略模式的缺点

  • 客户端必须知道所有的策略类,并自行决定使用哪一个策略类。
  • 策略模式将造成产生很多策略类,可以通过使用享元模式在一定程度上减少对象的数量。

适用环境

在以下情况下可以使用策略模式:

  • 如果在一个系统里面有许多类,它们之间的区别仅在于它们的行为,那么使用策略模式可以动态地让一个对象在许多行为中选择一种行为。
  • 如果一个对象有很多的行为,如果不用恰当的模式,这些行为就只好使用多重的条件选择语句来实现。
  • 不希望客户端知道复杂的、与算法相关的数据结构,在具体策略类中封装算法和相关的数据结构,提高算法的保密性与安全性。
  • 需要动态的对一个模块选择实现方式
关注微信公众号(yuantanphp)
回复关键字 设计模式 可获取以下材料
《设计模式:可复用面向对象软件的基础》pdf版.
php设计模式23种实例实现php源码
在这里插入图片描述
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冯伪猿

觉得文章不错?打赏支持一下吧。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值