SlideShare uma empresa Scribd logo
1 de 39
SOLID Principles
Principles for good design
Three Presentations
1. SOLID Principles Overview
2. Test Driven Development Overview
3. Refactoring Patterns
SOLID - describes five design principles
Michael Feathers and Bob Martin were heavily
involved in formulating these principles
They help us to write testable code
by using good design principles
I'll present these as SDOLI though...
Most importantly
Read up on the principles afterwards and
investigate for yourselves.
Try some of them out
Consider the following...
Evaluating the design
Pros
●Easy to write (very imperative, like a recipe)
Cons
●Hard to test
●Hard to read
●Not reusable
Let's separate the responsibilities using:
Extract Method
Evaluating the design
We have methods with one responsibility now.
But the class still does several things
Makes it harder to test
Separate the responsibilities using
Extract Class
This wraps up our database interaction
This wraps up our email server interaction
Evaluating the design
We have separate classes with responsibilities
Much simpler
It does require us to navigate in our IDE to see
what MailSenderImpl and OrderDAOImpl do
though
Is it easier to test?
We've just demonstrated the...
Evaluation of our class
Our class is bound to specific implementations:
MailSenderImpl - what if I want to send via
SMS instead of mail?
Generalise the class by refactoring with
Extract Interface
Can inject implementations of these interfaces
Programmatically as well as with Spring!
Evaluation of our class
We've just demonstrated...
● Should be able to change the external
dependencies of the class
● Without change the class internals
● Well we can do this already!
● We used an Interface and injected our
dependencies into the class
Evaluation of our class
There are other ways to make the class
extensible:
● Dependency Injection - Inject when container starts
● Decorator and composition - Bound at compile time.
Wrap a class providing before method & after method.
● Dynamic language extensibility - Bound at runtime.
One way is to use a dynamic language or bean to
provide functionality.
Other extensibility mechanisms
This one is short because we've already done
the refactoring previously...
In conclusion
Open Closed
We previously demonstrated...
We write a Duck interface which defines a
method called swim:
public interface Duck {
void swim();
}
We're envisioning all kinds of different
implementations of this.
Consider the following...
StandardDuck
We create a class that implements this:
public class StandardDuck implements Duck {
public void swim() {
System.out.println("Off we go...");
}
}
ElectricDuck
Soon we need to provide a
new type of duck.
public class ElectricDuck implements Duck {
public void swim() {
if ( isTurnedOn() ) {
System.out.println("Off we go...");
}
}
}
This breaks our principle because of the state.
ExceptoDuck
This is also bad...
public class ExceptoDuck implements Duck {
public void swim() {
throw new IllegalStateException("No way");
}
}
This also breaks our principle.
From the pond...
Think of this from a client perspective.
If you have a getDuck() method and you call
swim() on that then you should expect it to just
work
This goes in hand with the:
Principle of Least Astonishment
● An object should be substitutable by it's:
oInterface
oBase Class
● This means that if you create a new class
and implement an interface it shouldn't do
anything surprising
● No side effects - throwing exceptions or
doing things contrary to the interface
Liskov Substitution Principle
Liskov Substitution
We've just demonstrated...
●Let's add another method to our Duck
interface because we want our duck to swim
and speak:
public interface Duck {
void swim();
void speak();
}
Interfaces...
Enter RoboDuck
public class RoboDuck implements Duck {
public void swim() {
System.out.println("Let's move creep");
}
public void speak() {
// I really really don't want to be forced
// to speak...
}
}
●We shouldn't force people to implement
things they don't want to
●We should create a separate interface called
Speakable or something...
●In short Interfaces should also have single
responsibilities as well as classes and
methods
Bringing Back SRP
Interface Segregation
We've just demonstrated...
●YAGNI - You Ain't Gonna Need It. Don't
build what you don't need today !
●Don't over-engineer abstractions, hierarchies
or genericise where you don't need to
●It's called Speculative Generality
●And it's way worse than any of the above
because it makes work harder for us
One other principle - YAGNI
●If you have abstract classes that don't do
anything - Collapse Hierarchy
●Don't delegate when you don't need to - fold
the class back in - Inline the Class
●Above all...
If someone proposes a complex design to a
problem then question it until you understand
Some refactoring advice
●Further reading on this...
●One of the single best articles out there on
bad code or code smells
http://www.codinghorror.com/blog/2006/05/code-smells.html
Coding Horror - Code Smells
●Found out about this last night...
http://sourcemaking.com/
SourceMaking.com
●Refactoring
oHow many do you know?
oHow many people use Eclipse for this?
●Test Driven Development
oDoes it help us here with the principles?
oDo people write their tests first?
oDo people use it when fixing bugs?
Discussion

