Cohesion And Coupling in OOPs

Back to home
Logicmojo - Updated Dec 11, 2023



Cohesion and Coupling: Two OO Design Principle

The quality of an OO design is determined by cohesion and coupling. Good OO design should be loosely connected and very coherent in general. The concept of "loose coupling and high cohesiveness" underpins many of the design ideas and patterns that have been developed.

The aim of the design should be to make the application:

  1. easier to develop

  2. easier to maintain

  3. easier to add new features

  4. less Fragile.

Coupling and cohesion are two words in software engineering that are sometimes misinterpreted. These phrases are used to describe a qualitative analysis of a system's modularity, and they aid in identifying and quantifying the design complexity of object-oriented systems.

However, a good knowledge of both is necessary to build systems that are scalable, manageable and can be extended over time.

Cohesion

In computer programming, cohesion refers to the degree to which the elements inside a module belong together. In one sense, it is a measure of the strength of relationship between the methods and data of a class and some unifying purpose or concept served by that class. In another sense, it is a measure of the strength of relationship between the class's method and data themselves.

Cohesion represents the clarity of the responsibilities of a module.So, cohesion focuses on how single module/class is designed. Higher the cohensiveness of the module/class, better is the OO design.

Advantages of high cohesion

  1. Reduced module complexity (they are simpler, having fewer operations).

  2. Increased system maintainability, as logical domain changes touch fewer modules and changes in one module necessitate fewer changes in others.

  3. Increased module reusability, as application developers will be able to discover the component they require more readily among the module's cohesive collection of activities.

Example about cohesion

We can see that when there is a lack of cohesiveness, only one class is in charge of performing a large number of jobs that are not all the same. It will lower the likelihood of reusability and upkeep.

There is a separate class for all the jobs to execute a given work in high cohesion, which results in improved usability and maintenance.

There is a separate class for all the jobs to execute a given work in high cohesion, resulting in improved usability and maintenance.

  1. High cohesion is when we have a class that does a well defined job. Low cohesion is when a class does a lot of jobs that don’t have much in common.

  2. High cohesion gives us better maintaining facility and Low cohesion results in monolithic classes that are difficult to maintain, understand and reduces re-usability


For example,

public class Person {
     private int     age;
     private String  name;

     // getter, setter properties.

     // method
     public void readInfor();

     public void writeInfor();
 }

The Person class has low cohesion, simply because Person’s responsibilities is relevant to save information about people. It do not relate to functionalities about read/write to file. So, to reduce low cohension, we should separate the implementation about read/write file into other class such as File, …



Coupling

Coupling is the degree of interdependence between software modules; a measure of how closely connected two routines or modules are; the strength of the relationships between modules.

Coupling increases between two classes A and B if:

  1. A has an attribute that refers to (is of type) B.

  2. A calls on services of an object B.

  3. A has a method that reference B (via return type or parameter).

  4. A is a subclass of (or implements) class B.

Low coupling describes a connection in which one module communicates with another module through a simple and stable interface and is not concerned with the internal implementation of the other module.

When this coupling is high, we may assume that the software modules are interdependent, i.e., they cannot function without the other. There are several dimensions of coupling:

  1. Content coupling : This is a sort of connection in which one module can edit or access the content of another module. In essence, there is a control coupling between the two components when one component passes parameters to influence the activity of another component.

  2. Common coupling : This is a sort of coupling in which multiple modules have access to a common global data store.

  3. Stamp coupling :This is a type of coupling in which data structures are utilised to transfer data from one system component to another.

  4. Control coupling :This is a sort of coupling in which one module can alter the execution flow of another.

  5. Data coupling :Two modules interact by exchanging or transferring data as a parameter in this sort of coupling.



With this article at Logicmojo, you must have the complete idea of cohesion and coupling.