Design pattern to partially fill a object across multiple stages

Viewed 62

I have an object called Fund that has many attributes that needs to be populated with data. I also have many DataSource classes that I will be calling into to populate values into the Fund object. Is there a design pattern to solve the above case where I keep passing partially populated Fund object across multiple data sources that will populate attributes in the Fund object. I am looking to implement it in Java.

  1. Additionally is it good practice to pass the partially built Fund object across multiple classes.
  2. Also how can I ensure loose coupling between the Fund object and multiple datasources.
1 Answers

we have many options to design such a system for example you can use the decorator pattern and make the base class to load the basic attributes then add decorators to attach extra functionality to this class and load the remaining attributes each one in a separate phase. the class diagram can be like this enter image description here

in decorators, each decorator method will return the object after load extra attributes to be passed to the second decorator. the client code can be

       IFund _myFundObj=new Fund();
        _myFundObj.LoadData();

        FundDataLoader _phase1Loader=new Phase1();
        _myFundObj=_phase1Loader.LoadData(_myFundObj);

        FundDataLoader _phase2Loader=new Phase1();
        _myFundObj=_phase2Loader.LoadData(_myFundObj);

        FundDataLoader _phase3Loader=new Phase1();
        _myFundObj=_phase3Loader.LoadData(_myFundObj);

to design a multi-data source it can be designed as a factory as the below class diagram enter image description here

Related