Event bus / PubSub

Full decoupling between publishers and subscribers.
Publishers don’t know about subscribers, and vice-versa.
Messages are sender to receiver. The receiver cannot “reply” to the sender.

Example, wxpython provides a nice example of pubsub model

  1. import wx
  2. from wx.lib.pubsub import Publisher
  3. class Model:
  4. def __init__(self):
  5. self.myMoney = 0
  6. def addMoney(self, value):
  7. self.myMoney += value
  8. Publisher.sendMessage("MONEY CHANGED", self.myMoney)

Objects that are interested in the notification can now subscribe to the
qualified notification as follows. The
handler will receive a message, qualified with the appropriate information

  1. class Controller:
  2. def __init__(self, app):
  3. # <...>
  4. pub.subscribe(self.moneyChangedHandler, "MONEY CHANGED")
  5. def moneyChangedHandler(self, message):
  6. """
  7. This method is the handler for "MONEY CHANGED" messages,
  8. which pubsub will call as messages are sent from the model.
  9. We already know the topic is "MONEY CHANGED", but if we
  10. didn't, message.topic would tell us.
  11. """
  12. self.view.setMoney(message.data)

NSNotificationCenter is a pubsub.

  • decoupling makes compile time checks useless.
  • delivery network can become complicated. Incorrect setup can lead to unintended listeners to receive
    messages they are not supposed to receive.
    the application flow is hard to understand and debug.
  • from the code alone, it’s hard to spot the dependency between a publisher and a subscriber,
    in particularly when the message is emitted and delivered.
  • delivery can be synchronous or asynchronous, but even when synchronous, it’s not possible
    to rely on delivery order.
  • message source may not be available to the receiver.

use of topics to group message types.