1. 程式人生 > >建造模式 + 外觀模式示例:實現扣除用戶金幣及寫入用戶明細

建造模式 + 外觀模式示例:實現扣除用戶金幣及寫入用戶明細

多個參數 hang get cti log csharp pri OS arp

實現功能:

扣除用戶金幣及寫入金幣明細,這是同一個行為,使用外觀模式

金幣明細需要多個參數來保證對象的完整,參數過多,所以使用建造模式

一.用戶明細類

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

  

建造模式 + 外觀模式示例:實現扣除用戶金幣及寫入用戶明細