• 代理模式+简单工厂或代理模式+策略模式实现开通推广功能


    实现功能:

    1.用户金币或银两达到一定数额后,可以用相应数量的金币或银两开通推广功能,开通推广功能后,由此产生的收益归该用户所有

    2.推广类,不允许直接操作推广类,需要判断用户是否有金币或银两来开通,所以使用代理类.

    3.用户可以用银两也可以用金币来开通,所以使用策略模式或简单工厂模式;就系统目前而言就两种开通方式,比较固化,所以使用简单工厂模式来创建开通类会比较恰当.

    一.推广接口

    /**
     * 推广接口
     * Interface PromotionInterface
     */
    interface PromotionInterface
    {
        const LEVEL_FIRST   = 1;
        const LEVEL_SECOND  = 2;
        const LEVEL_THIRD   = 3;
        public function open($level);
    }
    

      

    二.银两开通推广类:

    class PromotionSilver implements PromotionInterface
    {
        const EXPEND = [
            PromotionInterface::LEVEL_FIRST   => 1000,
            PromotionInterface::LEVEL_SECOND  => 2000,
            PromotionInterface::LEVEL_THIRD   => 3000,
        ];
    
        public function open($level)
        {
            echo "已开通{$level}推广级别";
        }
    }
    

      

    三.金币开通推广

    /**
     * 金币开通推广
     * Class PromotionMoney
     */
    class PromotionMoney implements PromotionInterface
    {
        const EXPEND = [
            PromotionInterface::LEVEL_FIRST   => 100,
            PromotionInterface::LEVEL_SECOND  => 200,
            PromotionInterface::LEVEL_THIRD   => 300,
        ];
    
        public function open($level)
        {
            echo "已开通{$level}推广级别";
        }
    }
    

      

    四.用户类

    /**
     * Class User
     */
    class User
    {
        private $uid;
        private $money;     // 金币
        private $silver;    // 银两
    
        public function __construct($uid)
        {
            $this->uid      = $uid;
            $this->money    = 200;
            $this->silver   = 5000;
        }
    
        public function getMoney()
        {
            return $this->money;
        }
    
        /**
         * 获得用户拥有的银两
         * @return int
         */
        public function getSilver()
        {
            return $this->silver;
        }
    
        /**
         * 扣除用户金币
         * @param $money
         */
        public function reduceMoney($money)
        {
            $this->money -= $money;
        }
    
        /**
         * 扣除用户银两
         * @param $silver
         */
        public function reduceSilver($silver)
        {
            $this->silver -= $silver;
        }
    }
    

      

    五.代理类+策略模式:

    /**
     * Class PromotionProxy
     */
    class PromotionProxy implements PromotionInterface
    {
        private $user;
        private $promotion;
    
        public function __construct($uid)
        {
            $this->user = new User($uid);
        }
    
        public function setPromotion(PromotionInterface $promotion)
        {
            $this->promotion = $promotion;
        }
    
        public function open($level)
        {
            if ($this->promotion instanceof PromotionSilver) {
                $expend = PromotionSilver::EXPEND[$level];
                if ($this->user->getSilver() < $expend) {
                    echo '银两不足';
                    return false;
                }
                // 扣除银两
                $this->user->reduceSilver($expend);
            }
            if ($this->promotion instanceof PromotionMoney) {
                $expend = PromotionMoney::EXPEND[$level];
                if ($this->user->getMoney() < $expend) {
                    echo '金币不足';
                    return false;
                }
                // 扣除金币
                $this->user->reduceMoney($expend);
            }
    
            $this->promotion->open($level);
            echo $this->user->getMoney();
        }
    }
    

      

    调用:

    $client = new PromotionProxy($uid = 10);
    $client->setPromotion(new PromotionMoney());
    $client->open(PromotionInterface::LEVEL_FIRST);
    

      

    六.代理类+简单工厂模式

    /**
     * Class PromotionProxy
     */
    class PromotionProxy2 implements PromotionInterface
    {
        const OPEN_MONEY    = 'money';
        const OPEN_SILVER   = 'silver';
    
        private $user;
        private $promotion;
    
        public function __construct($uid, $type)
        {
            $this->user = new User($uid);
    
            switch ($type) {
                case self::OPEN_MONEY:
                    $this->promotion = new PromotionMoney();
                    break;
                case self::OPEN_SILVER:
                    $this->promotion = new PromotionSilver();
                    break;
                default:
                    throw new Exception('开通类别错误');
                    break;
            }
        }
    
        /**
         * @param $level
         * @return bool
         */
        public function open($level)
        {
            if ($this->promotion instanceof PromotionSilver) {
                if ($this->user->getSilver() < PromotionSilver::EXPEND[$level]) {
                    echo '银两不足';
                    return false;
                }
            }
    
            if ($this->promotion instanceof PromotionMoney) {
                if ($this->user->getMoney() < PromotionMoney::EXPEND[$level]) {
                    echo '金币不足';
                    return false;
                }
            }
    
            $this->promotion->open($level);
            return true;
        }
    }
    

      

    调用:

    $client = new PromotionProxy2(10, PromotionProxy2::OPEN_SILVER);
    $client->open(PromotionInterface::LEVEL_FIRST);
  • 相关阅读:
    rapidjson解析
    约瑟夫环
    [易学易懂系列|rustlang语言|零基础|快速入门|(29)|实战6:BDD工具cucumber_rust]
    [易学易懂系列|rustlang语言|零基础|快速入门|(28)|实战5:实现BTC价格转换工具]
    [易学易懂系列|rustlang语言|零基础|快速入门|(27)|实战4:从零实现BTC区块链]
    [易学易懂系列|rustlang语言|零基础|快速入门|(26)|实战3:Http服务器(多线程版本)]
    [易学易懂系列|rustlang语言|零基础|快速入门|(26)|实战3:Http服务器]
    [易学易懂系列|rustlang语言|零基础|快速入门|(25)|实战2:命令行工具minigrep(2)]
    [易学易懂系列|rustlang语言|零基础|快速入门|(24)|实战2:命令行工具minigrep(1)]
    [易学易懂系列|rustlang语言|零基础|快速入门|(23)|实战1:猜数字游戏]
  • 原文地址:https://www.cnblogs.com/itfenqing/p/8728099.html
Copyright © 2020-2023  润新知