Data Pull vs. Push OOP Approach

后端 未结 12 744
旧时难觅i
旧时难觅i 2021-01-30 02:41

When I design my system from scratch, I often face a dilemma whether my object should push information into another objects OR whether the objects should

12条回答
  •  伪装坚强ぢ
    2021-01-30 03:13

    As stated by other answers, neither push nor pull is better, but rather you should pick the one that best fits your design needs.

    From here discussing whether an observer model should be push or pull based:

    Who triggers the update?

    The communication between the subject and its observers is done through the notify method declared in observer interface. But it can be triggered from either subject or observer object. Usually the notify method is triggered by the subject when it's state is changed. But sometimes when the updates are frequent the consecutive changes in the subject will determine many unnecessary refresh operations in the observer. In order to make this process more efficient the observer can be made responsible for starting the notify operation when it consider necessary.

    For this pattern the determining characteristic is the frequency at which the data changes and then the corresponding rate at which the observers wish to receive that data. If the observers want the data at a slower rate than the subject generates the data (an example would be GPS on a phone, you don't need your position all the time, only when you have a specific use for it) then polling is more efficient. If the observers want the data as fast as the subject can produce it (a possible example would be a real-time stock ticker application), then push notifications are probably better.

提交回复
热议问题