得到一套依赖于深度嵌套的依赖关系图的类 [英] Getting a set of dependencies to a class deeply nested in the dependency graph
问题描述
因此,这里是我的问题:
So here is my problem:
例如,请考虑:
A 文件
有一组类
,以及进口
的
A 类
有一组实例方法
,<$ C的$ C>静态方法和变量
。
A 实例方法
的参数
和机身
。
机身
有... yadayada。
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, ...) { }
...
}
这是所有其他类别将不再需要,运行。我把这里的问题是,如何让这些依赖于机身
,而无需通过这一切依赖通过文件
,类
和 InstanceMethod
。
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
.
我可以创建一个 BodyFactory
,但问题仍然是一样的,我必须要通过 BodyFactory
到文件
,类
和 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?
推荐答案
让机身
实施 IBody
接口,让 InstanceMethod
仅仅依赖在 IBody
和的IEnumerable< IParameter>
(如果我们假设C# - 在Java中这将是一个不同的命名集合接口,但原则将是相同的)。
Let Body
implement an IBody
interface and let InstanceMethod
depend only on IBody
and IEnumerable<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:
File file = new File(
new List<IClass>
{
new Class(
new List<IInstanceMethod>
{
new InstanceMethod(
new List<IParameter>(),
new Body(
new Dependency1()
new Dependency2(),
new DependencyN()))
},
new List<IStaticMethod>(),
new List<IVariable>())
},
new List<IImport>());
这篇关于得到一套依赖于深度嵌套的依赖关系图的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!