Loose coupling and tight coupling in java – Before and After Code Example

Learn loose coupling and tight coupling in java with example. An example of tight coupling and loose coupling in java will be demonstrated with the same program example and outputs for a better understanding between their difference.

First you'll understand tightly coupled code and issue with it and then understand the loosely coupled code using the same example and it's benefits.

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

loose coupling and tight coupling definition in Java

Loose coupling in Java means reducing the dependencies of a class that uses the different classes 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.

Consider a very simple example scenario. There is a Manager and he wants to manage the work of his workers. There are two types of workers, smart worker and lazy worker.

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

TIGHTLY COUPLED

/*
 * Tight Coupling Example in java using concrete classes
 */

class Manager {

	SmartWorker sw;
	LazyWorker lw;

	public Manager(SmartWorker sw, LazyWorker lw) {

		this.sw = sw;
		this.lw = lw;
	}

	public void ManageWork() {
		sw.work();
		lw.work();
	}

}

class SmartWorker {
	public void work() {
		System.out.println("smart worker working");
	}

}

class LazyWorker {
	public void work() {
		System.out.println("Lazy worker working");
	}

}


public class Test {

	public static void main(String[] args) {

		SmartWorker sw = new SmartWorker();
		LazyWorker lw = new LazyWorker();
		Manager mn = new Manager(sw, lw);
		mn.ManageWork();

	}

}

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

class ExtraordinaryWorker {
	public void work() {
		System.out.println("ExtraOrdinary worker working");
	}
}

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 ExtraordinaryWorker like below.

class Manager {

	SmartWorker sw;
	LazyWorker lw;
	ExtraordinaryWorker ew; //add one more object

	//modify constructor
	public Manager(SmartWorker sw, LazyWorker lw,ExtraordinaryWorker ew) {

		this.sw = sw;
		this.lw = lw;
		this.ew = ew;//add
	}

	public void ManageWork() {
		sw.work();
		lw.work();
		ew.work();//make call
	}

}

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 the 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.

We can make the tightly coupled code to loosely couple by using an interface instead of using concrete classes directly.

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

/*
 * Loose Coupling in java example
 */

class Manager {

	IWorker worker;
	public Manager(IWorker worker) {

		this.worker=worker;
		
	}

	public void ManageWork() {	
		
		this.worker.work();
	}

}

interface IWorker{
	void work();
	
}
class SmartWorker implements IWorker{
	public void work() {
		System.out.println("smart worker working");
	}

}

class LazyWorker implements IWorker {
	public void work() {
		System.out.println("Lazy worker working");
	}

}

class ExtraordinaryWorker implements IWorker{
	public void work() {
		System.out.println("ExtraOrdinary worker working");
	}
}


public class Test {

	public static void main(String[] args) {

		
		SmartWorker sw = new SmartWorker();
		Manager mn = new Manager(sw);
		mn.ManageWork();
		
		LazyWorker lw = new LazyWorker();
		Manager mn2 = new Manager(lw);
		mn2.ManageWork();
		
		ExtraordinaryWorker ew = new ExtraordinaryWorker();
		Manager mn3 = new Manager(ew);
		mn3.ManageWork();

	}

}

Output:

smart worker working
Lazy worker working
ExtraOrdinary worker working

Conclusion:

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

For Your Information:

You can stop your mind raining these type of OOP Questions with the book OOP Concepts Booster. View some OOPs Questions Sample here from the book.



8 thoughts on “Loose coupling and tight coupling in java – Before and After Code Example”

  1. Thanks for that topic. I’m a self tought programer who’s still learning and your explanation was clear. I understood the concept. Keep up the good work. Thank you!

    Reply
    • You are right Mammad. The baby steps in the main() method are written intentionally for some novice programmers. Goal was to make all users understand the confusing topics easily. Thanks for your response, it will be a notice to people 🙂

      Reply

Leave a Comment