Data Pull vs. Push OOP Approach

后端 未结 12 751
旧时难觅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条回答
  •  -上瘾入骨i
    2021-01-30 03:06

    destination.Push(source)

    • Destination knows what and how to get data from Source
    • Destination must depend on Source, or
    • else Source must implement a provided ISource interface and depend on the provider (might be the Destination package)
    • The method has direct private access to Destination.

    source.Pull(destination)

    • Source knows what and how to put into destination
    • Source must depend on Destination, or
    • else Destination must implement a provided IDestination interface and depend on the provider (might be Source package)
    • The method has direct private access to Source.

    Select your solution by looking at the dependencies you want to have in your code.

    If Source and Destination cannot depend on what's needed for the previous scheme, then you need the action to be performed by an external method that knows (depends on) both Source and Destination. It only has public access to both though.

    If you need any virtual ISource to feed any IDestination then you need those interfaces to expose all the methods needed for an external method to perform the action.

    If a single external method cannot perform the action on any ISource and any IDestination, then you might want to look at the Visitor pattern, the Visitor class performing all the particular actions on the specific Source1 and SourceX Destination1 and DestinationY.

提交回复
热议问题