Single Responsibility Principle
-
Upload
eyal-golan -
Category
Technology
-
view
1.247 -
download
0
description
Transcript of Single Responsibility Principle
Single Responsibility Principle
By Eyal Golan
Design around the…
Agenda
1. SOLID
2. SRP overview
3. Why SRP?
4. Recognizing SRP violation
5. Develop for SRP
6. Summarize
2
3
•Robert C. Martin – Uncle Bob•http://en.wikipedia.org/wiki/SOLID_(object-
oriented_design)4
Single responsibility principleOpen / closed principleLiskov substitution principleInterface segregation principleDependency inversion principle
SOLID
•Single responsibility principle•A class should have only a single
responsibility
5
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
SOLID
•Liskov substitution principle•Replace objects with instances of their
subtypes without altering the correctness of that program
7
Rectangle
Square
SOLID
•Interface segregation principle•Many client-specific interfaces are better
than one general-purpose interface
8
SOLID•Dependency inversion principle
•Abstractions should not depend on details•Don’t depend on anything concrete
•Work with interfaces
9
10
SingleResponsibilityPrinciple
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
Which Components?
•Methods•Classes•Packages•Modules•System
12
13
Why SRP ?
Why SRP?
•Organize the code
14
George A. Miller
15
Why SRP?
• A place for everything and everything in its place
Why SRP?
•Less fragile code
•Low coupling
•High cohesion
16
Why SRP?
•Easier code changes (Refactoring)
17
Why SRP?
•Easier naming•The smaller and more focused class, it will
be easier to name
18
Why SRP?
•Maintainability
•Testability and easier debugging
19
20
RecognizingSRPViolation
Recognizing By Structure
•Class / method is too long
21
Class:
LOC > 250
Bad
Recognizing By Structure
•Too many dependencies (fields / parameters)
22
ClassDependency 1
Dependency 2
Dependency 3
Dependency 4
Dependency 5
Dependency 6
Recognizing By Structure
•Low cohesion
23
Recognizing By Structure
•Description / name needs: “AND”
•Generic name: “EmployeeManager”
24
void calculateAndSend(…)
Recognizing By Structure
•A method with many levels
25
if
while
Recognizing By Behavior
•Complicated test
26
when(…).then(…); when(…).then(…);
when(…).then(…);
when(…).then(…);when(…).then(…);
when(…).then(…);
Recognizing By Behavior
•Change here, break there
•Test may be broken elsewhere
•The “shotgun effect”
27
Recognizing By Behavior
•Unable to encapsulate the module
28
29
Develop ForSRP
Develop for SRP
•Awareness•The state or ability to perceive, to feel, or to
be conscious of events, objects, or sensory patterns
30
Develop for SRP
•Testable code
•TDD
31
Test
CodeRefactor
Develop for SRP
•Code quality metrics•Coverage•SONAR
32
Develop for SRP
•Use other principles•High cohesion•Decrease coupling• Interfaces•Real encapsulation
•Law of Demeter
33
Develop for SRP
34
Keep it simple, stupid!
Keep it simple and short!
Keep it simple, short and specific!
Develop for SRP
•Naming•Think about it•Role play your entities•Longer and more focused name
35
36
Develop for SRP
• Extract method
• Extract class
Develop for SRP
•Refactor mercilessly
•Use design patterns
•Keep modularization clear
37
Example
38
Precise name(method, class)
Short class,35 lines
High cohesion
Conclusion
•OOD
•Clean code
•Better practice
39
Do 1 thingA class should have one reason to change!
SRP
Resources
• http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod• Uncle Bob about SRP
• http://www.codinghorror.com/blog/2007/03/curlys-law-do-one-thing.html• Coding Horror
• https://docs.google.com/file/d/0ByOwmqah_nuGNHEtcU5OekdDMkk/• PDF about SRP
• http://eyalgo.com/2014/02/01/the-single-responsibility-principle/• My blog post about SRP
40
Simple, Isn’t It?
41
Q & A
42