为什么循环引用视为有害吗?有害

由网友(爱笑的智障)分享简介:为什么是一个坏设计为对象来指回指向第一个另一目的Why is it a bad design for an object to refer to another object that refers back to the first one?推荐答案类之间循环的依赖关系并不一定是有害的。事实上,在一些情况下,它...

为什么是一个坏设计为对象来指回指向第一个另一目的

Why is it a bad design for an object to refer to another object that refers back to the first one?

推荐答案

类之间循环的依赖关系并不一定是有害的。事实上,在一些情况下,它们是理想的。例如,如果你的应用程序处理的宠物和它们的主人,您所期待的宠物类有一个方法来获得宠物的主人和所有者类有一个返回的宠物列表的方法。肯定的是,这可以使存储器管理更加困难(在非GC'ed语言)。但是,如果圆是固有的问题,然后试图摆脱它很可能会导致更多的问题。

Circular dependencies between classes are not necessarily harmful. Indeed, in some cases they are desirable. For example, if your application dealt with pets and their owners, you would expect the Pet class to have a method to get the pet's owner, and the Owner class to have a method that returns the list of pets. Sure, this can make memory management more difficult (in a non-GC'ed language). But if the circularity is inherent in the problem, then trying to get rid of it is probably going to lead to more problems.

在另一方面,模块之间循环依赖都是有害的。它通常是指示不好深思熟虑的模块结构的,和/或不粘附在原始模块化。一般来说,code-基地不受控制的交叉依赖将很难理解和难度比一用一干净的,分层的模块化结构,以保持。无体面模块,它可以是更难predict是否变化的影响。这使得维护困难,并导致code衰变的构想拙劣修补产生的。

On the other hand, circular dependencies between modules are harmful. It is generally indicative of a poorly thought-out module structure, and/or failure to stick to the original modularization. In general, a code-base with uncontrolled cross-dependencies will be harder to understand and harder to maintain than one with a clean, layered module structure. Without decent modules, it can be much harder to predict whether the effects of a change. And that makes maintenance harder, and leads to "code decay" resulting from ill-conceived patching.

(此外,构建工具类似Maven不会处理模块(文物)循环依赖。)

(Also, build tools like Maven won't handle modules (artefacts) with circular dependencies.)

阅读全文

相关推荐

最新文章