Sunday, October 31, 2021

Java OOPs: Coupling and Cohesion

Chapters

Coupling

Coupling refers to the dependency between two classes. There are two types of coupling: Tight Coupling and Loose Coupling.

Tight Coupling

Tight coupling means that two classes are highly dependent on each other. In tight coupling, if a class has changes then the other class will very likely need to do some changes to preserve their dependency on each other.

Tight coupling is used sparingly in coding due to its restrictive nature that makes our code stiff and hard to maintain. Too much use of tight coupling is a bad software design.
public class SampleClass{

  public static void main(String[] args){
    ClassB b1 = new ClassB();
    b1.sum();
  }
}

class ClassA{
  
  protected int a = 1;
  protected int b = 2;
  protected int c = 3;
  protected int d = 4;
  
  
}

class ClassB extends ClassA{

  void sum(){
    int result = a + b + c + d;
    System.out.println("sum: " + result);
  }
}
In the example above ClassA and ClassB are tighly coupled. How? you might ask. Try removing int a from ClassA. if you do that, you need to remove the "a" in ClassB. Same goes for other variables in ClassA. The changes in the code structure of ClassA have effect in the code structure of ClassB.

Loose Coupling

Loose coupling means that two classes are not highly dependent on each other. In loose coupling, if a class has changes then the other class will likely not need to do some changes to preserve their dependency on each other.

Loose coupling is often used in coding due to its flexible nature that makes our code easy to maintain. A good software design has more loosely coupled classes than tightly coupled ones.
public class SampleClass{

  public static void main(String[] args){
    ClassB b1 = new ClassB();
    b1.sum();
  }
}

class ClassA{
  
  private int a = 1;
  private int b = 2;
  private int c = 3;
  private int d = 4;
  
  protected int addFirst(){
    return a + b;
  }
  
  protected int addSecond(){
    return c + d;
  }
}

class ClassB extends ClassA{

  void sum(){
    int result = addFirst() + addSecond();
    System.out.println("sum: " + result);
  }
}
In the above example, ClassA and ClassB are loosely coupled. How? you might ask. Try removing int a and remove "a" and "+" in addFirst() block. If you do that, ClassB doesn't need to do any changes because the changes in the code structure of ClassA doesn't have any effect in the code structure of ClassB.

Cohesion

Cohesion refers to the functionality of a class. There are two types of cohesion: Low Cohesion and High Cohesion.

Low Cohesion

a class has low cohesion if it's focusing on multiple functionalities. A class with low cohesion may cause confusion which make our code less maintainalble and hard to read. We shouldn't create low cohesive classes often.
class DataOperation{

  void printData(){
    System.out.println("Data Printed!");
  }
  
  void printData(String filter){
    System.out.println("Data Printed!");
    System.out.println("Filter: " + filter);
  }
  
  void filterData(){
    System.out.println("Data Filtered!");
  }
  
  void filterData(String filter){
    System.out.println("Data Filtered!");
    System.out.println("Filter: " + filter);
  }
  
}
The class in the example above is a low cohesive class because the class focuses on two functionalities: the filter and print functionalities.

High Cohesion

a class has has cohesion if it's highly or solely focusing on a single functionality. A class with high cohesion are less confusing and easy to read. A good software design has more high cohesive classes that low cohesive ones.
class DataPrinter{

  void printData(){
    System.out.println("Data Printed!");
  }
  
  void printData(String filter){
    System.out.println("Data Printed!");
    System.out.println("Filter: " + filter);
  }
  
}
The class in the example above is a high cohesive class because the class solely focuses on one functionality: the print functionality.

No comments:

Post a Comment