samedi 5 septembre 2020

Passing references between classes

I am having a design Issue where I am trying to pass some references of classes to each other:

   self.positions = Positions(self.db, self.logger)
   self.instruments = Instruments(self.db, self.api, self.positions, self.logger)
   self.socket = Socket(self.api, self.task_manager, self.positions, self.instruments, self.logger)
  

db is database reference, logger is used for logging or print output to cli.

Now in Instruments I am passing positions because there is a function in positions that I need to call as callback.

In socket task_manager,positions,instruments are passed basically task_manager has a queue where I need to insert function reference from positions,instruments.

Problem is that now Positions now need a reference of socket which creates a cycle. I know this can be solved by having a separate class as reference holder. (But cyclic reference is considered as bad design)

I have seen various design pattern but not sure how to fix this. I am looking for some examples or some hint with my current design to improve on. Other than that how to improve current design as it require passing so many references in constructor.

Aucun commentaire:

Enregistrer un commentaire