So here is my problem:
For instance, consider that:
A File
has a set of Classes
, as well as Imports
.
A Class
has a set of Instance Methods
, Static Methods
and Variables
.
A Instance Method
has Parameters
and a Body
.
The Body
has ... yadayada.
The problem, when modelling this in a OO way is that Body
may need a lot of specific dependencies to function:
class Body {
...
public Body(Dependency1, Dependency2, Dependency_n, ...) { }
...
}
that all the other classes won't need, to run. The question I'm putting here is on how to get those dependencies to Body
without having to pass all this dependencies through File
, Class
and InstanceMethod
.
I could create a BodyFactory
, but the problem would still be the same, as I'd have to pass the BodyFactory
through File
, Class
and InstanceMethod
, if I'm not missing anything.
How to solve this issue without resorting to singletons?
As far as Java is concerned, I don't think you have a workaround here, if a method takes n parameters, then you are going to have to pass them, so you are going to have to carry them all the way, a solution to make this more elegant is to create a DataWrapper, call it BodyBean and pass it also all the way down.
Then you have the other option don't use parameters, then if these guys are singletons, just inject them, which by the way is the option I would recommend you
It seems that you are dreaming about kind of injection framework. For example Spring Framework.
Let
Body
implement anIBody
interface and letInstanceMethod
depend only onIBody
andIEnumerable<IParameter>
(if we assume C# - in Java it would be a differently named collection interface, but the principle would be the same).Repeat this process of refactoring to Facade Services all the way to the Composition Root, where you can then compose the entire object graph like this: