Cyclic dependencies
About
How Cyclic Dependencies Arise ?
1. Direct Circular Reference
2. Indirect Circular Reference
Example of Cyclic Dependency
1. Direct Circular Dependency
class A {
private B b;
public A(B b) {
this.b = b;
}
public void methodA() {
b.methodB();
}
}
class B {
private A a;
public B(A a) {
this.a = a;
}
public void methodB() {
a.methodA();
}
}2. Indirect Circular Dependency
Problems with Cyclic Dependencies
How to Avoid Cyclic Dependencies ?
1. Refactor to Break the Cycle
2. Dependency Injection
3. Follow SOLID Principles
4. Restructure our Code:
5. Use Lazy Initialization:
6. Leverage Framework Features:
Detecting Cyclic Dependencies
1. Static Analysis Tools:
2. Dependency Graphs:
Last updated