1. 程式人生 > >設計模式實例:中介模式實現打賞功能

設計模式實例:中介模式實現打賞功能

減少 blog tail attr HA 通知 struct media 模式

實現功能:

1.用戶A向用戶B打賞金幣

2.減少用戶A的金幣,並寫入金幣明細表

3.增加用戶B的金幣,並寫入金幣明細表

4.給用戶B發送一個打賞通知

ps:本文中的同事,並非是指該類為同事,而是沿用中介者模式中的稱呼.與中介者打交道的各個類.

一.虛擬同事類:

/**
 * Class Colleague
 */
abstract class Colleague
{
    protected $mediator;

    public function setMediator(Mediator $mediator)
    {
        $this->mediator = $mediator;
    }
}

  

二.虛擬用戶類,繼承虛擬同事類

/**
 * Class User
 */
abstract class User extends Colleague
{
    protected $uid;
    protected $money;

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

  

三.創建兩個同事類:付款方和收款方

1.付款方

class UserPayer extends User
{
    public function changeMoney($money)
    {
        $this->money -= $money;

        // 寫入明細
        $this->mediator->writeMoneyDetail([
            ‘uid‘   => $this->uid,
            ‘money‘ => $money,
            ‘rule‘  => ‘-‘
        ]);

        // 付款給對方
        $this->mediator->payee($money);
    }

    public function getUid()
    {
        return $this->uid;
    }
}

  

2.收款方

class UserPayee extends User
{
    public function changeMoney($money)
    {
        $this->money += $money;

        // 寫入明細
        $this->mediator->writeMoneyDetail([
            ‘uid‘   => $this->uid,
            ‘money‘ => $money,
            ‘rule‘  => ‘+‘
        ]);

        $attributes = [
            ‘to_uid‘    => $this->uid,
            ‘from_uid‘  => $this->mediator->getPayerUid(),
            ‘money‘     => $money
        ];
        // 增加通知
        $this->mediator->notify($attributes);
    }
}

  

四.繼續創建同事類:通知類

/**
 * 通知類
 * Class Notify
 */
class Notify extends Colleague
{
    public function writeNotify(array $attribute)
    {
        echo "用戶{$attribute[‘from_uid‘]}向用戶{$attribute[‘to_uid‘]}打賞{$attribute[‘money‘]}個金幣<br>";
    }
}

  

五.繼續創建同事類:金幣明細

/**
 * 金幣明細
 * Class MoneyDetial
 */
class MoneyDetial extends Colleague
{
    public function writeToDb(array $attribute)
    {
        echo "UID為{$attribute[‘uid‘]}的用戶金幣{$attribute[‘rule‘]} {$attribute[‘money‘]}<br>";
    }
}

  

六.中介者類:

/**
 * 中介者類
 * Class Mediator
 */
class Mediator
{
    private $userPayer;
    private $userPayee;
    private $notify;
    private $moneyDetial;

    public function __construct(UserPayer $userPayer,
                                UserPayee $userPayee,
                                Notify $notify,
                                MoneyDetial $moneyDetial)
    {
        $this->userPayer    = $userPayer;
        $this->userPayee    = $userPayee;
        $this->notify       = $notify;
        $this->moneyDetial  = $moneyDetial;
    }

    /**
     * 寫入明細
     * @param array $arributes
     */
    public function writeMoneyDetail(array $arributes)
    {
        $this->moneyDetial->writeToDb($arributes);
    }

    /**
     * 收款
     * @param $money
     */
    public function payee($money)
    {
        $this->userPayee->changeMoney($money);
    }

    /**
     * 寫入通知
     * @param array $attribute
     */
    public function notify(array $attribute)
    {
        $this->notify->writeNotify($attribute);
    }

    /**
     * 獲得付款方UID
     * @return mixed
     */
    public function getPayerUid()
    {
        return $this->userPayer->getUid();
    }
}

  

七.具體調用

// 創建各個同事類
$userPayer      = new UserPayer(1, 100);
$userPayee      = new UserPayee(2, 200);
$notify         = new Notify();
$moneyDetial    = new MoneyDetial();

// 創建中介者
$mediator = new Mediator($userPayer, $userPayee, $notify, $moneyDetial);

// 為每個同時類設置中介者
$userPayer->setMediator($mediator);
$userPayee->setMediator($mediator);
$notify->setMediator($mediator);
$moneyDetial->setMediator($mediator);

// 打賞
$userPayer->changeMoney(10);

  

八.輸出結果:

UID為1的用戶金幣- 10
UID為2的用戶金幣+ 10
用戶1向用戶2打賞10個金幣

設計模式實例:中介模式實現打賞功能