Tight coupling and loose coupling between Java Objects

  1. While developing java applications one class will collaborate with another class to provide the service to the end user. In java one class is collaborating other class means one class is using the business method of the other class.
  2. If one class is calling the other class method means then we say that both classes are coupled together.
  3. Generally one class is depending on another class then that Java class will create an object of its dependent class and then calls the business method of that class.

public class Travel {
 Car car = new Car();
 void startJourney() {  
  car.travel();
 }
}


public class Car {
 
 void travel(){
  System.out.println("Travel by Car"); 
 }
}

In the above example Travel class is dependent on Car class to provide service to the end user called Journey. Car class is dependency of Travel class.

In the above case Travel class is tightly coupled with Car class it means if any change in Car class requires Travel class to change. For example if car class travel() method change go() method then Travel class startJourney() method need to call go() instead of travel() method.

public class Travel {
         Car car = new Car();
 void startJourney() {
  
  car.go();
 }
}


public class Car {
  
 void go(){
  System.out.println("Travel by Car"); 
 }
}

If we directly create the dependent object required in a class then also we will get tight coupling between objects. For example if we want to replace Car class with Bike then the Travel class need to create Bike object by removing Car object. In Tight coupling between objects, if dependent object is modified then main object is also need to be modified.
To avoid tight coupling between objects then we need to follow Interface(POJI) and implemented class (POJO) method. If we follow POJI/POJO model then it is possible to get loose coupling between objects. In the above example to get loose coupling between Travel and Car class we need to rewrite like below.

public interface Vehicle {
 
 void start();

}
public class Car implements Vehicle{

 @Override
 public void start() {
  System.out.println("Travel by car");
  
 }

}


public class Bike implements Vehicle {

 @Override
 public void start() {
  System.out.println("Travel by Bike");

 }

}

public class Travel {
 
 Vehicle v;
 public void setV(Vehicle v){
  this.v=v;
 }
 
 public void startJourney(){
  v.start();
 }

}

In the above example Car and Travel objects are loose coupled. It means Vehicle is an Interface and we can inject any of the implemented classes at runtime and we can provide service to end user called Journey.

The dependent object is externally injected by calling its setter method. So in Travel class no changes are required even though if we change vehicle , Car, Bike. This concept is called loose coupling between objects. Spring framework provides loose coupling between objects by following poji/pojo model, and hence spring is said to be a most popular framework for the development of enterprise level applications in java.

No comments

Powered by Blogger.