1
50kviews
Explain Coupling and cohesion with suitable example.

Mumbai University > Information Technology > Sem 3 > Object Oriented Programming Methodology

Marks: 8 M

Year: Dec 2014

1 Answer
1
2.1kviews

Cohesion and Coupling deal with the quality of an OO design. Generally, good OO design should be loosely coupled and highly cohesive. Lot of the design principles, design patterns which have been created are based on the idea of “Loose coupling and high cohesion”.

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

  • easier to develop
  • easier to maintain
  • easier to add new features
  • Less Fragile.

Coupling:

Coupling is the degree to which one class knows about another class. Let us consider two classes class A and class B. If class A knows class B through its interface only i.e. it interacts with class B through its API then class A and class B are said to be loosely coupled.

If on the other hand class A apart from interacting class B by means of its interface also interacts through the non-interface stuff of class B then they are said to be tightly coupled. Suppose the developer changes the class B‘s non-interface part i.e. non API stuff then in case of loose coupling class A does not breakdown but tight coupling causes the class A to break.

So it’s always a good OO design principle to use loose coupling between the classes i.e. all interactions between the objects in OO system should use the APIs. An aspect of good class and API design is that classes should be well encapsulated.

// Tightly coupled class design - Bad thing     
class DoTaxes {
    float rate; 
    float doColorado() {
        SalesTaxRates str = new SalesTaxRates();
        rate = str.salesRate; // ouch
        // this should be a method call:
        // rate = str.getSalesRate("CO");
        // do stuff with rate
    }
}


class SalesTaxRates {
    public float salesRate; // should be private 
    public float adjustedSalesRate; // should be private 
    public float getSalesRate(String region) { 
        salesRate = new DoTaxes().doColorado(); // ouch again! 
        // do region-based calculations 
        return adjustedSalesRate; 
    } 
}

Ideally, all interactions between objects in an OO system should use the APIs. In other words, the contracts, of the object's respective classes. Theoretically, if all of the classes in an application have well-designed APIs, then it should be possible for all interclass interactions to use those APIs exclusively.

Cohesion:

Cohesion is used to indicate the degree to which a class has a single, well-focused purpose. Coupling is all about how classes interact with each other, on the other hand cohesion focuses on how single class is designed. Higher the cohesiveness of the class, better is the OO design.

Benefits of Higher Cohesion:

  • Highly cohesive classes are much easier to maintain and less frequently changed.
  • Such classes are more usable than others as they are designed with a well-focused purpose.
  • Single Responsibility principle aims at creating highly cohesive classes.

     // Less cohesive class design 
    
    class BudgetReport { 
    
        void connectToRDBMS() { 
    
        }
        void generateBudgetReport() { 
        } 
        void saveToFile() { 
        } 
        void print() { 
        }
    } 
    
    More cohesive class design 
    // More cohesive class design 
    class BudgetReport { 
        Options getReportingOptions() { 
        } 
        void generateBudgetReport(Options o) { 
        } 
    } 
    class ConnectToRDBMS { 
        DBconnection getRDBMS() { 
        } 
    } 
    class PrintStuff { 
        PrintOptions getPrintOptions() { 
        } 
    } 
    class FileSaver { 
        SaveOptions getFileSaveOptions() { 
        } 
    }
    

This design is much more cohesive because Instead of one class that does everything, we have broken the system into four main classes. Each with a very specific, or cohesive, role. Because we have built these specialized, reusable classes.

Please log in to add an answer.