10/26/15

Cohesion and Coupling

Hi Readers

We must have heard or someone just asked you about Coupling and Cohesion. So lets here it
You must know Cohesion and Coupling for good understanding of OO Design.

A good OO design generally have loose coupling and high cohesion. In application design our goal is to make application

  • Easy to Create
  • Easy to Maintain
  • Easy to Enhance or Extend 

Coupling


Coupling is a degree to which one class knows about the other. Lets take an example where we have Class A and B.
If class A only know things that are exposed by class B through an interface. Then they are loosely coupled, which is a good thing. On the opposite, we will call it a tight coupling if class A depends on some implementation of class B.

So there is difference in both the cases. If a developer tries to enhance class B, he/she will assume that these enhancements should not affect interface part of it. So he/she will change non-interface part and that will cause class A to break. So through this we have understood that loose coupling is good.

Example :-


class Bird{

// Other Methods
public String flyType(){
// Fly Functionality
}

}


class Duck{

// In some method
public void fun(){
Bird bird = new Bird();
String flyType = bird.fly();
}
}

In this example we have seen that Duck is using flyType of Bird class and hence it is tightly coupled. If tomorrow flyType of Bird gets enhanced then it will directly break class Duck, which is wrong.

In OO application class do interact with each other very often. Ideally this sort of interaction should happen through API. And for this API type of design, class should be well Encapsulated.

Cohesion


Coupling is about how class interact with each other, Cohesion is about how a single class is designed. Cohesion is a degree to which a single class has well-focused purpose. More focused a class is, higher its cohesiveness, which is a good thing.
Benefit from this is that such classes are much easy to maintain. And because of more focused purpose, they are more likely to be reused (Inherited).

Lets take an example

class BudgetReport {

    void connectToRDBMS(){ }

    void generateBudgetReport() { }

    void saveToFile() { }

    void print() { }

}

Suppose, as always happens, tomorrow client says, they wants to generate some more reports. They also want to give choices in database, printer and save to data files.

So we should not put all our code into one class. We should use following OO design

class BudgetReport {
    Options getReportingOptions() { }
    void generateBudgetReport(Options o) { }
}
  class ConnectToRDBMS {
    DBconnection getRDBMS() { }
}
  class PrintStuff {
    PrintOptions getPrintOptions() { }
}
  class FileSaver {
    SaveOptions getFileSaveOptions() { }
}

In later design we have broken down our one class to four classes. Each of the class is serving one specific purpose. This design is much more Cohesive.
This design is much easy to maintain and extend. So high Cohesion is good.

Always Remember for good OO design


Loose Coupling and High Cohesion.