mercredi 12 avril 2023

Good Patterns to pass down data in deep OOP composition?

This is a general open question about best practices and scalability in Python using OOP.

Overtime, I have been using in several projects Class inheritance and composition. This pattern has helped me abstracting and encapsulating a lot of the code. However, as the codebase increases, I have been trapped in a recurrent pattern where I have a "master" Class which "orchestrates" another classes by composition, which in turn also have another composed classes to orchestrate and so on. This creates a "deep" tree structure of dependant classes.

The problem is that if I want to pass down arguments to many of the Class components from the master class, I have to pass it down on everytime I init the class again and again, writing redundant code. For example (a simple example just to illustrate the kindof composition but imagine a much deeper structure than this):

 class UploaderExecutor:
     def __init__(self, mediatype: Literal['video', 'image'], channel: Literal['online', 'offline'], ...):
     self.uploader = Uploader(mediatype, channel, ...)
     self.validator = Validator(mediatype, channel, ...)
     self.reporter = Reporter(mediatype, ...)
     ...

 class Uploader():
     def __init__(self, mediatype, channel, ...):
         self.configurator = Configurator(mediatype, channel...)
         self.file_parser = FileParser(mediatype, channel...)
         self.Notificator = Notificator(mediaype, channel...)
         ...

 class Validator():
     def __init__(self, mediatype, channel, ...):
         self.file_validator = FileValidator(mediatype, channel...)
         self.name_validator = NameValidator(channel...)
         ...

 class Reporter():
     def __init__(self, mediatype, channel, ...):
         self.slack_reporter = SlackReporter(mediatype, channel...)
         self.telegram_reporter = TelegramReporter(mediatype, channel...)
         ...

   #etc etc

I guess this pattern of passing down arguments to all the composed classes is not escalable as more and more cases are added. Also I do not want to rely in solutions like global variables, because I want to decide which data passing down to which composed classes (maybe not all of them would need for example mediatype). So my question is which kind of abstractions or design patterns are recommended in these cases?

Aucun commentaire:

Enregistrer un commentaire