Where loose and tight coupling would be used as a real scenario?
Tight Coupling
In complex cases the logic of one class will call the logic of another class just to provide the same service
If this happen there is the so called tight-coupling between the two classes.
In this case the first class that wants to call the logic from the second class will have to create an object from the second class
Example: we have two classes first is traveller
and the second is a car
. Traveller
class is calling logic of car
class; in this case traveller class creates an object of car class.
This will mean the car
class will depend on the traveller
object.
Public class Traveller {
Car c = new Car();
Public void startJourney() {
c.move();
}
}
Public class Car {
Public void move(){
...
}
}
Here traveller
object is tightly coupled with car
object.
If traveller
wants to change from car
to plane
then the whole traveller
class has to be changed like the following:
Public class Traveller {
Plane p = new Plane();
Public void startJourney() {
p.move();
}
}
Public class Plane {
Public void move(){
...
}
}
Loose Coupling
Our main object,
traveller
in this case will allow an external entity, a so calledcontainer
to provide theobject
. Sotraveller
doesn't have to create an own class from thecar
orplane
object it will get it from thecontainer
When a object allow dependency injection mechanism.
The external entity, the
container
can inject thecar
orplane
object based on a certain logic, a requirement of thetraveller
.
Example:
Public class Traveller {
Vehicle v;
Public void setV(Vehicle v) {
this.V = V;
}
Public void startJourney() {
V.move();
}
}
Here traveller
class injects either a car
or a plane
object. No changes required in traveller
class if we would like to change the dependency from car to a plane.
Traveller
class took a vehicle reference, so an external object (Container) can inject either car
object or plane
object, depends on requirement of traveller
.
Tight-Coupling:-
While creating complex application in java, the logic of one class will call the logic of another class to provide same service to the clients.
If one class calling another class logic then it is called collaboration.
When one class is collaborating with another class then there exists tight-coupling between the two classes.
If one class wants to call the logic of a second class then they first class need an object of second class it means the first class create an object of second class.
For example, if we have two classes called traveller and car, traveller class is calling logic of car class; in this case traveller class creates an object of car class.
In the above traveller class and car classes, car class object of dependency for traveller object.
Example:-
- In the above example traveller object is tightly coupled with car object because in place car object if you want to use bike object then, we need to make changes in Traveller class
Example :-
Loose-Coupling:-
In Loose-Coupling, when one object is depending on another class object, some external entity will provide that dependency object to the main object that external object we call as a Container.
In order to get loose-coupling between objects the following two rules are required
The classes should follow POJI/POJO model.
Apply dependency injection mechanism.
For example:-
In the above traveler class, an external entity injects either car (or) Bike object.
In traveler, these are no changes required we are shifting the dependency from car to a Bike.
In the above traveler class, we are token vehicle reference, so that an external object (Container) can injects either car object (or) Bike object, depends on requirement if a traveler.
In spring frame work, spring container follows dependency injection mechanism and injects the dependency objects required for a main object.
Spring frame work is much success because of one of the main reason is it promotes Loose-Coupling between the objects.
Source:- Tight-coupling and Loose-coupling between objects