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!!!
  • 相关阅读:
    BZOJ1556 墓地秘密
    [NOI2006]网络收费
    UVA11401 Triangle Counting
    UVA11538 Chess Queen
    BZOJ2560 串珠子
    BZOJ4057 [Cerc2012]Kingdoms
    [HNOI2012] 集合选数
    [Haoi2016]字符合并
    [Snoi2013]Quare
    洛谷平衡树模板总结
  • 原文地址:https://www.cnblogs.com/longfeiPHP/p/5688131.html
Copyright © 2011-2022 走看看