Cohesion

Cohesion in Java

In Java, cohesion refers to the measure of how closely the members (variables and methods) within a class are related and contribute to a single, well-defined purpose or responsibility. It represents the degree to which a class has a clear and focused role.

Why is Cohesion Required?

Cohesion is required in software development to ensure that classes and modules have a single, well-defined purpose. High cohesion leads to more maintainable, reusable, and testable code. It promotes modularity and reduces dependencies between different parts of a system.

Types of Cohesion

  • Functional Cohesion: It is the highest level of cohesion where all the members of a class are related to a single, well-defined function or responsibility. Each method in the class contributes to the overall functionality.
  • Sequential Cohesion: It indicates that the members of a class are related and executed in a specific sequence, where the output of one member serves as the input for the next member.
  • Communicational Cohesion: It signifies that the members of a class are related by a common data element or communicate with each other to achieve a specific goal.
  • Procedural Cohesion: It implies that the members of a class are related because they are involved in a specific procedure or process, even if they are not directly related to each other.
  • Temporal Cohesion: It represents that the members of a class are related and executed within a specific time frame or under a specific condition.
  • Logical Cohesion: It indicates that the members of a class are related by a logical grouping or share a common purpose but do not fit into any other specific cohesion type.
  • Coincidental Cohesion: It is the lowest level of cohesion where the members of a class are unrelated and do not contribute to a single purpose. This should be avoided as it leads to poor code organization and maintainability.

Example:

Consider a class named `Employee` that represents an employee in a company. The class has multiple methods that perform different tasks related to an employee, such as calculating salary, updating personal information, and generating reports.

    
      class Employee {
          private String name;
          private int age;
          private double salary;
          
          public void setName(String name) {
              this.name = name;
          }
          
          public void setAge(int age) {
              this.age = age;
          }
          
          public void setSalary(double salary) {
              this.salary = salary;
          }
          
          public double calculateSalary() {
              // Salary calculation logic
              return salary;
          }
          
          public void updatePersonalInfo(String name, int age) {
              // Update personal information logic
              this.name = name;
              this.age = age;
          }
          
          public void generateReport() {
              // Report generation logic
          }
      }
    
  

In the above example, the `Employee` class exhibits low cohesion as it contains methods that handle various unrelated tasks such as setting employee details, calculating salary, updating personal information, and generating reports. This lack of cohesion makes the class less maintainable and harder to understand.

To improve cohesion, the class can be refactored into smaller, more focused classes, each responsible for a specific aspect of an employee's functionality. For example, separate classes can be created for salary calculation, personal information management, and report generation. This results in higher cohesion and better code organization.

Cohesion plays a vital role in creating well-structured and maintainable code. By striving for high cohesion and ensuring that class members are closely related to a single purpose, developers can improve code quality and promote modular design.

Go To OOPS Page Click Here

Post a Comment

Previous Post Next Post