Loose coupling between classes is the desirable state of having classes that are well encapsulated, minimize references to each other. On the other hand, cohesion is all about how a single class is designed. The term cohesion is used to indicate the degree to which a class has a single, well-focused purpose. The more focused a class is the higher its cohesiveness a good thing.
Coupling is the degree to which one class knows about another class. If the only knowledge that class A has about class B is what class B has exposed through its interface, then class A and class B are said to be loosely coupled, which is good.
If, on the other hand, class A relies on parts of class B that are not part of class B’s interface, then the coupling between the classes is tighter, and, that is not good.
In other words, if A knows more than it should about how B was implemented, then A & B are tightly coupled.
Loose coupling between software components is the desirable state of having classes that are well encapsulated, minimize references to each other.
On the other hand, cohesion is all about how a single class is designed. The term cohesion is used to indicate the degree to which a class has a single, well-focused purpose. The more focused a class is the higher its cohesiveness a good thing.
Let’s understand it with an example of how loose coupling has an advantage over tight coupling.
Now talking about loose coupling. Suppose in the future you have to change the name of Class B to ABC then you do not have to change its name in the display method of class B, just make the object of new (ABC class) and pass it to the display method in MailClass. You do not have to change anything in Class A.
Speak With Expert Engineers
Post comments