Loose coupling and tight coupling in java – Comparison Example

(Last Updated On: May 9, 2018)

Notes on loose coupling and tight coupling in java with example. Example of tight coupling and loose coupling in java will be demonstrated with same program and outputs for better understanding between their difference.

Tight coupling will be implemented using concrete implementation of classes and loose coupling will be achieved by using class and interface.

loose coupling and tight coupling definition
Loose coupling means reducing dependencies of a class that use different class directly. Tight coupling means classes and objects are dependent on one another.

Let’s understand loosely coupled code and tightly coupled code by example and their advantages and disadvantages with this example only.
Let’s consider a very simple example scenario. There is a Manager and he wants to manage work of his workers. There are two workers, smart worker and lazy worker.

Below are the classes for Manager, Smart worker and Lazy worker and complete program for this scenario. Code is self-understandable. Manager class have created objects of smart and lazy workers classes and have call them in ManageWork() method.

 

TIGHTLY COUPLED

 

 

Output:
smart worker working
Lazy worker working

 

So for sounds good. But let’s say requirement changes and we want to add one more worker that is Extraordinary Worker.

What will you do now? It’s very simple. We will create a class for Extraordinary Worker like below

 

And we can add one more object in Manager class. We will change the constructor code and in method ManageWork(), we will add code to make a call for Super worker like below.

 

What did you notice?

We notice that the source code is tightly coupled and that’s the reason we had to modify the existing manager class. You saw that Manager class was modified at three places. Add extra object of extraordinary worker, modified constructor and manageWork() method too. It violets OPEN CLOSED PRINCIPLE ( A class should be open for extension and closed for modification).  You can read open closed oops design principle . The code is in C++ but concept is same for oops and easily understandable.

 

NOTE:Note that loosely coupled code should not be modified or has minimal modification.

Disadvantage of above tightly coupled code is that it required modification and testing. In other words, it took effort, testing and time. This is the simple program, but, consider it is in large project and Manager class is very complex and have tons of operations to perform. In real time no product is released without testing.

As a conclusion, tightly coupled code takes maintenance time and huge efforts.

 

LOOSELY COUPLED

Loosely coupled code reduces the cost of maintenance and efforts. Below is the same example implemented as a loosely coupled code using interface. I will recommend to read Interface example in java oops.

Notice that we are not going to touch the Manager class when we want to extend with Extra ordinary worker class.

Just we will write new extra ordinary worker class and implement the interface. And in client program, we will call him.

We will create an interface IWorker and every worker will implement it.This program follows open-close principle and dependency Inversion.

Loose Coupling Example

 

 

Output:

smart worker working
Lazy worker working
ExtraOrdinary worker working

 

Conclusion:

  • Short definition of loose coupling and tight coupling in java is that loose coupling means reducing dependencies of a class that use different class directly. Tight coupling means classes and objects are dependent on one another.
  • Disadvantage of tightly coupled code is that it took effort, testing and time that is reduced by writing loosely coupled coupled code.
  • tightly coupled code takes maintenance time and huge efforts.