I would like to understand the difference between separation of concerns and loose coupling.
Is it true that coding by separation of concerns gives a loosely coupled code?
Thank you.
Separation of concerns is a principle used in programming to separate an application into units, with minimal overlapping between the functions of the individual units. The separation of concerns is achieved using modularization, encapsulation and arrangement in software layers.
Tight coupling means classes and objects are dependent on one another. In general, tight coupling is usually not good because it reduces the flexibility and re-usability of the code while Loose coupling means reducing the dependencies of a class that uses the different class directly.
Loose coupling is an approach to interconnecting the components in a system or network so that those components, also called elements, depend on each other to the least extent practicable. Coupling refers to the degree of direct knowledge that one element has of another.
In a nutshell, loose coupling in Java is much better as compared to tight coupling. It provides better flexibility and reusability of code. As the two classes are independent of each other, it makes changes in the code very easy. It also provides better testability.
Coupling is about how much each component knows about other components.
Separation of concerns is about separating different aspects of functionality in different components. You can separate different aspects, but make components tight coupled (i.e. use concrete classes instead of abstractions).
UPDATE: cohesion shows how closely-related responsibilities of component. When you separate different concerns in different components, then responsibilities of component are strongly-related, thus you have high cohesion.
A helpful way to think about this is that separation of concerns dictates what each class/function should do, and coupling dictates how much these classes/functions know about each other.
Separation of concerns is related to the Single Responsibility Principle, which says that each component in your system should only be responsible for one thing.
The amount of coupling in your system relates to how each of these components talk to each other. Do they have strong knowledge of each other (tight coupling) or is their knowledge of each other abstracted away via interfaces/functional bindings/etc (loose coupling)? Loose coupling typically makes a system easier to change because changing one piece of the system doesn't affect the other components.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With