Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

separation of concerns vs loose coupling

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.

like image 586
MoShe Avatar asked May 16 '12 14:05

MoShe


People also ask

What is meant by separation of concerns?

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.

What is the difference between close coupling and loose coupling?

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.

What does loose coupling mean?

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.

Which is better loose coupling or tight coupling?

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.


2 Answers

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.

like image 80
Sergey Berezovskiy Avatar answered Sep 28 '22 03:09

Sergey Berezovskiy


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.

like image 31
Brian Geihsler Avatar answered Sep 28 '22 03:09

Brian Geihsler