Ask a Question

write down the difference between cohesion and coupling with example.


sanchayita

on 2011-03-04 10:30:00  

Coupling is the unnecessary dependance of one component upon another component\'s implementation. An example would be if you had a Dog class that should be able to bark and jump. You could write it like this: class Dog { void doAction(int number) { if(number==1) //Jump code goes here if(number==2) //Bark code goes here } } However, this way whoever used your class would have to follow your convention that doAction(1) means jump and doAction(2) means bark. A less-coupled way would be to write separate bark() and jump() methods. Cohesion occurs when components are wired together in a smart, logical way. If you have ever used a Controller class, that\'s a great example of acheiving cohesion. Highly cohesive components interact with each other semantically, in other words, they tell each other what they want to do. In a bank example, an ATM object should be able to tell an Account object to getBalance(), or withdrawFunds(), etc. These are sensible ways for the ATM and Account to interact, not specific ways that are only useful in one program.

Dipankar

on 2011-03-04 10:30:00  

Cohesion is the measure of INTRA-MODULAR strength, that is module strength or module binding. Coupling which is the measure of strength of INTERDEPENDENCE between modules in a system. High-end cohesion and low coupling is desirable. Low coupling often correlates with high cohesion, and vice versa. Coupling talks about the inter dependencies between the various modules while cohesion describes how related functions within a module are.