Wednesday, July 3, 2019

Coupling Vs Cohesion In Java

Q- Difference between cohesion and coupling?

Note:- Low coupling and high cohesion is the best for any software development.

Loose coupling and tight coupling in java – Comparison Example

The tight coupling will be implemented using a concrete implementation of classes and we can achive loose coupling by using class and interface.

Loose coupling means reducing the dependencies of a class that uses the different classes directly. Tight coupling means classes and objects are dependent on one another.

Let’s understand loosely coupled code and tightly coupled code by example and their advantages and disadvantages with this example only.

Tight Coupling Example:

/*
 * Tight Coupling Example in java using concrete classes
 */

class Manager {

 SmartEmployee smartEmp;
 LazyEmployee lazyEmp;

 public Manager(SmartEmployee smartEmp, LazyEmployee lazyEmp) {

  this.smartEmp = smartEmp;
  this.lazyEmp = lazyEmp;
 }

 public void ManageWork() {
  smartEmp.work();
  lazyEmp.work();
 }
}

class SmartEmployee {
 public void work() {
  System.out.println("Smart Emplayee Working");
 }

}

class LazyEmployee {
 public void work() {
  System.out.println("Lazy Emplayee Working");
 }
}

public class TightCouplingExample {

 public static void main(String[] args) {

  SmartEmployee smartEmp = new SmartEmployee();
  LazyEmployee lazyEmp = new LazyEmployee();
  Manager mn = new Manager(smartEmp, lazyEmp);
  mn.ManageWork();
 }
}
Output:
Smart Emplayee Working
Lazy Emplayee Working
So the above code seems good and fullfill the current requirement.But let’s say if requirement changes and we want to add one more employee type that is Extraordinary Employee then we nee to modify above code.

/*
 * Tight Coupling Example in java using concrete classes
 */

class Manager {

 SmartEmployee smartEmp;
 LazyEmployee lazyEmp;
 ExtraordinaryEmployee extOdEmp;  // add new employee type

 public Manager(SmartEmployee smartEmp, LazyEmployee lazyEmp, ExtraordinaryEmployee extOdEmp) {

  this.smartEmp = smartEmp;
  this.lazyEmp = lazyEmp;
  this.extOdEmp = extOdEmp;
 }

 public void ManageWork() {
  smartEmp.work();
  lazyEmp.work();
  extOdEmp.work();
 }
}

class SmartEmployee {
 public void work() {
  System.out.println("Smart Emplayee Working");
 }
}

class LazyEmployee {
 public void work() {
  System.out.println("Lazy Emplayee Working");
 }
}

// add one more class ExtraordinaryEmployee
class ExtraordinaryEmployee {
 public void work() {
  System.out.println("ExtraOrdinary Employee Working");
 }
}

public class TightCouplingExample {

 public static void main(String[] args) {

  SmartEmployee smartEmp = new SmartEmployee();
  LazyEmployee lazyEmp = new LazyEmployee();
  ExtraordinaryEmployee extOdEmp = new ExtraordinaryEmployee();
  Manager mn = new Manager(smartEmp, lazyEmp, extOdEmp);
  mn.ManageWork();
 }
}
Output:

Smart Emplayee Working
Lazy Emplayee Working
ExtraOrdinary Employee Working
you can notice that we need to modify the source code of existing manager class that mean it is tightly coupled. You saw that Manager class was modified at three places. Add extra object of extraordinary employee, modified constructor, and manageWork() method too. Which is violets OPEN CLOSED PRINCIPLE ( A class should be open for extension and closed for modification).

Loose Coupling Example:

/*
 * Loose Coupling in java example
 */

class Manager {

 Employee emp;

 public Manager(Employee emp) {
  this.emp = emp;
 }

 public void ManageWork() {
  this.emp.work();
 }
}

// Create interface Employee
interface Employee {
 void work();
}

class SmartEmployee implements Employee {
 public void work() {
  System.out.println("Smart employee working");
 }

}

class LazyEmployee implements Employee {
 public void work() {
  System.out.println("Lazy employee working");
 }

}

class ExtraordinaryEmployee implements Employee {
 public void work() {
  System.out.println("ExtraOrdinary employee working");
 }
}

public class LooseCouplingExample {

 public static void main(String[] args) {

  SmartEmployee smartEmp = new SmartEmployee();
  Manager mn = new Manager(smartEmp);
  mn.ManageWork();

  LazyEmployee lazyEmp = new LazyEmployee();
  Manager mn2 = new Manager(lazyEmp);
  mn2.ManageWork();

  ExtraordinaryEmployee extOdEmp = new ExtraordinaryEmployee();
  Manager mn3 = new Manager(extOdEmp);
  mn3.ManageWork();
 }
}
Output:
smart employee working
Lazy employee working
ExtraOrdinary employee working

You can see in the above code if we need to create any more employee type class than we don't need to modify Manager Calss (or we can say don't need to modify existing code ).

NOTE: Note that loosely coupled code should not be modified or has minimal modification. Loosely coupled code reduces the cost of maintenance.

Cohesion In Java:


It is clear from above fig. "class A" have all the responsibility in single class hence it has "Low cohension" and in other side there are different class for different responsibility(single class has single responsibility) i.e high cohesion.

Cohesion in Java. 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 responsibility, well-focused purpose. The more focused a class is, the cohesiveness of that class.

There are two types of cohesion -
Low cohesion(a bad programming design)
          low cohesive means when a class is designed to do many different operation(task) rather than focus on a single specialized operation (task). Low cohesive classes are said to be badly design class.

High Cohesion(a good programming design)

         High cohesion refers all about how a single class is designed. A class should be designed with a single responsibility, well-focused purpose. like if we design user class that should be responsible only for user management not any other task.

Note:- Low coupling and high cohesion is the best for any software development.

No comments:

Post a Comment