Mais conteúdo relacionado

Mais procurados

principles of object oriented class design
principles of object oriented class designprinciples of object oriented class design
principles of object oriented class designNeetu Mishra
 
SOLID Design Principles applied in Java
SOLID Design Principles applied in JavaSOLID Design Principles applied in Java
SOLID Design Principles applied in JavaIonut Bilica
 
Design principles - SOLID
Design principles - SOLIDDesign principles - SOLID
Design principles - SOLIDPranalee Rokde
 
The Single Responsibility Principle
The Single Responsibility PrincipleThe Single Responsibility Principle
The Single Responsibility PrincipleLars-Erik Kindblad
 
Object Oriented Design SOLID Principles
Object Oriented Design SOLID PrinciplesObject Oriented Design SOLID Principles
Object Oriented Design SOLID Principlesrainynovember12
 
Open Close Principle
Open Close PrincipleOpen Close Principle
Open Close PrincipleThaichor Seng
 
Geecon09: SOLID Design Principles
Geecon09: SOLID Design PrinciplesGeecon09: SOLID Design Principles
Geecon09: SOLID Design PrinciplesBruno Bossola
 
Learning solid principles using c#
Learning solid principles using c#Learning solid principles using c#
Learning solid principles using c#Aditya Kumar Rajan
 
Solid design principles
Solid design principlesSolid design principles
Solid design principlesMahmoud Asadi
 
Object Oriented Concepts and Principles
Object Oriented Concepts and PrinciplesObject Oriented Concepts and Principles
Object Oriented Concepts and Principlesdeonpmeyer
 
S.O.L.I.D. Principles for Software Architects
S.O.L.I.D. Principles for Software ArchitectsS.O.L.I.D. Principles for Software Architects
S.O.L.I.D. Principles for Software ArchitectsRicardo Wilkins
 
Introducing Clean Architecture
Introducing Clean ArchitectureIntroducing Clean Architecture
Introducing Clean ArchitectureRoc Boronat
 

Mais procurados (20)

principles of object oriented class design
principles of object oriented class designprinciples of object oriented class design
principles of object oriented class design
 
Solid Principles
Solid PrinciplesSolid Principles
Solid Principles
 
SOLID Design Principles applied in Java
SOLID Design Principles applied in JavaSOLID Design Principles applied in Java
SOLID Design Principles applied in Java
 
SOLID
SOLIDSOLID
SOLID
 
SOLID Principles
SOLID PrinciplesSOLID Principles
SOLID Principles
 
Design principles - SOLID
Design principles - SOLIDDesign principles - SOLID
Design principles - SOLID
 
The Single Responsibility Principle
The Single Responsibility PrincipleThe Single Responsibility Principle
The Single Responsibility Principle
 
Clean code: SOLID
Clean code: SOLIDClean code: SOLID
Clean code: SOLID
 
Object Oriented Design SOLID Principles
Object Oriented Design SOLID PrinciplesObject Oriented Design SOLID Principles
Object Oriented Design SOLID Principles
 
Open Close Principle
Open Close PrincipleOpen Close Principle
Open Close Principle
 
Solid Principles
Solid PrinciplesSolid Principles
Solid Principles
 
Geecon09: SOLID Design Principles
Geecon09: SOLID Design PrinciplesGeecon09: SOLID Design Principles
Geecon09: SOLID Design Principles
 
Learning solid principles using c#
Learning solid principles using c#Learning solid principles using c#
Learning solid principles using c#
 
SOLID Principles
SOLID PrinciplesSOLID Principles
SOLID Principles
 
Solid design principles
Solid design principlesSolid design principles
Solid design principles
 
Domain Driven Design 101
Domain Driven Design 101Domain Driven Design 101
Domain Driven Design 101
 
