zoukankan      html  css  js  c++  java
  • PHP 设计模式之策略模式

    简单描述

    策略模式(Strategy Pattern)作为一种软件设计模式,指对象有某个行为,但是在不同的场景中,该行为有不同的实现算法。
    比如每个人都要“交个人所得税”,但是“在美国交个人所得税”和“在中国交个人所得税”就有不同的算税方法。

    策略模式是一种对象行为型模式。

    模式结构

    策略模式包含如下角色:

    • Context: 环境类
    • Strategy: 抽象策略类
    • ConcreteStrategy: 具体策略类

    模式动机

    • 完成一项任务,往往可以有多种不同的方式,每一种方式称为一个策略,我们可以根据环境或者条件的不同选择不同的策略来完成该项任务。
    • 在软件开发中也常常遇到类似的情况,实现某一个功能有多个途径,此时可以使用一种设计模式来使得系统可以灵活地选择解决途径,也能够方便地增加新的解决途径。
    • 在软件系统中,有许多算法可以实现某一功能,如查找、排序等,一种常用的方法是硬编码(Hard Coding)在一个类中,如需要提供多种查找算法,可以将这些算法写到一个类中,在该类中提供多个方法,每一个方法对应一个具体的查找算法;当然也可以将这些查找算法封装在一个统一的方法中,通过if…else…等条件判断语句来进行选择。这两种实现方法我们都可以称之为硬编码,如果需要增加一种新的查找算法,需要修改封装算法类的源代码;更换查找算法,也需要修改客户端调用代码。在这个算法类中封装了大量查找算法,该类代码将较复杂,维护较为困难。
    • 除了提供专门的查找算法类之外,还可以在客户端程序中直接包含算法代码,这种做法更不可取,将导致客户端程序庞大而且难以维护,如果存在大量可供选择的算法时问题将变得更加严重。
    • 为了解决这些问题,可以定义一些独立的类来封装不同的算法,每一个类封装一个具体的算法,在这里,每一个封装算法的类我们都可以称之为策略(Strategy),为了保证这些策略的一致性,一般会用一个抽象的策略类来做算法的定义,而具体每种算法则对应于一个具体策略类。

    使用场景

    订单系统中不同类型的客户有不同的结算方式;
    不同商家的打折优惠模式不同;

    优点

    替换继承关系,避免使用多重条件转移语句,扩展性良好

    缺点

    客户端必须知道所有策略类,并自行决定使用哪一种策略类。
    如果算法较多,则会造成很多的策略类。

    实例

    <?php
    // 策略接口
    interface IStrategy{
        public function algorithMethod();
    }
    
    // 具体策略实现
    class ConcreteStrategy implements IStrategy{
        public function algorithMethod(){
            echo "this is ConcreteStrategy method...<br>";
        }
    }
    
    class ConcreteStrategy2 implements IStrategy{
        public function algorithMethod(){
            echo "this is ConcreteStrategy2 method...<br>";
        }
    }
    
    class ConcreteStrategy3 implements IStrategy{
        public function algorithMethod(){
            echo "this is ConcreteStrategy3 method...<br>";
        }
    }
    
    // 策略上下文
    class StrategyContext{
        public $strategy = null;
        // 使用构造器注入具体的策略类
        public function __construct(IStrategy $strategy){
            $this->strategy = $strategy;
        }
    
        public function contextMethod(){
            // 调用策略实现的方法
            $this->strategy->algorithMethod();
        }
    }
    
    // 客户端调用
    // 1. 创建具体策略实现
    $strategy = new ConcreteStrategy2();
    // 2. 创建策略上下文的同时,将具体的策略实现对象注入到策略上下文中
    $ctx = new StrategyContext($strategy);
    // 3. 调用上下文对象的方法来完成对具体策略实现的回调
    $ctx->contextMethod();

    输出:
    this is ConcreteStrategy2 method…

    示例 2. 不同用户调用不同的优惠

    <?php
    /*  需求:
    *   商场根据不同的客户进行打折
    *   新用户 9折
    *   老用户 8折
    *   VIP 会员 5折
    */ 
    
    // 策略接口
    interface IStrategy{
        public function get_price();
    }
    
    class NewUserStrategy implements IStrategy{
        public function get_price(){
            // 新用户处理逻辑
            return '新用户价格';
        }
    }
    
    class OldUserStrategy implements IStrategy{
        public function get_price(){
            // 老用户处理逻辑
            return '老用户价格';
        }
    }
    
    class VIPUserStrategy implements IStrategy{
        public function get_price(){
            // VIP用户处理逻辑
            return 'VIP用户价格';
        }
    }
    // 新增加 MVP 用户
    class MVPUserStrategy implements IStrategy{
        public function get_price(){
            // VIP用户处理逻辑
            return 'MVP用户价格';
        }
    }
    
    // 策略上下文
    class StrategyContext{
    
        private $user_strategy = null;
    
        public function __construct(IStrategy $user_strategy){
            $this->user_strategy = $user_strategy;
        }
    
        public function get_price(){
            echo $this->user_strategy->get_price();
        }
    
    }
    
    // 客户端调用
    $user = 'MVP用户';
    if ($user == '新用户'){
        $user_strategy = new NewUserStrategy();
    }
    else if ($user == '老用户'){
        $user_strategy = new OldUserStrategy();
    }
    else if ($user == 'VIP用户'){
        $user_strategy = new VIPUserStrategy();
    }
    else if ($user == 'MVP用户'){
        $user_strategy = new MVPUserStrategy();
    }
    
    $strategy = new StrategyContext($user_strategy);
    $strategy->get_price();
    
    
    // 优点:增加新的用户类型时,不需要修改原有的类,只需要增加一个 MVPUserStrategy 类,然后在客户端调用即可,符合设计模式的开闭原则

    原文链接:https://www.ryanzoe.top/%e8%ae%be%e8%ae%a1%e6%a8%a1%e5%bc%8f/php-strategy-mode/

  • 相关阅读:
    下拉选择框,允许手动输入和过滤
    MVC数据绑定
    一个页面多个ng-app注意事项
    modal 多层弹窗 Maximum call stack size exceeded 解决方法
    VS10x CodeMap 注册码(key):
    VS2015卸载再安装
    VS2015无法创建工程
    解决VS2015版本key required问题手动方案
    猪猪公寓—事后诸葛亮
    猪猪公寓——测试总结
  • 原文地址:https://www.cnblogs.com/ryanzheng/p/12784365.html
Copyright © 2011-2022 走看看