mardi 16 juillet 2019

Construction of complex object made up of other complex objects

I am working on an application which takes files generated by a cycling computer (fit protocol, .fit files) and extracts their contents to a database where they can be analysed later. I am using Python and use the fitparse library to extract the data inside the file and then map those to the db using sqlalchemy model classes.

There are a few classes but the main one is the Activity class which represents (funnily enough) an activity e.g. a training ride or race. An Activity has collections of other objects such as Records which represent the regular 1 second reading of sensor data e.g. speed, temperature, altitude, Lap data, or Events such as a distance alerts or goals achieved.

At the moment to construct an Activity object I parse the .fit file, extract each part I need, build the relevant model objects from the extracted data, and connect them all up to the Activity. When the Activity is finally constructed it goes into the db. Its a lot of steps made worse by the objects in its member collections also having a lot of steps to construct.

My question is how can I do this so it's more manageable? I initially opted for a builder pattern to separate out the construction of the Activity model object but with many steps involved the Builder is getting bigger and bigger. Do I break up the construction of each complex object into separate builders and have an Activity Builder that coordinates them into an Activity? Should a Builder call other Builders to construct its object? Do I need to layer a bit more and if so how? Really not sure.

Building in Python so not necessarily tied to a class based suggestion. Thank you.

Aucun commentaire:

Enregistrer un commentaire