Difference between Single Responsibility Principle and Separation of Concerns

后端 未结 13 1371
-上瘾入骨i
-上瘾入骨i 2020-12-07 15:20

What is the difference between Single Responsibility Principle and Separation of Concerns?

13条回答
  •  既然无缘
    2020-12-07 15:56

    Since none of the previous answers quote Robert Martin, who created the Single Responsibility Principle, I think a more authoritative answer is needed here.

    Martin's inspiration for the SRP was drawn from David Parnas, Edsger Dijkstra (who coined the term Separation of Concerns) and Larry Constantine (who coined the terms Coupling and Cohesion). Martin consolidated their ideas into the SRP.

    Another wording for the Single Responsibility Principle is:

    Gather together the things that change for the same reasons. Separate those things that change for different reasons.

    If you think about this you’ll realize that this is just another way to define cohesion and coupling. We want to increase the cohesion between things that change for the same reasons, and we want to decrease the coupling between those things that change for different reasons.

    However, as you think about this principle, remember that the reasons for change are people. It is people who request changes. And you don’t want to confuse those people, or yourself, by mixing together the code that many different people care about for different reasons.

    To the original question, the minor difference between the SRP and SoC is that Martin refined the term concerns to refer to people.

提交回复
热议问题