The Single Responsibility Principle


Published on

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.

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

The Single Responsibility Principle

  1. 1. The Single Responsibility PrincipleLars-Erik KindbladSenior ConsultantBlog:
  2. 2. AgendaWhat is the Single Responsibility Principle?Code SampleSummary
  3. 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, reasonto 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. 4. The God ObjectA God object is an object that knows too much or does too muchThe opposite of the SRP
  5. 5. Single Responsibility Objects
  7. 7. Database codeSend mail code
  8. 8. 1. Extract mail sending code
  9. 9. 2. Extract database code
  10. 10. Repositories are potential God-objects
  11. 11. 3. Replace Repository.GetUser with a DbCommand
  12. 12. 4. Replace Repository.AddUser with a DbCommand
  13. 13. 5. Replace Repository.DeleteUser with a DbCommand
  14. 14. UserManager looks likea facade class... Extract
  15. 15. 6. Extract RegisterUser
  16. 16. 7. Rename UserManager... to UserFacade
  17. 17. Not every dependencyare needed at all times
  18. 18. 8. Change to a Service Locator
  19. 19. 9. ValidationCould be extracted but notneeded now since the classis small...
  20. 20. OverviewAddUserDbCommandDeleteUserDbCommandGetUserDbCommandSendEmailConfirmationRegisterUserUserFacadeRemove?
  21. 21. SummaryA class following the single responsibility principle is a class that does onlyone 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, extendand testArguments against Too many classes Difficult to understand the big pictureThe pros outweights the cons – SRP is a requirement forwriting maintainable unit-tests
  22. 22. QUESTIONS?
  23. 23. The information contained in this presentation is proprietary.© 2012 Capgemini. All rights reserved.www.capgemini.comAbout CapgeminiWith more than 120,000 people in 40 countries, Capgemini is oneof the worlds foremost providers of consulting, technology andoutsourcing services. The Group reported 2011 global revenuesof EUR 9.7 billion.Together with its clients, Capgemini creates and deliversbusiness and technology solutions that fit their needs and drivethe results they want. A deeply multicultural organization,Capgemini has developed its own way of working, theCollaborative Business ExperienceTM, and draws on Rightshore ®,its worldwide delivery model.Rightshore® is a trademark belonging to Capgemini