Your SlideShare is downloading. ×
0
The Single Responsibility PrincipleLars-Erik KindbladSenior ConsultantBlog: kindblad.com
AgendaWhat is the Single Responsibility Principle?Code SampleSummary
Single Responsibility PrincipleSingle Responsibility Principle = SRP1 of the 5 SOLID principles for creating better obje...
The God ObjectA God object is an object that knows too much or does too muchThe opposite of the SRP
Single Responsibility Objects
CODE SAMPLE
Database codeSend mail code
1. Extract mail sending code
2. Extract database code
Repositories are potential God-objects
3. Replace Repository.GetUser with a DbCommand
4. Replace Repository.AddUser with a DbCommand
5. Replace Repository.DeleteUser with a DbCommand
UserManager looks likea facade class... Extract
6. Extract RegisterUser
7. Rename UserManager... to UserFacade
Not every dependencyare needed at all times
8. Change to a Service Locator
9. ValidationCould be extracted but notneeded now since the classis small...
OverviewAddUserDbCommandDeleteUserDbCommandGetUserDbCommandSendEmailConfirmationRegisterUserUserFacadeRemove?
SummaryA class following the single responsibility principle is a class that does onlyone thing and has only one reason t...
QUESTIONS?
The information contained in this presentation is proprietary.© 2012 Capgemini. All rights reserved.www.capgemini.comAbout...
Upcoming SlideShare
Loading in...5
×

The Single Responsibility Principle

2,671

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
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,671
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
81
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "The Single Responsibility Principle"

  1. 1. The Single Responsibility PrincipleLars-Erik KindbladSenior ConsultantBlog: kindblad.com
  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
  6. 6. CODE SAMPLE
  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
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×