Interviews4 answers
I have scenario that to use different data source at run time hence i just created common interface used Factory Pattern. But my challenge is, each datasource expect different parameters Lets say i have GteData() in the interface, For 1 st datasource, i would pass as GetData(int i) but for the second datasource it would be GetData(int i, string s) Having this, still factory pattern would be preferable or any other alternate solution?
4 Answers
use composite design pattern ..
create datasource base class(abstract) and inehrit diff concrete data source with all having own logic to getdata method with diff params.
now in main class will keep reference of base object and initialize this at runtime and which intern will invoke appropriate concrete method
Vijay SaxenaPosted Jan 10, 2020, 3:55 AM EST
Please use provider pattern and in your GetData method prepare a class and pass that
class object rather than passing multiple parameters.
Mohd AhmadPosted Jan 21, 2019, 1:23 PM EST
we can have optional parameters in the getdata definition and use it as needed.
CharuPosted Nov 28, 2018, 2:38 PM EST
Create a base parameter class; use it in your GetData method as parameter. and in your method call, just pass the actual implementation of parameter base class. Hope that solves your issue.
Pravin PravinPosted Feb 6, 2018, 5:36 PM EST