Tight coupling
Tight coupling in simpler terms means one part of code tightly coupled with another part of code, modifying one part of code can cause rippling effects across the code base like how spaghetti is confusing while delicately connecting intricate parts with one another.
Loose coupling
Loose coupling in simpler terms can be compared with socket and cord. A socket is a framework and any cord could be plugged in, used and then can be replaced with another.
Scenario
Let’s consider a man who has joined a company recently. Since he is a fresher he could learn any task quickly and can approach any task with a growth mindset. If he is tightly coupled with the type of work he do and won’t accept tasks which dares his confidence, then he could not grow in the career since he won’t be able to explore the other sides of the world.
Let’s consider tightly coupled scenario…
EnergeticMan class denoting the person. He is tightly coupled with backend development alone.
package com.java.examplesworld;
public class EnergeticMan {
public void work(BackendDeveloper work) {
work.doWork();
}
}
BackendDeveloper class
package com.java.examplesworld;
public class BackendDeveloper {
public void doWork() {
System.out.println("Develop resilient backend for the website");
}
}
Main class
import com.java.examplesworld.*;
public class Main {
public static void main(String[] args) {
EnergeticMan Rex = new EnergeticMan();
Rex.work(new BackendDeveloper());
}
}
Result of executing main class.
So by the above code Rex can only do back-end development.
Let’s now consider a case that Rex changes his mind and wants to become a full-stack developer and now he accepts front-end tasks also. So now how can we make the code loosely coupled.
We can make it loosely coupled by introducing an interface called as Work, which could be implemented by any type of work classes so that Rex can accept any type of work and do any type of work.
Work interface
package com.interfaces.java.examplesworld;
public interface Work {
public void doWork();
}
Backend and frontend developer classes implementing Work interface.
package com.java.examplesworld;
import com.interfaces.java.examplesworld.*;
public class BackendDeveloper implements Work{
public void doWork() {
System.out.println("Develop resilient backend for the website");
}
}
package com.java.examplesworld;
import com.interfaces.java.examplesworld.*;
public class FrontendDeveloper implements Work{
public void doWork() {
System.out.println("Development user friendly frontend applications.");
}
}
EnergeticMan transformation, now accepting any type of work!!!
package com.java.examplesworld;
import com.interfaces.java.examplesworld.Work;
public class EnergeticMan {
public void work(Work work) {
work.doWork();
}
}
Main class
import com.java.examplesworld.*;
public class Main {
public static void main(String[] args) {
EnergeticMan Rex = new EnergeticMan();
Rex.work(new BackendDeveloper());
Rex.work(new FrontendDeveloper());
System.out.println("Yay! Rex is now a full stack developer.");
}
}
Result of this transformation..
Conclusion
We’ve looked into some funny examples demonstrating tight and loose coupling. I hope you would have learnt something useful. If you like it kindly motivate me by giving your claps to write more.