The Single Responsibility Principle (SRP) is one of the 5 SOLID principles. These slides gives you an overview of the principle as well as a refactoring from a non-SRP code to a SRP-code.
3. Single Responsibility Principle
Single Responsibility Principle = SRP
1 of the 5 SOLID principles for creating better object-oriented code
The Single Responsibility Principle
The Open Closed Principle
The Liskov Substitution Principle
The Interface Segregation Principle
The Dependency Inversion Principle
SRP states that ”a class or module should have one, and only one, reason
to change”
A class should do one thing
A class should have only one responsibility
The benefit: smaller classes that are easier to read, maintain and unit-test
4. The God Object
A God object is an object that knows too much or does too much
The opposite of the SRP
21. Summary
A class following the single responsibility principle is a class that does only
one thing and has only one reason to change
The opposite of SRP is a God-object
Benefits
Easy to give the class a good name
Less code per class means reduced complexity, less errors, easier to maintain, extend
and test
Arguments against
Too many classes
Difficult to understand the big picture
The pros outweights the cons – SRP is a requirement for
writing maintainable unit-tests