Java — Coupling in Java | Code Factory

Code Factory
4 min readNov 23, 2020

--

Donate : Link

WordPress Blog : Link

Applications… : Link

In object oriented design, Coupling refers to the degree of direct knowledge that one element has of another. In other words, how often do changes in class A force related changes in class B.

There are two types of coupling:

1. Tight coupling

  • In general, Tight coupling means the two classes often change together. In other words, if A knows more than it should about the way in which B was implemented, then A and B are tightly coupled.
  • Example : If you want to change the skin, you would also have to change the design of your body as well because the two are joined together — they are tightly coupled. The best example of tight coupling is RMI(Remote Method Invocation).
package com.example.java.programming;/**
* @author code.factory
*/
class ClassA {
ClassB classB = new ClassB();
public void sayHello() {
classB.hello();
}
}
class ClassB {
public void hello() {
System.out.println("Hello...");
}
}

Explanation: In the above program the ClassA class is dependents on ClassB class. In the above program ClassA class is tightly coupled with ClassB class it means if any change in the ClassB class requires ClassA class to change. For example, if ClassB class hello() method change to hi() method then you have to change the sayHello() method will call hi() method instead of calling hello() method.

package com.example.java.programming;/**
* @author code.factory
*/
public class Test {
public static void main(String... strings) throws Exception {
Hello h = new Hello();
h.msg = "Code Factory";
System.out.println(h.msg);
}
}
class Hello {
public String msg = "Hi";
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
if(msg.length() < 5) {
this.msg = msg;
}
}
}
  • In the above example, the code that is defined by this kind of implementation uses tight coupling and is very bad since class Test knows about the detail of class Hello, if class Hello changes the variable ‘str’ to private then class Test breaks, also class Hello’s implementation states that variable ‘str’ length should not be more than 5 but as we can see there is no way to enforce such a rule as we can go directly to the variable and change its state to whatever value we decide.

Output:

Code Factory

2. Loose coupling

  • In simple words, loose coupling means they are mostly independent. If the only knowledge that class A has about class B, is what class B has exposed through its interface, then class A and class B are said to be loosely coupled. In order to over come from the problems of tight coupling between objects, spring framework uses dependency injection mechanism with the help of POJO/POJI model and through dependency injection its possible to achieve loose coupling.
  • Example : If you change your shirt, then you are not forced to change your body — when you can do that, then you have loose coupling. When you can’t do that, then you have tight coupling. The examples of Loose coupling are Interface, JMS.
interface ClassB {
void hello();
}
class ClassB1 implements ClassB {
public void hello() {
System.out.println("ClassB1 Hello...");
}
}
class ClassB2 implements ClassB {
public void hello() {
System.out.println("ClassB2 Hello...");
}
}
class ClassA {
ClassB classB = new ClassB1();
public void sayHello() {
classB.hello();
}
}
  • Explanation : In the above example, ClassB1 and ClassB2 objects are loosely coupled. It means ClassB is an interface and we can inject any of the implemented classes at run time and we can provide service to the end user.
package com.example.java.programming;/**
* @author code.factory
*/
public class Test {
public static void main(String... strings) throws Exception {
Hello h = new Hello();
h.setMsg("Code Factory");
System.out.println(h.getMsg());
}
}
class Hello {
private String msg = "Hi";
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
if(msg.length() < 5) {
this.msg = msg;
}
}
}
  • In the above example, the code that is defined by this kind of implementation uses loose coupling and is recommended since class Test has to go through class Hello to get its state where rules are enforced. If class Hello is changed internally, class Test will not break as it uses only class Hello as a way of communication.

Output:

Hi

Which is better tight coupling or loose coupling?

  • In general, Tight Coupling is bad in but most of the time, because it reduces flexibility and re-usability of code, it makes changes much more difficult, it impedes test ability etc. loose coupling is a better choice because 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.

--

--