Tutorials Hut

Cohesion and Coupling

Cohesion and coupling are very important concepts in OOP programming language, check below definitions of cohesion and coupling.

Cohesion

Cohesion means amount of responsibility of a class or software component and how much atomic it is, this is a generic term and may apply to classes and other software components as well.

If a class writes only code for which it is responsible and not a lot of methods and code that are not class’s responsibility then it will be called high cohesion which is good.

Example of high cohesion class:

class Email {



	public void sendEmail(String msg){

    	//some code

	}



	public void receiveEmail(){

    	//some code

	}

}

If a class writes a lot of code which is not its responsibility then it will be called low cohesion which is bad, class should not become monolithic or GOD code.

Example of low cohesion class:

class Email {



	public void sendEmail(String msg){

    	//some code

	}



	public void receiveEmail(){

    	//some code

	}



	public void connectToDb() {

    	//some code

	}



	public void formatText() {

    	//some code

	}



	public void findMoreInfo() {

    	//some code

	}



	public void formatDate() {

    	//some code

	}



	public void connectToPPrinter() {

    	//some code

	}

}

How to Achieve High Cohesion

  • Write small classes.
  • Class should have one and only one responsibility.
  • Responsibility of class should be atomic.
  • Add only methods that are really needed and belongs to class’ specific responsibility.
  • If you see there are many methods in class try to logically move them to another class.

Coupling

Coupling means how much a software component or class is dependent or other component or class. Coupling is a generic term and may apply to any software component or class.

If a class is too much dependent on other classes and any change in other classes impacts the class then it is called tight(high) coupling)which is bad, a class should not be heavly dependent on others.

If a class is very less or not dependent at all on other classes then it will be called loose (low) coupling which is good for software development.

See below example where we can see MyPlanet class has a lot of dependency on PlanetDetails, PlanetFinder and PrintPlanetInfo, if some methods in those classes change may impact this class as well such high level of dependency is called high coupling which is bad.

Example:

class PlanetDetails{

	public int planetAge(String name) { return 34000000; }

}



class PlanetFinder {

	public String findPlanet() { return "Name of planet"; }

}

class PrintPlanetInfo {

	public void printPlanet(String name) { System.out.println(name);}

}



class MyPlanet {

	public static void main(String[] args) {

    	PlanetFinder planetFinder = new PlanetFinder();

    	String name = planetFinder.findPlanet();

    	PrintPlanetInfo printPlanetInfo = new PrintPlanetInfo();

    	printPlanetInfo.printPlanet(name);

    	PlanetDetails planetDetails = new PlanetDetails();

    	planetDetails.planetAge(name)

	}

}

How to Achieve Loose(low) Coupling

  • Don’t create a lot of other classes within the class one class for usage.
  • Use interface and extends to bring useful features from another class.
  • Don’t write methods which needs another class and their methods and then another class which depends on previous class’ methods’ output, this will create a lot of dependency.

Cohesion vs Coupling

Below is comparison between cohesion and coupling:

Cohesion Coupling
It measure degree of responsibility of a single class.It measures dependency of one class to other class.
High cohesion is good and low cohesion is bad.High coupling is bad and low coupling is good.
Cohesion always talks about specific class or software component.Coupling talks about more than 1 class and its dependency on others.
Cohesion refers to how high atomic responsibility is of class.Coupling refers to how much dependency one class has on others.

References

Oracle reference page on cohesion and coupling: Cohesion and Coupling

Next Article














  • Leave a Reply

    Your email address will not be published. Required fields are marked *