This presentation is based on a blog post I made:
http://eyalgo.com/2014/02/01/the-single-responsibility-principle/
More details are in that blog post.
I had a presentation at work with these slides.
6. SOLID
• Open/closed principle
• Open for extension, but closed for
modification
• Alistair Cockburn: “…Identify points of predicted
variation and create a stable interface around them…”
6
7. SOLID
• Liskov substitution principle
• Replace objects with instances of their
subtypes without altering the correctness of
that program
Rectangle
Square
7
8. SOLID
• Interface segregation principle
• Many client-specific interfaces are better
than one general-purpose interface
8
9. SOLID
• Dependency inversion principle
• Abstractions should not depend on details
• Don’t depend on anything concrete
• Work with interfaces
9
11. Single Responsibility Principle
• Wikipedia
• …the single responsibility principle states that every class
should have a single responsibility, and that responsibility
should be entirely encapsulated by the class. All its services
should be narrowly aligned with that responsibility…
• Clean Code
• A class or module should have one, and only
one, reason to change
11
39. Conclusion
• OOD
• Clean code
• Better practice
SRP
Do 1 t h i
A
s
o
c
c l a s s
h o u l d h a v e
ne r e a s on t o
h a n g e !
39
40. Resources
• http://butunclebob.com/ArticleS.UncleBob.Principles
OfOod
• Uncle Bob about SRP
• http://www.codinghorror.com/blog/2007/03/curlyslaw-do-one-thing.html
• Coding Horror
• https://docs.google.com/file/d/0ByOwmqah_nuGNH
EtcU5OekdDMkk/
• PDF about SRP
• http://eyalgo.com/2014/02/01/the-singleresponsibility-principle/
• My blog post about SRP
40
SOLID was introduced by Robert C. Martin – Uncle BobThis are PRINCIPLES !
SRP – We’ll discuss in more details in following slides
OCPAn entity can allow its behavior to be modified without altering its source codeNew or changed features would require that a different class be createdExample: if shape is circle the area = … else if shape is square then area = …
LSPIf S is a subtype of T, then objects of type T may be replaced with objects of type SNew derived classes just extend without replacing the functionality of old classes.Square class that derives from a Rectangle classIf a Square object is used in a context where a Rectangle is expected, unexpected behavior may occur because the dimensions of a Square cannot (or rather should not) be modified independently
ISPSmall and specific interfacesClient should not be depend upon an interface it does not need ISPstates that no client should be forced to depend on methods it does not useISP is intended to keep a system decoupled
While developing, code for interfaces and not concreteI find that TDD really helps me achieve that
At each level of the system, we need to take care that any component has one responsibilitySymbol of “everything”
7+-2: Limits on Our Capacity for Processing Information Cognitive psychologist: George A. Mille
When we were young we ordered our room like the pictureOur parents taught us betterOur experience taught us even better
Change due needs of C might break B.
Refactoring is much easier for a single responsibility module
You will usually end up with longer, more precise name for your classes
Simpler tests for simple code
We’re going to see some hints (smell) of breaking the SRPBy structure and then by behavior of the code
A class should be longer than 200-250 LOC as a rule of thumb
Some tips on how to develop in order to apply the SRPMost of them will move you towards more OOD / SOLID principles
This is a general tip for being craftsman and professional
TDD will show when a class does too muchThe code which is designed by TDD will usually evolve to code that holds many principles
Enough said. Let’s finish !
If you can’t find a focused name (and handler / manager are not focused), try to make the module more focused
Refactorrefactorrefactor
OOD , Clean Code, Better practice – SRP is helping to achieve them
I chose simple items to show how they things can be extremely good when doing just one thing (+SMS)