Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Eventbus a Mediator or Observer Pattern?

Is Eventbus more a Mediator or an Observer? According to Google, "eventbus mediator" gets 2.430 hits and "eventbus observer" gets 3.850 hits.

From the description, they would both match what I was trying to do (the mediator even a little more). So does eventbus implement a specific pattern or is it up to me which I say it is?

like image 914
Stefan Avatar asked May 12 '11 08:05

Stefan


4 Answers

Often, a given piece of code isn't intrinsically an example of one pattern or another. This is why they're called "patterns" (rather than, say, "implementation techniques"). A lot of software kinda looks like one pattern, but also resembles another -- this is good. It's best not to adhere to patterns for patterns' sake, but to use them as a shared vocabulary for discussing architecture.

EventBus is one such tool. I wrote it with Observer-like situations in mind, but if you structure your application appropriately it can play a Mediator-like role.

like image 175
Cliff L. Biffle Avatar answered Nov 10 '22 17:11

Cliff L. Biffle


The general usage of EventBus is to fire events. Use of the word Observer is better fit for that. Observer pattern uses events or messages to notify of a change to objects of interest about the object being observed(changed). Mediator also tries to de-couple the two implementations but is more concrete than Observer in the sense, it can know all about the two objects/interfaces and works as a glue to make those two work. Observer doesn't claim to know about the internals or even the interface. All it knows or cares about is when the event occurs, it needs to notify the objects who are interested.

Mediator could be a scenario specific setup whereas the Observer could be more generic.

EventBus being almost always a singleton within the scope of the application, I would definitely categorise EventBus as using Observer as its real intent in most cases is to facilitate messaging globally amongst the various modules/objects within your runtime.

like image 40
skipy Avatar answered Nov 10 '22 18:11

skipy


wikipedia: The essence of the Mediator Pattern is to "Define an object that encapsulates how a set of objects interact"

EventBus does not do this.

EventBus is also not observer pattern because if you have N objects and you want to communicate between all of them you need N*N observers if you use the observer pattern but only one global EventBus is enough to do the same job.

So EventBus is THE EventBus pattern.

like image 6
jhegedus Avatar answered Nov 10 '22 19:11

jhegedus


I'd say that a typical event bus utilises both of these patterns:

  • event bus essentially encapsulates how other objects communicate, so it is a mediator
  • objects that register as event handlers/listeners are observers (and the subjects of their observations are event types and/or objects that produce these events, not the bus itself), so as wikipedia says observer pattern "is mainly used to implement distributed event handling systems" (emphasis mine), but an event bus is not an observer per se.
like image 6
morgwai Avatar answered Nov 10 '22 19:11

morgwai