Design smells
Immobile
Reuse is impossible
(The code is hopelessly
entangled)
Slide 20
Slide 20 text
Design smells
Slide 21
Slide 21 text
Design smells
Viscous
Toughness in preserving
design
(Doing things right is
harder than doing things
wrong
Slide 22
Slide 22 text
It did not start that way.
Slide 23
Slide 23 text
It did not start that way.
Changes killed it
Slide 24
Slide 24 text
No content
Slide 25
Slide 25 text
Why SOLID?
It helps us to write code which is
Slide 26
Slide 26 text
• Loosely Coupled - Dependency Injection
Why SOLID?
It helps us to write code which is
Slide 27
Slide 27 text
• Loosely Coupled - Dependency Injection
• Highly Cohesive - Single Responsibility
Why SOLID?
It helps us to write code which is
Slide 28
Slide 28 text
• Loosely Coupled - Dependency Injection
• Highly Cohesive - Single Responsibility
• Easily Composable - Can be changed
Why SOLID?
It helps us to write code which is
Slide 29
Slide 29 text
• Loosely Coupled - Dependency Injection
• Highly Cohesive - Single Responsibility
• Easily Composable - Can be changed
• Context Independent - Can be
rearranged
Why SOLID?
It helps us to write code which is
Slide 30
Slide 30 text
• Loosely Coupled - Dependency Injection
• Highly Cohesive - Single Responsibility
• Easily Composable - Can be changed
• Context Independent - Can be
rearranged
• Reusable
Why SOLID?
It helps us to write code which is
Slide 31
Slide 31 text
• Loosely Coupled - Dependency Injection
• Highly Cohesive - Single Responsibility
• Easily Composable - Can be changed
• Context Independent - Can be rearranged
• Reusable
• Easily testable
Why SOLID?
It helps us to write code which is
Slide 32
Slide 32 text
• Easy to maintain and extend
over time
Why SOLID?
Ultimately it helps us to write code which is
Slide 33
Slide 33 text
Robert Martin
http://www.objectmentor.com
Slide 34
Slide 34 text
Michael Feathers coined SOLID mnemonic
acronym
Slide 35
Slide 35 text
S
O
L
I
D
Principles
Slide 36
Slide 36 text
Single Responsibility
O
L
I
D
Principles
Slide 37
Slide 37 text
Single Responsibility
Open/Closed
L
I
D
Principles
Slide 38
Slide 38 text
Single Responsibility
Open/Closed
Liskov Substitution
I
D
Principles
Slide 39
Slide 39 text
Single Responsibility
Open/Closed
Liskov Substitution
Interface Segregation
D
Principles
Slide 40
Slide 40 text
Single Responsibility
Open/Closed
Liskov Substitution
Interface Segregation
Dependency Inversion
Principles
Slide 41
Slide 41 text
Lets look at these principles in
detail !
Slide 42
Slide 42 text
Single Responsibility
Slide 43
Slide 43 text
Single Responsibility
!
• A class should serve a single
purpose
• There should never be more
than one reason for a class to
change.
Slide 44
Slide 44 text
Single Responsibility
• Reveal intent
• Rename
• Introduce constant
• Introduce variable
• Extract ‘Til you drop http://j.mp/extractDrop
Achieve using following techniques
Code example.
Single Responsibility
Requirement: Client needs Feed Saver application
Slide 47
Slide 47 text
Open/Closed
Bertrand Meyer
Slide 48
Slide 48 text
Software entities (classes/
modules, methods) should be
open for extension, but closed
for modification
Open/Closed
Slide 49
Slide 49 text
Open/Closed
• Inheritance
• Composition
Achieved using
Slide 50
Slide 50 text
Open/Closed
!
• snake of ‘if-else’ cases.
• long switch cases.
Smells
Slide 51
Slide 51 text
Open/Closed
• Replace branching with delegation.
• Inject behaviour
• Extract method
• Extract class
• Wrap behaviour with abstraction
Slide 52
Slide 52 text
Code example.
Open/Closed
Requirement: Client says application should save Atom feeds
Slide 53
Slide 53 text
Parser
+ parse(xml)
RSS
Parser
Atom
Parser
parse
parse has been closed for modification
Open/Closed
Abstraction
Slide 54
Slide 54 text
Liskov Substitution
Barbara Liskov
Slide 55
Slide 55 text
Subtypes must be substitutable
for their base types.
Liskov Substitution
Let q(x) be a property provable about objects x of type T.
Then q(y) should be true for objects y of type S where S is
subtype of T
Slide 56
Slide 56 text
If a piece of client code works for a
type then it must work for all derived/
variant types.
A new subtype should not screw up
the client code.
Liskov Substitution
Slide 57
Slide 57 text
• Implement inheritance based on behaviour.
• Do not violate behaviour of parent class
• Obey the pre and postconditions rules.
Liskov Substitution
Rules
Slide 58
Slide 58 text
Code example.
Liskov Substitution
Lets see classic Rectangle, Square problem
Also we will see what are preconditions and postconditions rules
Slide 59
Slide 59 text
Interface Segregation
Slide 60
Slide 60 text
Many client specific interfaces
are better than one general
purpose interface.
Interface Segregation
Slide 61
Slide 61 text
Many client specific interfaces
are better than one general
purpose interface.
Interface Segregation
Slide 62
Slide 62 text
Many client specific modules are
better than one general purpose
module.
Interface Segregation
Slide 63
Slide 63 text
Many client specific classes are
better than one general purpose
class.
Interface Segregation
Slide 64
Slide 64 text
Clients should not be forced to
depend on methods they do not
use.
Interface Segregation
Code example.
Interface Segregation
Lets see HDTV, Normal TV app example
And car, driver, mechanic app example
Slide 67
Slide 67 text
Dependency Inversion
Slide 68
Slide 68 text
Depend on abstractions.
Do not depend on concretions.
Dependency Inversion
Slide 69
Slide 69 text
Dependency Inversion
Abstractions should not depend on details. Details
should depend on abstractions.
High-level modules should not depend on low-level modules.
Both should depend on abstractions.