Are circular dependencies bad?

In my work (this is 90% Java, but I'm sure this question applies to other languages). I often create two classes that "know about each other." More specifically, class A imports B, and class B imports class A, and both have member or local variables of a different type.

Is this a bad design? Anti-pattern if you want?

+6
source share
2 answers

Here is my trick:

  • If two classes belong to the same logical module, then this is probably excellent (call on call, of course, with a lot of gray areas anyway).
  • This is less good if two classes belong to different modules. This creates a cyclic dependency between modules. I try to avoid as much as possible, preferring a clear hierarchical structure.
+4
source

Yes, this is a bad design, This is against the principles of oop. It seems you need to create a new class or interface that will contain common parameters and functions for a and b, a and b, import this new class ...

+1
source

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


All Articles