Is Eventbus a Mediator or Observer Pattern?

前端 未结 5 1611
面向向阳花
面向向阳花 2021-01-01 19:11

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 descriptio

5条回答
  •  借酒劲吻你
    2021-01-01 19:45

    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.

提交回复
热议问题