SOLID design principles in Ruby
-
Upload
anil-wadghule -
Category
Technology
-
view
1.148 -
download
2
Transcript of SOLID design principles in Ruby
![Page 2: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/2.jpg)
![Page 3: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/3.jpg)
In the beginning your application was perfect
![Page 4: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/4.jpg)
Your application will change.
![Page 5: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/5.jpg)
![Page 6: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/6.jpg)
Then it has changed.
![Page 7: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/7.jpg)
Modular code doesn’t mean good design
![Page 8: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/8.jpg)
Design is all about managing dependencies.
![Page 9: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/9.jpg)
Dependencies are important
Z
A
X
YA
X
Y
Z
A
B
class
subclass
![Page 10: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/10.jpg)
Design might save you.
Unmanaged dependencies are killing your application
![Page 11: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/11.jpg)
What are smells of bad design?
![Page 12: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/12.jpg)
Design smells
![Page 13: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/13.jpg)
Design smells
Rigid
![Page 14: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/14.jpg)
Rigid Difficult to change.
(Every change causes too many changes in other
parts of the system)
Design smells
![Page 15: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/15.jpg)
Design smells
![Page 16: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/16.jpg)
Design smells
Fragile
![Page 17: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/17.jpg)
Fragile Easily breakable
(Each change breaks distant and unrelated
things)
Design smells
![Page 18: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/18.jpg)
Design smells
![Page 19: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/19.jpg)
Design smells
Immobile
![Page 20: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/20.jpg)
Design smells
Immobile Reuse is impossible
(The code is hopelessly entangled)
![Page 21: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/21.jpg)
Design smells
![Page 22: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/22.jpg)
Design smells
Viscous
![Page 23: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/23.jpg)
Design smells
Viscous Toughness in preserving
design (Doing things right is
harder than doing things wrong
![Page 24: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/24.jpg)
It did not start that way.
![Page 25: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/25.jpg)
![Page 26: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/26.jpg)
Why SOLID?It helps us to write code which is
![Page 27: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/27.jpg)
• Loosely Coupled - Dependency Injection
Why SOLID?It helps us to write code which is
![Page 28: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/28.jpg)
• Loosely Coupled - Dependency Injection
• Highly Cohesive - Single Responsibility
Why SOLID?It helps us to write code which is
![Page 29: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/29.jpg)
• Loosely Coupled - Dependency Injection
• Highly Cohesive - Single Responsibility • Easily Composable - Can be changed
Why SOLID?It helps us to write code which is
![Page 30: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/30.jpg)
• 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
![Page 31: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/31.jpg)
• 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
![Page 32: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/32.jpg)
• Loosely Coupled - Dependency Injection
• Highly Cohesive - Single Responsibility • Easily Composable - Can be changed
• Context Independent - Can be rearranged
• Reusable • Easily testable code
Why SOLID?It helps us to write code which is
![Page 34: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/34.jpg)
S O L I D
Principles
![Page 35: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/35.jpg)
Single Responsibility O L I D
Principles
![Page 36: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/36.jpg)
Single Responsibility Open/Closed L I D
Principles
![Page 37: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/37.jpg)
Single Responsibility Open/Closed Liskov Substitution I D
Principles
![Page 38: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/38.jpg)
Single Responsibility Open/Closed Liskov Substitution Interface Segregation D
Principles
![Page 39: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/39.jpg)
Single Responsibility Open/Closed Liskov Substitution Interface Segregation Dependency Inversion
Principles
![Page 40: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/40.jpg)
Lets look at these principles in detail.
Principles
![Page 41: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/41.jpg)
Single Responsibility
![Page 42: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/42.jpg)
Single Responsibility
!
• A class should serve a single purpose
![Page 43: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/43.jpg)
Single Responsibility!
• A class should serve a single purpose
• There should never be more than one reason for a class to change.
![Page 44: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/44.jpg)
Single Responsibility
• Generates ‘Highly cohesive’ code.
• Removes ‘Immobility Smell’
![Page 45: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/45.jpg)
Code example.
Single Responsibility
Requirement: Client needs Feed saver application
![Page 46: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/46.jpg)
Open/Closed
![Page 47: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/47.jpg)
Software entities (classes/modules, methods) should be open for extension, but closed for modification
Open/Closed
![Page 48: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/48.jpg)
Open/Closed• Helps to remove snake of ‘if-
else’ cases. • and remove those bad switch
cases.
![Page 49: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/49.jpg)
Code example.
Open/Closed
Requirement: Client says application should save Atom feeds
![Page 50: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/50.jpg)
Parser
+ parse(xml)
RSS Parser
Atom Parser
parse
parse has been closed for modification
Open/Closed
![Page 51: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/51.jpg)
Liskov Substitution
![Page 52: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/52.jpg)
Subclasses should be substitutable for their base classes.
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
![Page 53: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/53.jpg)
If a piece of client code works for a type then it must work for all derived types. A new subtype should not screw up the client code.
Liskov Substitution
![Page 54: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/54.jpg)
• Implement inheritance based on behaviour.
• Obey the pre and postconditions rules.
Liskov Substitution
Rules
![Page 55: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/55.jpg)
Code example.
Liskov Substitution
Lets see classic Rectangle, Square problem
Also preconditions and postconditions rules
![Page 56: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/56.jpg)
Interface Segregation
![Page 57: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/57.jpg)
Many client specific interfaces are better than one general purpose interface.
Interface Segregation
![Page 58: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/58.jpg)
Many client specific interfaces are better than one general purpose interface.
Interface Segregation
![Page 59: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/59.jpg)
Many client specific classes are better than one general purpose class.
Interface Segregation
![Page 60: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/60.jpg)
Interface Segregation
• Helps for ‘Highly cohesive’ code.
• Removes ‘Immobility Smell’
![Page 61: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/61.jpg)
Code example.
Interface Segregation
Lets see HDTV, Normal TV application
Also car, driver, mechanic app
![Page 62: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/62.jpg)
Dependency Inversion
![Page 63: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/63.jpg)
Depend on abstractions. Do not depend on concretions.
Dependency Inversion
A. High-level modules should not depend on low-level modules. Both should depend on abstractions. B. Abstractions should not depend on details. Details should depend on abstractions.
![Page 64: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/64.jpg)
Dependency InversionCopy
ReadKeyboard
char char
Dependencies downwards
WritePrinter
Dependent design
![Page 65: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/65.jpg)
Copy
ReadKeyboard
char char
Dependent design
Dependencies downwards
Dependency Inversion
WriteDisk
![Page 66: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/66.jpg)
Copy
Reader
Inverted Dependencies.
Dependency Inversion
ReadKeyboard
Writer
PrinterWriter
![Page 67: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/67.jpg)
Code example.
Dependency Inversion
Lets see classic Button & Lamp application
![Page 68: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/68.jpg)
Dependency Inversion
Downwards dependency
Button
Lamp
Dependency Inversion
![Page 69: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/69.jpg)
Abstract Button
Button Client
Inverted Dependencies.
Dependency InversionButton
Lamp
Abstract
Details
Details
![Page 70: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/70.jpg)
![Page 71: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/71.jpg)
It’s possible to learn Software Design and aim for good Software Design
![Page 72: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/72.jpg)
Design becauseTDD is not enough
DRY is not enough
Design because you expect your application to succeed (and to change in the future to come)
![Page 73: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/73.jpg)
• Design principles — Set of guidelines
• Design patterns — Reusable solution to commonly occurring problems
Design Principles vs Design Patterns
![Page 74: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/74.jpg)
Abstraction is the key.
![Page 75: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/75.jpg)
Thank you !!!Follow me on Twitter
@anildigital
![Page 76: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/76.jpg)
Recommended Read
![Page 77: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/77.jpg)
http://www.flickr.com/photos/scjn/3586487445
http://www.flickr.com/photos/51241173@N03/8083645853
http://www.flickr.com/photos/wouterrietberg/12076192934
http://www.flickr.com/photos/clonedmilkmen/3604999084
http://lostechies.com/wp-content/uploads/2011/03/pablos_solid_ebook.pdf
Photo credits
![Page 78: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/78.jpg)
Sandi Metz - SOLID Object-Oriented Design talk 2009
Clean Coders Videos - Uncle Bob Martin
SOLID Ruby - Jim Weirich - Ruby Conference 2009
https://github.com/kevinbuch/solid for examples (ISP, DIP)
Pablo's SOLID Software Development - http://lostechies.com/wp-content/uploads/2011/03/pablos_solid_ebook.pdf
http://blog.groupbuddies.com/posts/19-solid-principles-in-ruby for examples (ISP)
http://www.codeproject.com/Articles/613304/SOLID-Principles-The-Liskov-Principle-What-Why-and for examples (LSP)
References
![Page 79: SOLID design principles in Ruby](https://reader034.fdocuments.in/reader034/viewer/2022052307/554f8655b4c905d25b8b4ccb/html5/thumbnails/79.jpg)
Questions?