SlideShare une entreprise Scribd logo
1  sur  79
Télécharger pour lire hors ligne
SOLID
Anil Wadghule
Software Engineer, Equal Experts
awadghule@equalexperts.com
Design principles in Ruby
In the beginning your
application was perfect
Your application will change.
Then it has changed.
Modular code doesn’t mean
good design
Design is all about managing
dependencies.
Dependencies are important
Z
A
X
YA
X
Y
Z
A
B
class
subclass
Design might save you.
Unmanaged dependencies are killing
your application
What are smells of bad design?
Design smells
Design smells
Rigid
Rigid
Difficult to change.
(Every change causes too
many changes in other
parts of the system)
Design smells
Design smells
Design smells
Fragile
Fragile
Easily breakable
(Each change breaks
distant and unrelated
things)
Design smells
Design smells
Design smells
Immobile
Design smells
Immobile
Reuse is impossible
(The code is hopelessly
entangled)
Design smells
Design smells
Viscous
Design smells
Viscous
Toughness in preserving
design
(Doing things right is
harder than doing things
wrong
It did not start that way.
Why SOLID?
It helps us to write code which is
• Loosely Coupled - Dependency Injection
Why SOLID?
It helps us to write code which is
• Loosely Coupled - Dependency Injection
• Highly Cohesive - Single Responsibility
Why SOLID?
It helps us to write code which is
• Loosely Coupled - Dependency Injection
• Highly Cohesive - Single Responsibility
• Easily Composable - Can be changed
Why SOLID?
It helps us to write code which is
• 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
• 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
• 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
Robert Martin
http://www.objectmentor.com
S
O
L
I
D
Principles
Single Responsibility
O
L
I
D
Principles
Single Responsibility
Open/Closed
L
I
D
Principles
Single Responsibility
Open/Closed
Liskov Substitution
I
D
Principles
Single Responsibility
Open/Closed
Liskov Substitution
Interface Segregation
D
Principles
Single Responsibility
Open/Closed
Liskov Substitution
Interface Segregation
Dependency Inversion
Principles
Lets look at these principles in detail.
Principles
Single Responsibility
Single Responsibility
!
• A class should serve a single
purpose

Single Responsibility
!
• A class should serve a single
purpose

• There should never be more
than one reason for a class to
change.
Single Responsibility
• Generates ‘Highly cohesive’
code.
• Removes ‘Immobility Smell’
Code example.
Single Responsibility
Requirement: Client needs Feed saver application
Open/Closed
Software entities (classes/
modules, methods) should be
open for extension, but closed
for modification
Open/Closed
Open/Closed
• Helps to remove snake of ‘if-
else’ cases.
• and remove those bad switch
cases.
Code example.
Open/Closed
Requirement: Client says application should save Atom feeds
Parser
+ parse(xml)
RSS
Parser
Atom
Parser
parse
parse has been closed for modification
Open/Closed
Liskov Substitution
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
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
• Implement inheritance based on
behaviour.
• Obey the pre and postconditions rules.
Liskov Substitution
Rules
Code example.
Liskov Substitution
Lets see classic Rectangle, Square problem
Also preconditions and postconditions rules
Interface Segregation
Many client specific interfaces
are better than one general
purpose interface.
Interface Segregation
Many client specific interfaces
are better than one general
purpose interface.
Interface Segregation
Many client specific classes are
better than one general purpose
class.
Interface Segregation
Interface Segregation
• Helps for ‘Highly cohesive’
code.
• Removes ‘Immobility Smell’
Code example.
Interface Segregation
Lets see HDTV, Normal TV application
Also car, driver, mechanic app
Dependency Inversion
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.
Dependency Inversion
Copy
ReadKeyboard
char char
Dependencies downwards
WritePrinter
Dependent design
Copy
ReadKeyboard
char char
Dependent design
Dependencies downwards
Dependency Inversion
WriteDisk
Copy
Reader
Inverted Dependencies.
Dependency Inversion
ReadKeyboard
Writer
PrinterWriter
Code example.
Dependency Inversion
Lets see classic Button & Lamp application
Dependency Inversion
Downwards dependency
Button
Lamp
Dependency Inversion
Abstract Button
Button Client
Inverted Dependencies.
Dependency Inversion
Button
Lamp
Abstract
Details
Details
It’s possible to learn Software Design and aim
for good Software Design
Design because
TDD is not enough
DRY is not enough
Design because you expect your
application to succeed (and to change
in the future to come)
• Design principles — Set of guidelines
• Design patterns — Reusable solution
to commonly occurring problems
Design Principles vs Design Patterns
Abstraction is the key.
Thank you !!!
Follow me on Twitter
@anildigital
Recommended Read
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
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
Questions?

Contenu connexe

En vedette

En vedette (10)

SOLID Ruby, SOLID Rails
SOLID Ruby, SOLID RailsSOLID Ruby, SOLID Rails
SOLID Ruby, SOLID Rails
 
NoSQL and CouchDB
NoSQL and CouchDBNoSQL and CouchDB
NoSQL and CouchDB
 
Relax, it's spa time
Relax, it's spa timeRelax, it's spa time
Relax, it's spa time
 
Geecon09: SOLID Design Principles
Geecon09: SOLID Design PrinciplesGeecon09: SOLID Design Principles
Geecon09: SOLID Design Principles
 
SOLID Design principles
SOLID Design principlesSOLID Design principles
SOLID Design principles
 
Solid principles
Solid principlesSolid principles
Solid principles
 
SOLID PRINCIPLES
SOLID PRINCIPLESSOLID PRINCIPLES
SOLID PRINCIPLES
 
Refactoring Applications using SOLID Principles
Refactoring Applications using SOLID PrinciplesRefactoring Applications using SOLID Principles
Refactoring Applications using SOLID Principles
 
The SOLID Principles Illustrated by Design Patterns
The SOLID Principles Illustrated by Design PatternsThe SOLID Principles Illustrated by Design Patterns
The SOLID Principles Illustrated by Design Patterns
 
3 Things Every Sales Team Needs to Be Thinking About in 2017
3 Things Every Sales Team Needs to Be Thinking About in 20173 Things Every Sales Team Needs to Be Thinking About in 2017
3 Things Every Sales Team Needs to Be Thinking About in 2017
 

Similaire à SOLID design principles in Ruby

SOLID Design Principles
SOLID Design PrinciplesSOLID Design Principles
SOLID Design Principles
Samuel Breed
 

Similaire à SOLID design principles in Ruby (20)

Solid principles of oo design
Solid principles of oo designSolid principles of oo design
Solid principles of oo design
 
The OO Design Principles
The OO Design PrinciplesThe OO Design Principles
The OO Design Principles
 
Solid OO & Clean Coding is essential to successful Agile development
Solid OO & Clean Coding is essential to successful Agile developmentSolid OO & Clean Coding is essential to successful Agile development
Solid OO & Clean Coding is essential to successful Agile development
 
Is your code SOLID enough?
 Is your code SOLID enough? Is your code SOLID enough?
Is your code SOLID enough?
 
SOLID Design Principles for Test Automaion
SOLID Design Principles for Test AutomaionSOLID Design Principles for Test Automaion
SOLID Design Principles for Test Automaion
 
SOLID Design Principles
SOLID Design PrinciplesSOLID Design Principles
SOLID Design Principles
 
Refactoring to SOLID Code
Refactoring to SOLID CodeRefactoring to SOLID Code
Refactoring to SOLID Code
 
Design Principles
Design PrinciplesDesign Principles
Design Principles
 
android principle.pptx
android principle.pptxandroid principle.pptx
android principle.pptx
 
Schibsted Spain - Day 1 - DDD Course
Schibsted Spain - Day 1 - DDD CourseSchibsted Spain - Day 1 - DDD Course
Schibsted Spain - Day 1 - DDD Course
 
Dependency Injection, Design Principles and Patterns
Dependency Injection, Design Principles and PatternsDependency Injection, Design Principles and Patterns
Dependency Injection, Design Principles and Patterns
 
SOLID Design Principle
SOLID Design PrincipleSOLID Design Principle
SOLID Design Principle
 
Solid Principle
Solid PrincipleSolid Principle
Solid Principle
 
RSpec & Rails, an introduction
RSpec & Rails, an introductionRSpec & Rails, an introduction
RSpec & Rails, an introduction
 
Solid design principles
Solid design principlesSolid design principles
Solid design principles
 
King Tut Architecture
King Tut ArchitectureKing Tut Architecture
King Tut Architecture
 
Android architecture
Android architectureAndroid architecture
Android architecture
 
Entity Framework: To the Unit of Work Design Pattern and Beyond
Entity Framework: To the Unit of Work Design Pattern and BeyondEntity Framework: To the Unit of Work Design Pattern and Beyond
Entity Framework: To the Unit of Work Design Pattern and Beyond
 
Is your code solid
Is your code solidIs your code solid
Is your code solid
 
S.O.L.I.D xp
S.O.L.I.D xpS.O.L.I.D xp
S.O.L.I.D xp
 

Dernier

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Dernier (20)

Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 

SOLID design principles in Ruby