Data Pull vs. Push OOP Approach

后端 未结 12 747
旧时难觅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 02:59

    It should not be any different in OOP (there may be something i have missed) but a pull approach is better.

    The reason I say this is because of the recent trends in design patterns. Domain Driven Design and CQRS being some of the very prominent, they promote loose coupling which is a very good thing.

    A object shouldn't care what another object does with it's data, it is not it's responsibility so to speak. The object should only make the data available and then the ones needing the data should fetch/pull it from that object. Look at event driven design.

    It makes the object independent of the other objects and makes it more portable (don't have to change where it pushes to, since it will get pulled from).

    TL;DR I would recommend pull'ing over pushing.

    NOTE: all these different design patterns dosen't exclude each other, but coexist.

提交回复
热议问题