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

    通过理解,php策略模式就是通过不同实例化不同的类,从而实现不同的业务逻辑。

    <?php
    /**
     * php设计模式之策略模式
     */
    interface Strategy
    {
        public function computePrice($price);
    }
    
    /**
     * 普通会员
     */
    class CommonMember implements Strategy
    {
        public function computePrice($price)
        {
            return $price;
        }
    }
    
    /**
     * 中级会员
     */
    class MiddleMember implements Strategy
    {
        public function computePrice($price)
        {
            return $price*0.8;
        }
    }
    /**
     * 高级会员
     */
    class SupperMember implements Strategy
    {
        public function computePrice($price)
        {
            return $price*0.7;
        }
    }
    /**
     *
     */
    class Price
    {
        private $strategyInstance;
    
        public function __construct($instance)
        {
            $this->strategyInstance = $instance;
        }
    
        public function compute($price)
        {
            return $this->strategyInstance->computePrice($price);
        }
    }
    
    $p = new Price(new SupperMember());
    echo $price = $p->compute(100);
    If the copyright belongs to the longfei, please indicate the source!!!
  • 相关阅读:
    ## js 性能 (未完。。。)
    React 创建元素的几种方式
    Json 与 javascript 对象的区别
    js 基本数据类型
    第十三章 事件
    第十二章 DOM2和DOM3
    第十一章 DOM扩展
    第十章 DOM
    第八章 BOM
    第七章 函数表达式
  • 原文地址:https://www.cnblogs.com/longfeiPHP/p/5688131.html
Copyright © 2011-2022 走看看