zoukankan      html  css  js  c++  java
  • 设计模式实例:中介模式实现打赏功能

    实现功能:

    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个金币
  • 相关阅读:
    Chapter 03Using SingleRow Functions to Customize Output(03)
    Chapter 03Using SingleRow Functions to Customize Output(01)
    Chapter 04Using Conversion Functions and Conditional ExpressionsNesting Functions
    Chapter 04Using Conversion Functions and Conditional ExpressionsGeneral Functions
    Chapter 11Creating Other Schema Objects Index
    传奇程序员John Carmack 访谈实录 (zz.is2120)
    保持简单纪念丹尼斯里奇(Dennis Ritchie) (zz.is2120.BG57IV3)
    王江民:传奇一生 (zz.is2120)
    2011台湾游日月潭
    2011台湾游星云大师的佛光寺
  • 原文地址:https://www.cnblogs.com/itfenqing/p/8728011.html
Copyright © 2011-2022 走看看