- Back to Home »
- Single Responsibility Principle
Posted by : Sushanth
Thursday, 17 December 2015
“Every object should have a single responsibility and that responsibility should be entirely encapsulated by the class”
“Classes should have one responsibility- one reason to change”
Example 1:
Consider a module that compiles and prints a report. Such a module can be changed for two reasons. First, the content of the report can change. Second, the format of the report can change. These two things change for very different causes; one substantive, and one cosmetic. The single responsibility principle says that these two aspects of the problem are really two separate responsibilities, and should therefore be in separate classes or modules. It would be a bad design to couple two things that change for different reasons at different times.
Consider a module that compiles and prints a report. Such a module can be changed for two reasons. First, the content of the report can change. Second, the format of the report can change. These two things change for very different causes; one substantive, and one cosmetic. The single responsibility principle says that these two aspects of the problem are really two separate responsibilities, and should therefore be in separate classes or modules. It would be a bad design to couple two things that change for different reasons at different times.
Example 2:
3 responsibilities: Reader, Librarian, Database Administrator. Responsibility determined by the actor and the role he plays.
1.Reuse
2.clarity
3.naming
4.Readability
2.clarity
3.naming
4.Readability