0

First of all, not sure about the title of the topic here. Sorry if it doesn't really corresponds to what I'm asking.

I'm in a situation where I have some ideas but all I don't know if one of them is better then another and maybe I'm missing something. Here is the thing :

Currently I have one instance A which creates an instance of another class B, and use callbacks from instance B (via a callback class which is implemented by class A), and use this callback to notify another instance of a class C, also created inside of class A and private. Here is a summary diagram :

enter image description here

Now, in the future, for some reason, it is another object, of class D, which needs to create and keep the object of class B. The issue is that the object C still need to be notified when I have callbacks from B object. And of course, the C instance cannot move from A to D. So, from D, I haven't A instance nor B instance, and I don't know the best way of doing it. Here is another diagram with the D class :

enter image description here

This is basically the same as the first diagram, except that the last call (notifySomething()) is hypothetical since I don't have access to the C instance from the D class.

Does someone ever faced this situation and what is the best way of resolving it ?

Thanks !

etienne31
  • 27
  • 8
  • 1
    What is the big difference between A and D ? Because you could simply make D extends of A and change the `registerListener` and `notifySomething` function to `protected`. This way, D could also access the variable and method from A, to notify C. I might not have understand correctly here but that's what I would do. – Nicolas Mar 21 '20 at 13:30
  • Why not use an object in between? An object that manage communication with B&C and acts as a delegate for/to A|D... – Jean-Baptiste Yunès Mar 21 '20 at 14:34
  • @Nicolas, A and D have completely different tasks, hence it wouldn't be logical that D extends A – etienne31 Mar 24 '20 at 15:30
  • @Jean-BaptisteYunès, I thought about that, but I don't really know how to implement it in my case (I tried to be the most general in my question but the project is actually very specific. Thanks though) – etienne31 Mar 24 '20 at 15:34
  • I think jaco0646's answer is the best one in this context. – Jean-Baptiste Yunès Mar 24 '20 at 16:02

1 Answers1

1

A and D have one job: to execute business logic (when their callback methods are invoked).

With that in mind, A and D are trying to do three unrelated tasks that don't belong to them. Something else (perhaps a DI container) needs to take responsibility for these tasks.

  1. Dependency management: A and D should not care how their dependencies are instantiated. Something else should be responsible for instantiating B. Creation is not the job of A nor D.
  2. Listener registration: A and D should not care who is invoking their callback methods. Something else should be responsible for registering A and D to B. Registration is not the job of A nor D.
  3. Listener notification: A and D should not care who else needs notification from B. Something else should be responsible for registering C to B. Notification is not the job of A nor D.

One solution...

Separate all of the creation and dependency management and registration away from your other objects and put that logic in its own unique place. That place is called the composition root of your application, and none of your other objects should try to do the job of the composition root.

The goal is to achieve dependency inversion.

jaco0646
  • 15,303
  • 7
  • 59
  • 83