9

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

Is this considered bad design? An anti-pattern if you will?

skaffman
  • 398,947
  • 96
  • 818
  • 769
John Fitzpatrick
  • 4,207
  • 7
  • 48
  • 71

2 Answers2

5

Here is my take:

  • If the two classes belong to the same logical module, then it's probably fine (still a judgement call of course, with lots of grey areas).
  • It's less fine if the two classes belong to different modules. This creates a circular dependency between modules. I try to avoid that as much as can, preferring a clear hierarchical structure.
NPE
  • 486,780
  • 108
  • 951
  • 1,012
1

Yes , It's bad design , It's contradicting the oop principles , It's seems you have to create a new class or interface will contain the the shared parameters and function for a and b , a and b imports this new class...

TwTw
  • 551
  • 4
  • 15