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

实现功能:

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);

 

posted @ 2018-04-06 16:40  rorshach  阅读(352)  评论(0编辑  收藏  举报