Cohesion in Java with Example

1. Overview

In this article, we will learn the important object-oriented concept of Cohesion. The term cohesion is used to indicate the degree to which a class has a single, well-focused responsibility. In this article, we will learn about Cohesion in detail with examples.

2. Intent/Definition

The term cohesion is used to indicate the degree to which a class has a single, well-focused responsibility.

Cohesion is a measure of how the methods of a class or a module are meaningfully and strongly related and how focused they are in providing a well-defined purpose to the system.

3. Implementation

In object-oriented design, cohesion refers all about how a single class is designed. Cohesion is the Object Oriented principle most closely associated with making sure that a class is designed with a single, well-focused purpose.
The more focused a class is, the cohesiveness of that class is more.
The advantages of high cohesion are that such classes are much easier to maintain (and less frequently changed) than classes with low cohesion. Another benefit of high cohesion is that classes with a well-focused purpose tend to be more reusable than other classes.

4. Two types of Cohesion

  1. Low Cohesive
  2. High Cohesive

4.1 Low Cohesive

A class is identified as a low cohesive class when it contains many unrelated functions within it. And that what we need to avoid because big classes with unrelated functions hamper their maintaining. Always make your class small and with precise purpose and highly related functions.
Example: In this example, the purpose of MyReader class is to read the resource. But it contains some unrelated functions such as validateLocation(), checkFTP(), ping(). Hence it is a low cohesive.
class MyReader {
    // -------------- unrelated functions
    public boolean validateLocation(String pathIP) {
         return ping(pathIP) && checkFTP(pathIP);
    }
    private boolean checkFTP(String pathIP) {
         return true;
    }

    private boolean ping(String pathIP) {
          return true;
    }

    // -------------- functions related to read resource

    // read the resource from disk
    public String readFromDisk(String fileName) {
         return "data of " + fileName;
    }

    // read the resource from web
    public String readFromWeb(String url) {
         return "data of " + url;
    }

    // read the resource from network
    public String readFromNetwork(String networkAddress) {
         return "data of " + networkAddress;
    }

}

4.2 High Cohesion

  1. The code has to be very specific in its operations.
  2. The responsibilities/methods are highly related to the class/module.
  3. The term cohesion is used to indicate the degree to which a class has a single, well-focused responsibility. Cohesion is a measure of how the methods of a class or a module are meaningfully and strongly related and how focused they are in providing a well-defined purpose to the system. The more focused a class is the higher its cohesiveness - a good thing.
  4. A class is identified as a low cohesive class when it contains many unrelated functions within it. And that what we need to avoid because big classes with unrelated functions hamper their maintaining. Always make your class small and with precise purpose and highly related functions.
Example: In this example, the purpose of MyReader class is to read the resource and it does that only. It does not implement other unrelated things. Hence it is highly cohesive.
class HighCohesive {
    // -------------- functions related to read resource
    // read the resource from disk
    public String readFromDisk(String fileName) {
         return "reading data of " + fileName;
    }

    // read the resource from web
    public String readFromWeb(String url) {
         return "reading data of " + url;
    }

     // read the resource from network
    public String readFromNetwork(String networkAddress) {
         return "reading data of " + networkAddress;
    }
}

Related Oops Posts

Top Core Java Tutorials

  1. Java Tutorial for Beginners
  2. 50 Java Keywords
  3. JDBC 4.2 Tutorial
  4. All Java/J2EE Tutorial
  5. Java 8 Tutorial
  6. Java Collections Tutorial
  7. Java Exceptions Tutorial
  8. Java Generics Tutorial
  9. Java 8 Stream API Tutorial
  10. Java Wrapper Classes
  11. Java Arrays Guide
  12. Java Multithreading Tutorial
  13. Java Concurrency Tutorial
  14. Oops Concepts Tutorial
  15. Java String API Guide
  16. Java Reflection API Tutorial
  17. Java I/O Tutorial
  18. Date and Time API Tutorial
  19. JUnit 5 Tutorial
  20. JUnit 4 Tutorial
  21. Java XML Tutorial
  22. Google GSON Tutorial

Top Java EE Tutorials

  1. Spring Security Tutorial
  2. RabbitMQ Tutorial
  3. Hibernate ORM 5
  4. Java Persistence API
  5. Spring Boot 2 Tutorial
  6. Spring Core 5 Tutorial
  7. Spring MVC 5 Tutorial
  8. Spring Data JPA Tutorial
  9. Apache HttpClient Tutorial
  10. Spring Framework 5
  11. Apache Maven Tutorial
  12. JAX-RS Tutorial
  13. Jersey Rest Tutorial

Java Library Tutorials

  1. Java API Guides
  2. Java SQL Package Tutorial
  3. All Java/J2EE Tutorial
  4. Java Lang Package Tutorial
  5. Java Util Package Tutorial
  6. Java Lang Reflect Package Tutorial
  7. Java Time Package Tutorial
  8. Java IO Package Tutorial

Top Java Best Practices

  1. Java Enums and Annotations Best Practices
  2. Java Generics Best Practices
  3. JUnit Framework Best Practices
  4. Java Naming Conventions
  5. Single Responsibility Principle
  6. Liskov's Substitution Principle
  7. Interface Segregation Principle
  8. Dependency Inversion Principle
  9. Open Closed Principle
  10. Oops principles in java
  11. Restful API Best Practices
  12. JSP Best Practices
  13. Guide to JDBC Best Practices
  14. Collection Best Practices
  15. String Best Practices in Java
  16. Exception Handling Best Practices
  17. Synchronization Best Practices
  18. Guide to JDBC Best Practices
  19. Serialization Best Practices
 
The source code of this post is available on GitHub: Object-Oriented Design Guide

Comments