Solid principles
Solid principlesSolid principles
Solid principles
 
Object Oriented Concepts and Principles
Object Oriented Concepts and PrinciplesObject Oriented Concepts and Principles
Object Oriented Concepts and Principles
 
S.O.L.I.D. Principles for Software Architects
S.O.L.I.D. Principles for Software ArchitectsS.O.L.I.D. Principles for Software Architects
S.O.L.I.D. Principles for Software Architects
 
Introducing Clean Architecture
Introducing Clean ArchitectureIntroducing Clean Architecture
Introducing Clean Architecture
 

Destaque

Presentation on SOLID design principles
Presentation on SOLID design principlesPresentation on SOLID design principles
Presentation on SOLID design principlesKostadin Golev
 
Are You a SOLID Coder?
Are You a SOLID Coder?Are You a SOLID Coder?
Are You a SOLID Coder?Steve Green
 
Solid Software Design Principles
Solid Software Design PrinciplesSolid Software Design Principles
Solid Software Design PrinciplesJon Kruger
 
Do you need microservices architecture?
Do you need microservices architecture?Do you need microservices architecture?
Do you need microservices architecture?Manu Pk
 
Solid Principles Of Design (Design Series 01)
Solid Principles Of Design (Design Series 01)Solid Principles Of Design (Design Series 01)
Solid Principles Of Design (Design Series 01)Heartin Jacob
 
Design Pattern For C# Part 1
Design Pattern For C# Part 1Design Pattern For C# Part 1
Design Pattern For C# Part 1Shahzad
 
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 PatternsHayim Makabee
 
SOLID Principles and Design Patterns
SOLID Principles and Design PatternsSOLID Principles and Design Patterns
SOLID Principles and Design PatternsGanesh Samarthyam
 
2-Software Design (Object Oriented Software Engineering - BNU Spring 2017)
2-Software Design (Object Oriented Software Engineering - BNU Spring 2017)2-Software Design (Object Oriented Software Engineering - BNU Spring 2017)
2-Software Design (Object Oriented Software Engineering - BNU Spring 2017)Hafiz Ammar Siddiqui
 
Design Patterns Illustrated
Design Patterns IllustratedDesign Patterns Illustrated
Design Patterns IllustratedHerman Peeren
 

Destaque (14)

Presentation on SOLID design principles
Presentation on SOLID design principlesPresentation on SOLID design principles
Presentation on SOLID design principles
 
Are You a SOLID Coder?
Are You a SOLID Coder?Are You a SOLID Coder?
Are You a SOLID Coder?
 
SOLID PRINCIPLES
SOLID PRINCIPLESSOLID PRINCIPLES
SOLID PRINCIPLES
 
Solid Software Design Principles
Solid Software Design PrinciplesSolid Software Design Principles
Solid Software Design Principles
 
Do you need microservices architecture?
Do you need microservices architecture?Do you need microservices architecture?
Do you need microservices architecture?
 
SOLID Principles part 1
SOLID Principles part 1SOLID Principles part 1
SOLID Principles part 1
 
Solid Principles Of Design (Design Series 01)
Solid Principles Of Design (Design Series 01)Solid Principles Of Design (Design Series 01)
Solid Principles Of Design (Design Series 01)
 
Design Pattern For C# Part 1
Design Pattern For C# Part 1Design Pattern For C# Part 1
Design Pattern For C# Part 1
 
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
 
SOLID Principles and Design Patterns
SOLID Principles and Design PatternsSOLID Principles and Design Patterns
SOLID Principles and Design Patterns
 
SOLID principles
SOLID principlesSOLID principles
SOLID principles
 
2-Software Design (Object Oriented Software Engineering - BNU Spring 2017)
2-Software Design (Object Oriented Software Engineering - BNU Spring 2017)2-Software Design (Object Oriented Software Engineering - BNU Spring 2017)
2-Software Design (Object Oriented Software Engineering - BNU Spring 2017)
 
Object Oriented Design
Object Oriented DesignObject Oriented Design
Object Oriented Design
 
Design Patterns Illustrated
Design Patterns IllustratedDesign Patterns Illustrated
Design Patterns Illustrated
 

Semelhante a SOLID principles

