EventBus/PubSub vs (reactive extensions) RX with respect to code clarity in a single threaded application

前端 未结 4 1519
有刺的猬
有刺的猬 2020-12-22 22:42

Currently, I am using an EventBus/PubSub architecture/pattern with Scala (and JavaFX) to implement a simple note organizing app (sort of like an Evernote client with some ad

4条回答
  •  遥遥无期
    2020-12-22 23:12

    I learned one or two things since I asked this question 2 years ago, here is my current understanding (as explained in Stephen's FRP book):

    The both try to help to describe a state machine, i.e. describe how the state of the program is changing in response to events.

    The key difference between EventBus and FRP is compositionality:

    • What is compositional ?

      • Functional programming is compositional. We can all agree on that. We can take any pure function, combine it with other pure functions and what we get is a more complicated pure function.
      • Compositionality means that when you declare something then at the site of the declaration all behaviour of the declared entity is defined.
    • FRP is a compositional way of describing a state machine, and event-bus is not. Why ?

      • It describes a state machine.
      • It is compositional because the description is done by using pure functions and immutable values.
    • EventBus is not a compositional way of describing a state machine. Why not ?

      • You cannot take any two event busses and compose them in a way that gives a new, composed event bus describing the composed state machine. Why not ?
        • Event busses are not first class citizens (as opposed to FRP's Event/Stream) .
          • What happens if you try to make event busses first class citizens ?
            • Then you get something resembling FRP/RX.
        • State influenced by the event busses are not
          • first class citizens (i.e. referentially transparent, pure values in contrast to FRP's Behaviour/Cell)
          • tied to the event busses in a declarative/functional way, instead state is modified imperatively triggered by event handling

    In summary, EventBus is not compositional, because the meaning and behaviour of a composed EventBus (i.e. the time evolution of the state that is influenced by said composed EventBus) depends on time (i.e the state of those parts of the software which are not included explicitly in the declaration of the composed EventBus). In other words, if I would try to declare a composed EventBus then it would not be possible to determine (just by looking at the declaration of the composed EventBus) what rules govern the state evolution of those states that are influenced by the composed EventBus, this is in contrast to FRP, where this can be done.)

提交回复
热议问题