Single Responsibility Principle
states that every object should have a single responsibility, and that responsibility should be entirely encapsulated by the class.
responsibility is defined as a reason to change, and concludes that a class or module should have one, and only one, reason to change.
- As an example, 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.
The reason Single Responsibility Principl
e is important to keep a class focused on a single concern is that it makes the class more robust.