Mastering UI automation at Scale: Key Lessons and Best Practices (By Fernando...
Mastering UI automation at Scale: Key Lessons and Best Practices (By Fernando...Mastering UI automation at Scale: Key Lessons and Best Practices (By Fernando...
Mastering UI automation at Scale: Key Lessons and Best Practices (By Fernando...Applitools
 
Programming in Java: Getting Started
Programming in Java: Getting StartedProgramming in Java: Getting Started
Programming in Java: Getting StartedMartin Chapman
 
06 abstract-classes
06 abstract-classes06 abstract-classes
06 abstract-classesAnup Burange
 
Introduction to rails
Introduction to railsIntroduction to rails
Introduction to railsGo Asgard
 
01 - Intro To Using Java
01 - Intro To Using Java01 - Intro To Using Java
01 - Intro To Using Javathewhiteafrican
 
Hands on Exploration of Page Objects and Abstraction Layers with Selenium Web...
Hands on Exploration of Page Objects and Abstraction Layers with Selenium Web...Hands on Exploration of Page Objects and Abstraction Layers with Selenium Web...
Hands on Exploration of Page Objects and Abstraction Layers with Selenium Web...Alan Richardson
 
MobileConf 2021 Slides: Let's build macOS CLI Utilities using Swift
MobileConf 2021 Slides:  Let's build macOS CLI Utilities using SwiftMobileConf 2021 Slides:  Let's build macOS CLI Utilities using Swift
MobileConf 2021 Slides: Let's build macOS CLI Utilities using SwiftDiego Freniche Brito
 
Oop c sharp_part_1
Oop c sharp_part_1Oop c sharp_part_1
Oop c sharp_part_1shivaksn
 
Framework Design Guidelines For Brussels Users Group
Framework Design Guidelines For Brussels Users GroupFramework Design Guidelines For Brussels Users Group
Framework Design Guidelines For Brussels Users Groupbrada
 
Tdd is not about testing (OOP)
Tdd is not about testing (OOP)Tdd is not about testing (OOP)
Tdd is not about testing (OOP)Gianluca Padovani
 
Code quality
Code qualityCode quality
Code quality44ue
 
Dependency Injection and Autofac
Dependency Injection and AutofacDependency Injection and Autofac
Dependency Injection and Autofacmeghantaylor
 
Deep dive - Concourse CI/CD and Pipelines
Deep dive  - Concourse CI/CD and PipelinesDeep dive  - Concourse CI/CD and Pipelines
Deep dive - Concourse CI/CD and PipelinesSyed Imam
 

Semelhante a SOLID principles (20)

Mastering UI automation at Scale: Key Lessons and Best Practices (By Fernando...
Mastering UI automation at Scale: Key Lessons and Best Practices (By Fernando...Mastering UI automation at Scale: Key Lessons and Best Practices (By Fernando...
Mastering UI automation at Scale: Key Lessons and Best Practices (By Fernando...
 
06 abstract-classes
06 abstract-classes06 abstract-classes
06 abstract-classes
 
Programming in Java: Getting Started
Programming in Java: Getting StartedProgramming in Java: Getting Started
Programming in Java: Getting Started
 
06 abstract-classes
06 abstract-classes06 abstract-classes
06 abstract-classes
 
Recipes for Drupal distributions
Recipes for Drupal distributionsRecipes for Drupal distributions
Recipes for Drupal distributions
 
Greach 2015 Spock workshop
Greach 2015 Spock workshopGreach 2015 Spock workshop
Greach 2015 Spock workshop
 
Introduction to rails
Introduction to railsIntroduction to rails
Introduction to rails
 
01 - Intro To Using Java
01 - Intro To Using Java01 - Intro To Using Java
01 - Intro To Using Java
 
Hands on Exploration of Page Objects and Abstraction Layers with Selenium Web...
Hands on Exploration of Page Objects and Abstraction Layers with Selenium Web...Hands on Exploration of Page Objects and Abstraction Layers with Selenium Web...
Hands on Exploration of Page Objects and Abstraction Layers with Selenium Web...
 
MobileConf 2021 Slides: Let's build macOS CLI Utilities using Swift
MobileConf 2021 Slides:  Let's build macOS CLI Utilities using SwiftMobileConf 2021 Slides:  Let's build macOS CLI Utilities using Swift
MobileConf 2021 Slides: Let's build macOS CLI Utilities using Swift
 
Oop c sharp_part_1
Oop c sharp_part_1Oop c sharp_part_1
Oop c sharp_part_1
 
Core Java Tutorial
Core Java TutorialCore Java Tutorial
Core Java Tutorial
 
Framework Design Guidelines For Brussels Users Group
Framework Design Guidelines For Brussels Users GroupFramework Design Guidelines For Brussels Users Group
Framework Design Guidelines For Brussels Users Group
 
Tdd is not about testing (OOP)
Tdd is not about testing (OOP)Tdd is not about testing (OOP)
Tdd is not about testing (OOP)
 
Code quality
Code qualityCode quality
Code quality
 
Dependency Injection and Autofac
Dependency Injection and AutofacDependency Injection and Autofac
Dependency Injection and Autofac
 
Usable Software Design
Usable Software DesignUsable Software Design
Usable Software Design
 
Fewd week4 slides
Fewd week4 slidesFewd week4 slides
Fewd week4 slides
 
Spring boot
Spring bootSpring boot
Spring boot
 
Deep dive - Concourse CI/CD and Pipelines
Deep dive  - Concourse CI/CD and PipelinesDeep dive  - Concourse CI/CD and Pipelines
Deep dive - Concourse CI/CD and Pipelines
 

Mais de Jonathan Holloway

Spring boot - an introduction
Spring boot - an introductionSpring boot - an introduction
Spring boot - an introductionJonathan Holloway
 
Application design for the cloud using AWS
Application design for the cloud using AWSApplication design for the cloud using AWS
Application design for the cloud using AWSJonathan Holloway
 
Introduction to JVM languages and Fantom (very brief)
Introduction to JVM languages and Fantom (very brief)Introduction to JVM languages and Fantom (very brief)
Introduction to JVM languages and Fantom (very brief)Jonathan Holloway
 
Database migration with flyway
Database migration  with flywayDatabase migration  with flyway
Database migration with flywayJonathan Holloway
 
Introduction to using MongoDB with Ruby
Introduction to using MongoDB with RubyIntroduction to using MongoDB with Ruby
Introduction to using MongoDB with RubyJonathan Holloway
 

Mais de Jonathan Holloway (11)

The Role of the Architect
The Role of the ArchitectThe Role of the Architect
The Role of the Architect
 
Spring boot - an introduction
Spring boot - an introductionSpring boot - an introduction
Spring boot - an introduction
 
Jenkins CI presentation
Jenkins CI presentationJenkins CI presentation
Jenkins CI presentation
 
Mockito intro
Mockito introMockito intro
Mockito intro
 
Debugging
DebuggingDebugging
Debugging
 
Application design for the cloud using AWS
Application design for the cloud using AWSApplication design for the cloud using AWS
Application design for the cloud using AWS
 
Building data pipelines
Building data pipelinesBuilding data pipelines
Building data pipelines
 
Introduction to JVM languages and Fantom (very brief)
Introduction to JVM languages and Fantom (very brief)Introduction to JVM languages and Fantom (very brief)
Introduction to JVM languages and Fantom (very brief)
 
Database migration with flyway
Database migration  with flywayDatabase migration  with flyway
Database migration with flyway
 
Lightweight web frameworks
Lightweight web frameworksLightweight web frameworks
Lightweight web frameworks
 
Introduction to using MongoDB with Ruby
Introduction to using MongoDB with RubyIntroduction to using MongoDB with Ruby
Introduction to using MongoDB with Ruby
 

Último

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
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 educationjfdjdjcjdnsjd
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)Samir Dash
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
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...apidays
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityWSO2
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Zilliz
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 

Último (20)

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
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
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
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...
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
+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...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 

SOLID principles

  • 2. Three Presentations 1. SOLID Principles Overview 2. Test Driven Development Overview 3. Refactoring Patterns
  • 3. SOLID - describes five design principles Michael Feathers and Bob Martin were heavily involved in formulating these principles They help us to write testable code by using good design principles I'll present these as SDOLI though...
  • 4. Most importantly Read up on the principles afterwards and investigate for yourselves. Try some of them out
  • 5.
  • 7. Evaluating the design Pros ●Easy to write (very imperative, like a recipe) Cons ●Hard to test ●Hard to read ●Not reusable Let's separate the responsibilities using: Extract Method
  • 8.
  • 9. Evaluating the design We have methods with one responsibility now. But the class still does several things Makes it harder to test Separate the responsibilities using Extract Class
  • 10. This wraps up our database interaction This wraps up our email server interaction
  • 11. Evaluating the design We have separate classes with responsibilities Much simpler It does require us to navigate in our IDE to see what MailSenderImpl and OrderDAOImpl do though Is it easier to test?
  • 13.
  • 14. Evaluation of our class Our class is bound to specific implementations: MailSenderImpl - what if I want to send via SMS instead of mail? Generalise the class by refactoring with Extract Interface
  • 15. Can inject implementations of these interfaces Programmatically as well as with Spring! Evaluation of our class
  • 17.
  • 18. ● Should be able to change the external dependencies of the class ● Without change the class internals ● Well we can do this already! ● We used an Interface and injected our dependencies into the class Evaluation of our class
  • 19. There are other ways to make the class extensible: ● Dependency Injection - Inject when container starts ● Decorator and composition - Bound at compile time. Wrap a class providing before method & after method. ● Dynamic language extensibility - Bound at runtime. One way is to use a dynamic language or bean to provide functionality. Other extensibility mechanisms
  • 20. This one is short because we've already done the refactoring previously... In conclusion
  • 21. Open Closed We previously demonstrated...
  • 22.
  • 23. We write a Duck interface which defines a method called swim: public interface Duck { void swim(); } We're envisioning all kinds of different implementations of this. Consider the following...
  • 24. StandardDuck We create a class that implements this: public class StandardDuck implements Duck { public void swim() { System.out.println("Off we go..."); } }
  • 25. ElectricDuck Soon we need to provide a new type of duck. public class ElectricDuck implements Duck { public void swim() { if ( isTurnedOn() ) { System.out.println("Off we go..."); } } } This breaks our principle because of the state.
  • 26. ExceptoDuck This is also bad... public class ExceptoDuck implements Duck { public void swim() { throw new IllegalStateException("No way"); } } This also breaks our principle.
  • 27. From the pond... Think of this from a client perspective. If you have a getDuck() method and you call swim() on that then you should expect it to just work This goes in hand with the: Principle of Least Astonishment
  • 28. ● An object should be substitutable by it's: oInterface oBase Class ● This means that if you create a new class and implement an interface it shouldn't do anything surprising ● No side effects - throwing exceptions or doing things contrary to the interface Liskov Substitution Principle
  • 30.
  • 31. ●Let's add another method to our Duck interface because we want our duck to swim and speak: public interface Duck { void swim(); void speak(); } Interfaces...
  • 32. Enter RoboDuck public class RoboDuck implements Duck { public void swim() { System.out.println("Let's move creep"); } public void speak() { // I really really don't want to be forced // to speak... } }
  • 33. ●We shouldn't force people to implement things they don't want to ●We should create a separate interface called Speakable or something... ●In short Interfaces should also have single responsibilities as well as classes and methods Bringing Back SRP
  • 35. ●YAGNI - You Ain't Gonna Need It. Don't build what you don't need today ! ●Don't over-engineer abstractions, hierarchies or genericise where you don't need to ●It's called Speculative Generality ●And it's way worse than any of the above because it makes work harder for us One other principle - YAGNI
  • 36. ●If you have abstract classes that don't do anything - Collapse Hierarchy ●Don't delegate when you don't need to - fold the class back in - Inline the Class ●Above all... If someone proposes a complex design to a problem then question it until you understand Some refactoring advice
  • 37. ●Further reading on this... ●One of the single best articles out there on bad code or code smells http://www.codinghorror.com/blog/2006/05/code-smells.html Coding Horror - Code Smells
  • 38. ●Found out about this last night... http://sourcemaking.com/ SourceMaking.com
  • 39. ●Refactoring oHow many do you know? oHow many people use Eclipse for this? ●Test Driven Development oDoes it help us here with the principles? oDo people write their tests first? oDo people use it when fixing bugs? Discussion