Search code examples
phpdesign-patternslaravel-5observersmediator

Laravel 5 Event - Is this a mediator or observer?


I was discussing Laravel's event system with another developer today. He mentioned that Laravel's event dispatcher uses an observer pattern.

I always thought it implemented a mediator pattern as your objects always listen/fire events through an event dispatcher object, but the doc says it is an observer.

Event::listen('event.name', function ($foo, $bar) {
    //
});

Event::fire("event.name", []);

Isn't this a mediator pattern?


Solution

  • I have no precise idea about what Laravel's concretely do and actually if the doc says that it is built on an observer pattern, I would believe it.

    However your question is about what the code looks like and from my experience I can easily recognize an observer here with the analogy :

    • listen would be similar to Observer's subscribe/attach
    • fire would be similar to Observer's notify/update

    A mediator would seem weird to me here because even though the intent is to facilitate the communication between some objects, I can't see it as a good way to distribute notifications. I would definitely bet on an observer for that reason.