建造模式 + 外观模式示例:实现扣除用户金币及写入用户明细

Posted 罗夏

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了建造模式 + 外观模式示例:实现扣除用户金币及写入用户明细相关的知识,希望对你有一定的参考价值。

实现功能:

扣除用户金币及写入金币明细,这是同一个行为,使用外观模式

金币明细需要多个参数来保证对象的完整,参数过多,所以使用建造模式

 

一.用户明细类

class MoneyDetail
{
    const   MONEY_PAY       = 1;    // 充值获得
    const   MONEY_EXCHANGE  = 2;    // 兑换

    private $type;
    private $quantity;
    private $desc;
    private $time;

    public function __construct(MoneyDetailBuilder $builder)
    {
        $this->type         = $builder->getType();
        $this->quantity     = $builder->getQuantity();
        $this->desc         = $builder->getDesc();
        $this->time         = $builder->getTime();
    }

    /**
     * 写入数据库
     */
    public function writeDb()
    {
        echo "[{$this->type}|{$this->quantity}|{$this->desc}|{$this->time}]数据已写入数据库";
    }
}

  

二.用户明细建造者类

class MoneyDetailBuilder
{
    private $type;
    private $quantity;
    private $desc;
    private $time;

    /**
     * @return mixed
     */
    public function getType()
    {
        return $this->type;
    }

    /**
     * @param $type
     * @return $this
     */
    public function setType($type)
    {
        $this->type = $type;
        return $this;
    }

    /**
     * @return mixed
     */
    public function getQuantity()
    {
        return $this->quantity;
    }

    /**
     * @param $quantity
     * @return $this
     */
    public function setQuantity($quantity)
    {
        $this->quantity = $quantity;
        return $this;
    }

    /**
     * @return mixed
     */
    public function getDesc()
    {
        return $this->desc;
    }

    /**
     * @param $desc
     * @return $this
     */
    public function setDesc($desc)
    {
        $this->desc = $desc;
        return $this;
    }

    /**
     * @return mixed
     */
    public function getTime()
    {
        return $this->time;
    }

    /**
     * @param $time
     * @return $this
     */
    public function setTime($time)
    {
        $this->time = $time;
        return $this;
    }

    public function moneyDetail()
    {
        return new MoneyDetail($this);
    }
}

  

三.用户类:

class User
{
    private $money;

    public function __construct()
    {
        $this->money = 500;
    }

    public function reduceMoney($quantity)
    {
        $this->money -= $quantity;
        echo ‘用户扣除金币成功‘;
    }

    public function getMoney()
    {
        return $this->money;
    }
}

  

四.扣除用户金币外观类

class UserFacade
{
    private $user;
    private $moneyDetailBuilder;

    public function __construct(User $user, MoneyDetailBuilder $moneyDetailBuilder)
    {
        $this->user                 = $user;
        $this->moneyDetailBuilder   = $moneyDetailBuilder;
    }

    public function reduceMoney($quantity)
    {
        $this->user->reduceMoney($quantity);
        $this->moneyDetailBuilder->setDesc($this->moneyDetailBuilder->getDesc() . "扣除{$quantity}金币;当前金币{$this->user->getMoney()}");
        $this->moneyDetailBuilder->moneyDetail()->writeDb();
    }
}

  

五.调用

// 建造金币明细类
$moneyDetailBuilder = new MoneyDetailBuilder();
$moneyDetailBuilder->setType(MoneyDetail::MONEY_EXCHANGE)
    ->setQuantity(100)
    ->setTime(time())
    ->setDesc(‘兑换奖品,‘);

// 扣除金币
$user = new User();
$userFacade = new UserFacade($user, $moneyDetailBuilder);
$userFacade->reduceMoney(100);

  

以上是关于建造模式 + 外观模式示例:实现扣除用户金币及写入用户明细的主要内容,如果未能解决你的问题,请参考以下文章

设计模式建造者模式 ( 简介 | 适用场景 | 优缺点 | 代码示例 )

外观模式/建造者模式

2015-03-12---外观模式,建造者模式(附代码),观察者模式(附代码),boost库应用

设计模式-外观模式

设计模式之——Builder建造模式

外观模式及php实现