Coupling is the degree to which one class knows about another class.
Loose coupling means reducing dependencies of a class that use different class directly . Tight coupling means classes and objects are dependent on one another. In general tight coupling is usually not good because it reduces flexibility and re-usability of code and it makes changes much more difficult and not easy to test.
Loose coupling is the heart concept of Spring Core Module.
Tight-Coupling:-
See below code is for tight coupling.
public class Traveller {
Car car = new Car();
public void startJourney() {
car.move();
}
}
public class Car {
public void move() {
System.out.println("Travel by Car");
}
}
Example:-
In the above example, Traveler object is depends on car object. So traveler class creating an object of Car class inside it.
-
If the other class object is created in the dependent class [like Car class object in Traveler class], there exist tight coupling, I mean if method in car object is changed then we need to do the changes in the Traveler class too so its the tight coupling between Traveler and Car class objects.
Example :-
In the above example Traveller object is tightly coupled with car object because in place of car object if you want to use bike object then, we need to make changes in Traveller class.
Loose-Coupling:-
- The classes should follow POJI/POJO model.
- Apply dependency injection mechanism.
For example:-
Below code is an example of loose coupling,
public interface Vehicle {
void move();
}
public class Car implements Vehicle {
@Override
public void move() {
System.out.println("Travel by Car");
}
}
public class Bike implements Vehicle {
@Override
public void move() {
System.out.println("Travel by Bike");
}
}
// create main class Traveller
public class Traveller {
Vehicle v;
public void setV(Vehicle v) {
this.v = v;
}
public void startJourney() {
v.move();
}
}
In the above example, Traveller and Car objects are loosely coupled. It means Vehicle is an interface and we can inject any of the implemented classes at run time and we can provide service to the end user.
But Question is, how to inject Object?? Spring container will inject either Car object or Bike object into the Traveller by calling setter method, So if Car object is replaced with Bike then no changes are required in Traveller class, this means there is loose coupling between Traveller and Vehicle object.
The examples of Loose coupling are Interface, JMS, Spring IOC(Dependency Injection, it can reduce the tight coupling).
Final code :
Tight Coupling Between Java Objects
class Traveler
{
Car c=new Car();
void startJourney()
{
c.move();
}
}
class Car
{
void move()
{
// logic...
}
}
Loose Coupling Between Java Objects
class Traveler
{
Vehicle v;
public void setV(Vehicle v)
{
this.v = v;
}
void startJourney()
{
v.move();
}
}
//=========================Interface====================================
Interface Vehicle
{
void move();
}
//====================Multiple class implement vehicle interface. First class====
class Car implements Vehicle
{
public void move()
{
// logic
}
}
//===================Second class================
class Bike implements Vehicle
{
public void move()
{
// logic
}
}
Advantages Of Loose coupling:-
A loosely coupled will help you when your application need to change or grow. If you design with loosely coupled architecture, only a few parts of the application should be affected when requirements change. With too a tight coupled architecture, many parts will need to change and it will be difficult to identify exactly which parts will be affected. In short,
1) It improves the testability.
2) It helps you follow the GOF principle of program to interfaces, not implementations.
3) The benefit is that it’s much easier to swap other pieces of code/modules/objects/components when the pieces are not dependent on one another.
4) It’s highly changeable. One module doesn’t break other modules in unpredictable ways.