Deferring the setting of a dependency from constructor to setter

Let's understand how solves circular dependency by providing a dependency from the constructor to the setter method. There is a special case where due to circular dependency, you can't even create the object of the domain model. For example, say you are developing an application for a tyre manufacturer; who uses these tyres for cars. Based on the car's max speed, you need to set the min rim size of the tyre. For this, you have created the Car and Tyre classes, as in the following snippet:

public class Car {
private Tyre tyre;
private int maxSpeed;

public Car(Tyre tyre) {
this.tyre = tyre;
setMaxSpeed(150);
}
public int getMaxSpeed() {
return maxSpeed;
}
public void setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
}
}


public class Tyre {
private Car car;
private int minRimSize;

public Tyre(Car car) {
this.car = car;
if(this.car.getMaxSpeed()>100 && this.car.getMaxSpeed()<200) {
setMinRimSize(15);
}else if(this.car.getMaxSpeed()<100) {
System.out.println("Minimum RIM size is 14");
setMinRimSize(14);
}
}
public int getMinRimSize() {
return minRimSize;
}
public void setMinRimSize(int minRimSize) {
this.minRimSize = minRimSize;
}
}

As you can see, the Car and Tyre classes are dependent on each another. The dependency is passed through the constructor, hence why it is a circular dependency. You can't create an object for either of them. To handle this situation, you need to defer setting the dependency from the constructor to the setter in each case. We decided to make this change in the Car class, as in the following snippet:

public class Car{
private Tyre tyre;
private int maxSpeed;

public Car() {
}
public void setTyre(Tyre tyre) {
this.tyre = tyre;
}
public Tyre getTyre() {
return tyre;
}
public int getMaxSpeed() {
return maxSpeed;
}
public void setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
}
}

The dependency (of Tyre) is moved from the constructor to the setter method. In the Tyre class, you need to set a reference of the current class (Tyre) into the Car object, as in the following snippet:

public class Tyre {

private Car car;
private int minRimSize;

public Tyre(Car car) {
this.car = car;
this.car.setTyre(this);

if(this.car.getMaxSpeed()>100 && this.car.getMaxSpeed()<200) {
System.out.println("Minimum RIM size is 15");
setMinRimSize(15);
}else if(this.car.getMaxSpeed()<100) {
System.out.println("Minimum RIM size is 14");
setMinRimSize(14);
}

}
public int getMinRimSize() {
return minRimSize;
}
public void setMinRimSize(int minRimSize) {
this.minRimSize = minRimSize;
}
}

Everything is settled now. You can create an object of type Car first, then create an object of type Tyre so that you can pass the reference of the car object to it. The client code will be as in the following code snippet:

public class CircularDependencyWithSetterDemo {

public static void main(String[] args) {
Car car = new Car();
car.setMaxSpeed(120);
Tyre tyre = new Tyre(car);

car.setMaxSpeed(90);
tyre = new Tyre(car);
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.78.102