• php设计模式 之观察者模式 与Laravel 的事件


    观察者模式主要用于解耦

    1 没有观察者模式

    class order
    {
        public  function addOrder()
        {
            // 发短信
            Message::update();
            //发邮件
            Email::update();
            //记日志
            Log::update();
        }
       
        
    }
    $order = new order();
    $order->addOrder();

    2 观察者模式

    2.1 被观察者 order

    //被观察者
    interface Observable
    {
        //添加观察者实例
        function add();
        //删除观察者实例
        function del();
        //通知观察者
        function notify();
    }
    
    //订单类继承被观察者接口
    class order implements Observable
    {
        private $instance = array();
        //添加观察者实例
        function add(observe $observe)
        {
            // TODO: Implement add() method.
            $key = array_search($observe,$this->instance);
            if ($key === false){
                $this->instance[] = $observe;
            }
        }
        //删除观察者实例
        function del(observe $observe)
        {
            // TODO: Implement del() method.
            $key = array_search($observe,$this->instance);
            if ($key !== false){
                unset($this->instance[$key]);
            }
        }
        //通知观察者实例
        function notify()
        {
            // TODO: Implement notify() method.
            foreach ($this->instance as $key => $val){
                //调用实例化对象的update方法
                $val->update();
            }
        }
    }

    2.2 观察者 Email 、Message

    /**
     * Interface observe
     * 定义一个观察者
     */
    interface observe()
    {
       //每个实例化对象都有update方法
       function update();
    }
    
    class Email implements observe
    {
        function update()
        {
            echo "订单修改了,发送邮件";
        }
    }
    class Message implements observe
    {
        function update()
        {
            echo "订单修改了,发送短信";
        }
    }

    2.3 客户端调用

    $order = new order();
    $order->add(new Email());
    $order->add(new Message());
    $order->del(new Email());
    $order->notify();

    3 Laravel的事件机制

    3.1 执行命令

    php artisan make:event EventTest
    php artisan make:listener EventTestListener

    3.2 生成文件

    appListenersEventTestListener.php

    <?php
    
    namespace AppListeners;
    
    use IlluminateQueueInteractsWithQueue;
    use IlluminateContractsQueueShouldQueue;
    
    class EventTestListener
    {
        /**
         * Create the event listener.
         *
         * @return void
         */
        public function __construct()
        {
            //
        }
    
        /**
         * Handle the event.
         *
         * @param  object  $event
         * @return void
         */
        public function handle($event)
        {
            //
            echo "我是监听者/观察者 EventTestListener ";
        }
    }

    appEventsEventTest.php

    <?php
    
    namespace AppEvents;
    
    use IlluminateBroadcastingChannel;
    use IlluminateQueueSerializesModels;
    use IlluminateBroadcastingPrivateChannel;
    use IlluminateBroadcastingPresenceChannel;
    use IlluminateFoundationEventsDispatchable;
    use IlluminateBroadcastingInteractsWithSockets;
    use IlluminateContractsBroadcastingShouldBroadcast;
    
    class EventTest
    {
        use Dispatchable, InteractsWithSockets, SerializesModels;
    
        /**
         * Create a new event instance.
         *
         * @return void
         */
        public function __construct()
        {
            //
        }
    
        /**
         * Get the channels the event should broadcast on.
         *
         * @return IlluminateBroadcastingChannel|array
         */
        public function broadcastOn()
        {
            //return new PrivateChannel('channel-name');
        }
    }

    outesweb.php

    use AppEventsEventTest;
    Route::get('/', function () {
        event(new EventTest());
    });
  • 相关阅读:
    Kubernetes实战:高可用集群的搭建和部署
    华为云MVP程云:知识化转型,最终要赋能一线
    支持60+数据传输链路,华为云DRS链路商用大盘点
    关于单元测试的那些事儿,Mockito 都能帮你解决
    深入原生冰山安全体系,详解华为云安全服务如何构筑全栈安全
    云小课|ModelArts Pro 视觉套件:零代码构建视觉AI应用
    FLINK重点原理与机制:内存(1)task之间的数据传输
    FLINK重点原理与机制:内存(2)网络流控及反压机制剖析(一)
    FLINK重点原理与机制:状态(3)两阶段提交
    FLINK重点原理与机制:状态(2)Flink的检查点算法CHECKPOINT
  • 原文地址:https://www.cnblogs.com/polax/p/14617122.html
Copyright © 2020-2023  润新知