Is circular addiction good or bad

I need to know why we need to avoid circular dependencies? In the real world, if we think circular dependencies are pretty important. How does one friend need something from another friend, and the other needs something from this friend, why is his kind of circular law?

Then why is circular addiction a bad design? If we really need to avoid this, then what is the best possible construction in an object-oriented world for such a situation?

+5
source share
2 answers

The circular dependency problem is more like a chicken and egg problem.

If you depend on me, you are setting something up, and I rely on you setting something up, how do we get started?

The consequence of this is how we will end - if I have a link to your resource, and you have a link to mine, I can never clear it, because it will break you, and you cannot clear it, because it will break me.

The answer in both cases is to introduce an intermediary, transferring dependence on one of the parties to it. Therefore, if you transfer your resource to an intermediary, you will depend on me and the intermediary, and I will depend on the intermediary. That way you can clean up because now you have no resources, and I can clean up because no one is dependent on me, and then the intermediary can clean up.

+7
source

You need to understand that circular dependency implies that you can only use the corresponding circular dependent classes together: if you have a circular dependence between, say, A and B , you cannot use A or B independently in any program. To stick to your problem: of course, you don't need two other friends! All you need is some way to refer to some and interact with them in a way that may be limited by their actual abilities.

However, it is often possible that class objects use each other for no reason for circular dependency. To this end, it is important to determine what actually causes the dependency between the two classes / components (they are not quite equivalent, but providing a detailed definition will be somewhat long). A depends on B under these conditions:

  • When A contains a member of type B
  • When A comes from type B
  • When A uses a value of type B as part of the function signature.
  • When A uses B in its implementation.
  • I probably forgot something (I remember there were more reasons why the classes would be connected).

If you have a circular dependency between two classes, there may be ways to break this dependency. Often, a dependency can be broken down by splitting one of the two classes into a base class and a derived class with a base class independent of the other class. There are a number of other approaches to breaking dependency cycles. John Lakos "Large Scale C ++" (1996) is essentially all about interdependence cycles and the motivation for why cyclic dependencies are bad (I think he would not agree with this simplifying characteristic).

... and, yes, cyclic dependencies are bad:

  • They force programs to include unnecessary functions, because things are dragged and dropped, which are not needed.
  • They make software testing difficult.
  • They are much more difficult to talk about software.
  • They greatly complicate the replacement of parts of the system.
  • ... and perhaps a number of other reasons.

The above is formulated in terms of C ++. Some of the reasons for circular dependencies may be missing [directly] in C, but the same concepts apply to C.

+2
source

Source: https://habr.com/ru/post/1206437/


All Articles