Does the Single Responsibility Principle work in OOP?

前端 未结 7 1099
面向向阳花
面向向阳花 2020-12-03 15:17

I am struggling to understand how the Single Responsibility Principle can me made to work with OOP.

If we are to follow the principle to a tee, then are we not left

7条回答
  •  余生分开走
    2020-12-03 16:06

    Single Responsibility term was introduced by Rober C. Martin. Main motto of this principle is a reason to change. A class or module should have one, and only one, reason to be changed.The major benefit of this principle is that is makes the class more robust. Changes in one class or module doesn't break the other part.

    It prevents an object from becoming a God Object that is an example of Anti-Pattern. It also prevents from Ravioli code (A source code with lots of tiny, tightly-coupled objects).

    Hence, Single responsibility principle is an important part of good source code design in OOP.

提交回复
热议问题