Cohesion and Coupling: Two OO Design Principles

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 its 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.

Another related thoughts on Coupling.

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.



Categories: Design

Tags: , , , ,

43 replies

  1. Very good summary of cohesion and coupling

    Like

  2. Gareth,

    Thanks for the feedback!

    Like

  3. Nice article. Thank you.

    Like

  4. Thanks for giving more and understandable about coupling and cohesion…

    Like

  5. Many thanks for the clear explanation of both principles.

    Like

  6. Very Special Definitions of both never find on other links, Thanks a lot !

    Like

  7. hi thanks for explaining this 2 principles very clearly..and i feel very easy to understand

    Like

  8. txxxxxxxxx a lot a very good explanation

    Like

  9. Very well explained!! 🙂

    Like

  10. very helpful explanation…. 🙂

    Like

  11. Found this on stack overflow. Good job sanaulla.

    Like

  12. Nice, simple and straight explanation.
    Good article!

    Like

  13. Sorry, I do not understand. You did not include a simple example which clearly shows the concept. I don’t think that this answer will satisfy any knowledgeable and experienced interviewer.

    Like

  14. Copy paste for K&B, Do some real work then cheap ctrl+c ctrl+v

    Like

  15. hi brother mohamed
    i want to know how i can measure the coupling and cohesion for set of software variants for example 10 products

    Like

  16. Nice post.Really it has a value .

    Thank you sir.

    Like

  17. Mohamed, the explanation is very clear.. Thank you.. 🙂

    Like

  18. nice ….very wonder full……i and this concept loosely coupled each other

    Like

  19. Both concepts are difficult but due to this article i understand thes concepts.thankssss

    Like

  20. Best article found on the web about cohesion and coupling

    Like

  21. Hi.. good article but whats missing is . try and implement it with some code and than explain so that it will be easy for any1 to understand these concepts..

    Like

  22. was easy and quick to grasp… just need to have some code examples..

    Like

  23. Thanks for a good explanation. Became simple to understand. Coupling (Married couples will be tightly coupled (have issues) if they know non-interface stuff)

    Like

  24. easy explanations.

    Like

  25. very straight forward thank you very much

    Like

  26. Awesome explanation. Thanks a lot Mr. Sanaulla.

    Like

  27. please explain it with more examples……….

    Like

  28. Really nice Article.

    Like

  29. Nice Article! I have not came across a this kind of simple and clear explanation till now.
    Kudos to Author!

    Like

  30. Low cohesion is BAD because a class is trying to solve everything and not just its specific purposes . Many thanks for sharing this information.

    Liked by 1 person

Trackbacks

  1. Function Hell « Whathecode
  2. Another aspect of coupling in Object Oriented paradigm | Experiences Unlimited
  3. Final touch: CrapsGame program | hhAPComputerScience

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: