jeudi 30 mars 2017

Dependency injection in object hierarchy

I've a class diagram like this:

       TopClass
       ◆      ◆
       |      |
  SubClass1   SubClass2
  ◆       ◆
  |       |
  |       ChildClass2
  |
  ChildClass1

TopClass has SubClass1 and SubClass2. Subclass1 has ChildClass1 and ChildClass2.

I use dependency injection on each of these classes. So when ChildClass1 needs interface X I need to pass X thru TopClass and Subclass1. When ChildClass2 needs Y I also need to pass it thru whole hierarchy. Eventually I end up with TopClass full of dependencies to interfaces it actually doesn't use. I find it smelly.

I've found an article about facade pattern which seemd promising but eventually I cannot find a way to use it in my situation.

Any other idea? And I wouldn't like to use singleton or any similar solution as I need to be able to unit tests these classes easily.

Aucun commentaire:

Enregistrer un commentaire