如何在Laravel中实际使用Observer模式? [英] How to practically use the Observer pattern in laravel?

查看:237
本文介绍了如何在Laravel中实际使用Observer模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在学习设计模式,目前正在研究观察者模式.

So I am learning about design patterns and I am currently studying the Observer pattern.

要在原始基础上实现它,我做了这样的事情:

To implement it on a raw basis, I did something like this:

<?php

class Subject
{
    private $foo;
    private $bar;

    private $observers = [];

    public function addObserver(Observer $o)
    {
        $this->observers[] = $o;
    }

    public function removeObserver(Observer $o)
    {
        if(($key = array_search($o, $this->observers, $TRUE)) !== FALSE) {
            unset($this->observers[$key]);
        }
    }

    public function notify()
    {
        foreach ($this->observers as $observer) {
            $observer->update($this->foo, $this->bar);
        }
    }

    public function setFoo($foo)
    {
        $this->foo = $foo;
    }

    public function setBar($bar)
    {
        $this->bar = $bar;
    }
}

interface Observer{
    function update($foo, $bar);
}

class Observer1 implements Observer
{
    private $foo;
    private $bar;

    public function update($foo, $bar){
        $this->foo = $foo;
        $this->bar = $bar;
    }

    public function display()
    {
        echo "Observer1 : Foo -> " . $this->foo . " Bar -> " . $this->bar . "\n";
    }
}

class Observer2 implements Observer
{
    private $foo;
    private $bar;

    public function update($foo, $bar){
        $this->foo = $foo;
        $this->bar = $bar;
    }

    public function display()
    {
        echo "Observer2 : Foo -> " . $this->foo . " Bar -> " . $this->bar . "\n";
    }
}

$subject = new Subject;


$observer1 = new Observer1;
$observer2 = new Observer2;

$subject->addObserver($observer1);
$subject->addObserver($observer2);

$subject->setFoo(5);
$subject->setBar(10);
$subject->notify();

$observer1->display();
$observer2->display();

$subject->setFoo(20);
$subject->setBar(40);
$subject->notify();

$observer1->display();
$observer2->display();

所以我看到观察者正在更新.这是实现观察者模式的正确方法吗?我想我可以对Subject中的功能进行抽象,以添加,删除和通知其自己的抽象类.我还可以使用PHP提供的SplSubjectSplObserver.为了使这段代码更好,这里还可以做些什么?

So I see that the observers are getting updated. Is this the correct way to implement the observer pattern? I guess I could abstract the functionality in the Subject to add, remove and notify into its own abstract class. I could also user the SplSubject and SplObserver that PHP provides. What else can be done here to make this code better?

但是我真正想知道的是Laravel中使用的Observer模式.它必须在框架中的某处使用,我可以从中学习.其次,这有什么实际应用?我想到的一件事是,当我在laravel中进行缓存并且模型被更新时,它可能需要通知缓存对象进行更改.这是该模式的良好实用应用程序吗?还有什么?

But what I really want to know is where is the Observer pattern used in Laravel. It must be in use somewhere in the framework and I can learn from it. Secondly, what are the practical applications for this? One of the things that I could think of is when I am doing caching in laravel and a model gets updated, it might need to notify the cache objects to change. Is this a good practical application of the pattern? What others are there?

推荐答案

在我的项目中,我使用了Observers来检查模型是否已更改,如果发生更改则抛出事件.

In my project I used Observers to check if A model has changed and throw event if changes have been made.

假设我的用户模型有观察者,因此有UserObserver.观察者所做的是检查用户中的更改.如果有变化.

Let say I have Observer for my User Model hence UserObserver. What the observer does is it checks for changes in my user. And if there is changes.

我有一个事件处理程序来保存事务.

I have a event handler to save transactions.

例如,约翰·多伊(John Doe)将名字从迈克(Mike)改为约翰(John)"

名字是我的用户模型中名为first_name的字段;观察者还有很多,其中之一是我的申请.

Where first name is a field in my User Model named first_name; There is more to observers and one is my application.

这篇关于如何在Laravel中实际使用Observer模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