SlideShare a Scribd company logo
3
Most read
4
Most read
10
Most read
SOLID -  OO DESIGN PRINCIPLES Andreas Enbohm, Capgemini
Agenda What is SOLID Design Principles? Code Examples  Q&A 26 augusti 2011 Sida
SOLID Introduced by Robert C. Martins (”Uncle Bob”) Agile Manifesto Author of several books, e.g. ”Clean Code” 26 augusti 2011 Sida
SOLID SOLID -  S ingle Responsibility Principle -  O pen Closed Principle -  L iskov Substitution Principle -  I nterface Segregation Principle -  D ependency Inverison Principle Code becomes more  Testably  (remember TDD is not only about testing, more important its about Design) Apply ’smart’ - don’t do stuff ’just because of’ - very importad to see the context of the program/code when applying SOLID - Joel On Software advise – use with common sense! 26 augusti 2011 Sida
Single  Responsibility  Principle "There should never be more than one reason for a class to change." — Robert Martin, SRP paper linked from  The Principles of OOD My translation: A class should concentrate on doing one thing and one thing only 26 augusti 2011 Sida
Single Responsibility Principle Two resposibilities Connection Management + Data Communication 26 augusti 2011 Sida  interface Modem { public void dial(String pno); public void hangup(); public void send(char c); public char recv(); }
Single Responsibility Principle Separate into two interfaces 26 augusti 2011 Sida  interface DataChannel { public void send(char c); public char recv(); } interface Connection { public void dial(String phn); public char hangup(); }
Open Closed Principle "Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification." — Robert Martin paraphrasing Bertrand Meyer, OCP paper linked from  The Principles of OOD My translation: Change a class' behavior using inheritance and composition 26 augusti 2011 Sida
Open Closed Principle 26 augusti 2011 Sida  // Open-Close Principle - Bad example class GraphicEditor { public void drawShape(Shape s) { if (s.m_type==1) drawRectangle(s); else if (s.m_type==2) drawCircle(s); } public void drawCircle(Circle r) {....} public void drawRectangle(Rectangle r) {....} } class Shape { int m_type; } class Rectangle extends Shape { Rectangle() { super.m_type=1; } } class Circle extends Shape { Circle() { super.m_type=2; } }
Open Closed Principle – a Few Problems…. Impossible to add a new  Shape  without modifying  GraphEditor Important to understand  GraphEditor  to add a new  Shape Tight coupling between  GraphEditor  and  Shape Difficult to test a specific  Shape  without involving  GraphEditor If-Else-/Case  should be avoided  26 augusti 2011 Sida
Open Closed Principle - Improved // Open-Close Principle - Good example class GraphicEditor { public void drawShape(Shape s) { s.draw(); } } class Shape { abstract void draw(); } class Rectangle extends Shape { public void draw() { // draw the rectangle } }  26 augusti 2011 Sida
Liskov Substitution Principle "Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it." — Robert Martin, LSP paper linked from  The Principles of OOD My translation: Subclasses should behave nicely when used in place of their base class 26 augusti 2011 Sida
Liskov Substitution Principle 26 augusti 2011 Sida  // Violation of Liskov's  Substitution   Principle class Rectangle { int m_width; int m_height; public void setWidth(int width){ m_width = width; } public void setHeight(int h){ m_height = ht; } public int getWidth(){ return m_width; } public int getHeight(){ return m_height; } public int getArea(){ return m_width * m_height; }  } class Square extends Rectangle  { public void setWidth(int width){ m_width = width; m_height = width; } public void setHeight(int height){ m_width = height; m_height = height; } }
Liskov Substitution Principle class LspTest { private static Rectangle getNewRectangle() { // it can be an object returned by some factory ...  return new Square(); } public static void main (String args[]) { Rectangle r = LspTest.getNewRectangle(); r.setWidth(5); r.setHeight(10); // user knows that r it's a rectangle. It assumes that he's able to set the width and height as for the base class System.out.println(r.getArea()); // now he's surprised to see that the area is 100 instead of 50. } }  26 augusti 2011 Sida
Interface Segregation Principle "Clients should not be forced to depend upon interfaces that they do not use." — Robert Martin, ISP paper linked from  The Principles of OOD My translation: Keep interfaces small 26 augusti 2011 Sida
Interface Segregation Principle Don’t force classes so implement methods they can’t (Swing/Java) Don’t pollute interfaces with a lot of methods Avoid ’fat’ interfaces 26 augusti 2011 Sida
Interface Segregation Principle 26 augusti 2011 Sida  //bad example (polluted interface) interface Worker { void work(); void eat(); } ManWorker implements Worker { void work() {…}; void eat() {30 min break;}; } RobotWorker implements Worker { void work() {…}; void eat() {//Not Appliciable  for a RobotWorker}; }
Interface Segregation Principle Solution - split into two interfaces 26 augusti 2011 Sida  interface Workable { public void work(); } interface Feedable{ public void eat(); }
Dependency Inversion Principle "A. High level modules should not depend upon low level modules. Both should depend upon abstractions. B. Abstractions should not depend upon details. Details should depend upon abstractions." — Robert Martin, DIP paper linked from  The Principles of OOD My translation: Use lots of interfaces and abstractions 26 augusti 2011 Sida
Dependency Inversion Principle 26 augusti 2011 Sida  //DIP - bad example public class EmployeeService { private EmployeeFinder emFinder //concrete class, not abstract. Can access a SQL DB for instance public Employee findEmployee(…) { emFinder.findEmployee(…) } }
Dependency Inversion Principle Now its possible to change the finder to be a XmlEmployeeFinder, DBEmployeeFinder, FlatFileEmployeeFinder, MockEmployeeFinder…. 26 augusti 2011 Sida  //DIP - fixed public class EmployeeService { private IEmployeeFinder emFinder //depends on an abstraction, no an implementation public Employee findEmployee(…) { emFinder.findEmployee(…) } }
Q&A http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod http://www.oodesign.com http://www.slideshare.net/enbohm Twitter: enbohm 26 augusti 2011 Sida

More Related Content

PPTX
Solid principles
Monica Rodrigues
 
PPTX
Solid principles
Toan Nguyen
 
PDF
Solid Principles
NexThoughts Technologies
 
PPTX
SOLID principles
Jonathan Holloway
 
PPT
ImageProcessing10-Segmentation(Thresholding) (1).ppt
VikramBarapatre2
 
KEY
Solid principles
Declan Whelan
 
PPTX
Clean code: SOLID
Indeema Software Inc.
 
PDF
Clean code
Arturo Herrero
 
Solid principles
Monica Rodrigues
 
Solid principles
Toan Nguyen
 
Solid Principles
NexThoughts Technologies
 
SOLID principles
Jonathan Holloway
 
ImageProcessing10-Segmentation(Thresholding) (1).ppt
VikramBarapatre2
 
Solid principles
Declan Whelan
 
Clean code: SOLID
Indeema Software Inc.
 
Clean code
Arturo Herrero
 

What's hot (20)

KEY
SOLID Design Principles
Samuel Breed
 
PPTX
Solid design principles
Mahmoud Asadi
 
PPTX
Single Responsibility Principle
Eyal Golan
 
PDF
SOLID Design Principles applied in Java
Ionut Bilica
 
PPTX
Design principles - SOLID
Pranalee Rokde
 
PDF
Introduction to SOLID Principles
Ganesh Samarthyam
 
PPTX
SOLID Principles
Surendra Shukla
 
PPTX
Spring boot
sdeeg
 
PPTX
SOLID Principles
akbarashaikh
 
PPTX
Open Closed Principle kata
Paul Blundell
 
PPT
principles of object oriented class design
Neetu Mishra
 
PDF
Nodejs
Prem Sanil
 
PPTX
Solid Principles
humayunlkhan
 
PPTX
Learning solid principles using c#
Aditya Kumar Rajan
 
PPTX
Dependency injection - the right way
Thibaud Desodt
 
PPTX
Reactjs
Neha Sharma
 
PPTX
SOLID, DRY, SLAP design principles
Sergey Karpushin
 
PDF
Clean Architecture
Badoo
 
PPTX
Express js
Manav Prasad
 
SOLID Design Principles
Samuel Breed
 
Solid design principles
Mahmoud Asadi
 
Single Responsibility Principle
Eyal Golan
 
SOLID Design Principles applied in Java
Ionut Bilica
 
Design principles - SOLID
Pranalee Rokde
 
Introduction to SOLID Principles
Ganesh Samarthyam
 
SOLID Principles
Surendra Shukla
 
Spring boot
sdeeg
 
SOLID Principles
akbarashaikh
 
Open Closed Principle kata
Paul Blundell
 
principles of object oriented class design
Neetu Mishra
 
Nodejs
Prem Sanil
 
Solid Principles
humayunlkhan
 
Learning solid principles using c#
Aditya Kumar Rajan
 
Dependency injection - the right way
Thibaud Desodt
 
Reactjs
Neha Sharma
 
SOLID, DRY, SLAP design principles
Sergey Karpushin
 
Clean Architecture
Badoo
 
Express js
Manav Prasad
 
Ad

Similar to SOLID Design Principles (20)

PPTX
Object Oriented Principle’s
vivek p s
 
PPTX
An Introduction to the SOLID Principles
Attila Bertók
 
PPTX
Solid Principles
Hitheshh
 
PDF
L22 Design Principles
Ólafur Andri Ragnarsson
 
PPSX
Oop principles
Md. Mahedee Hasan
 
PPTX
Solid js
jonathanfmills
 
PDF
The maze of Design Patterns & SOLID Principles
Muhammad Raza
 
PPTX
The good, the bad and the SOLID
Frikkie van Biljon
 
PDF
Object Design - Part 1
Dhaval Dalal
 
PDF
Design Patterns
adil raja
 
PDF
Introduction to Object oriented Design
Amin Shahnazari
 
PPTX
Solid principles
Viet Vu
 
PPTX
Structural pattern 3
Naga Muruga
 
PPTX
Design Patterns - Part 1 of 2
Savio Sebastian
 
PDF
Pavlo Zhdanov "Mastering solid and base principles for software design"
LogeekNightUkraine
 
PPTX
L07 Design Principles
Ólafur Andri Ragnarsson
 
PDF
Solid principle
muhammadali0014
 
PPTX
Design principle vs design patterns
Prabhakar Sharma
 
PPTX
Programming for a better world
jhansi reddy
 
PDF
Software Frameworks
adil raja
 
Object Oriented Principle’s
vivek p s
 
An Introduction to the SOLID Principles
Attila Bertók
 
Solid Principles
Hitheshh
 
L22 Design Principles
Ólafur Andri Ragnarsson
 
Oop principles
Md. Mahedee Hasan
 
Solid js
jonathanfmills
 
The maze of Design Patterns & SOLID Principles
Muhammad Raza
 
The good, the bad and the SOLID
Frikkie van Biljon
 
Object Design - Part 1
Dhaval Dalal
 
Design Patterns
adil raja
 
Introduction to Object oriented Design
Amin Shahnazari
 
Solid principles
Viet Vu
 
Structural pattern 3
Naga Muruga
 
Design Patterns - Part 1 of 2
Savio Sebastian
 
Pavlo Zhdanov "Mastering solid and base principles for software design"
LogeekNightUkraine
 
L07 Design Principles
Ólafur Andri Ragnarsson
 
Solid principle
muhammadali0014
 
Design principle vs design patterns
Prabhakar Sharma
 
Programming for a better world
jhansi reddy
 
Software Frameworks
adil raja
 
Ad

More from Andreas Enbohm (9)

PPTX
BDD Short Introduction
Andreas Enbohm
 
PPTX
Behavior-driven Development and Lambdaj
Andreas Enbohm
 
PPTX
Hybrid Applications
Andreas Enbohm
 
PPTX
Software Craftsmanship
Andreas Enbohm
 
PPTX
Java7 - Top 10 Features
Andreas Enbohm
 
PPTX
Java Extension Methods
Andreas Enbohm
 
PPT
Scala
Andreas Enbohm
 
PPT
Project Lambda - Closures after all?
Andreas Enbohm
 
BDD Short Introduction
Andreas Enbohm
 
Behavior-driven Development and Lambdaj
Andreas Enbohm
 
Hybrid Applications
Andreas Enbohm
 
Software Craftsmanship
Andreas Enbohm
 
Java7 - Top 10 Features
Andreas Enbohm
 
Java Extension Methods
Andreas Enbohm
 
Project Lambda - Closures after all?
Andreas Enbohm
 

Recently uploaded (20)

PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PPTX
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PDF
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 
PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
PDF
The Future of Artificial Intelligence (AI)
Mukul
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PDF
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
PDF
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PDF
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
PDF
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
PPTX
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PPTX
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
The Future of Artificial Intelligence (AI)
Mukul
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 

SOLID Design Principles

  • 1. SOLID - OO DESIGN PRINCIPLES Andreas Enbohm, Capgemini
  • 2. Agenda What is SOLID Design Principles? Code Examples Q&A 26 augusti 2011 Sida
  • 3. SOLID Introduced by Robert C. Martins (”Uncle Bob”) Agile Manifesto Author of several books, e.g. ”Clean Code” 26 augusti 2011 Sida
  • 4. SOLID SOLID - S ingle Responsibility Principle - O pen Closed Principle - L iskov Substitution Principle - I nterface Segregation Principle - D ependency Inverison Principle Code becomes more Testably (remember TDD is not only about testing, more important its about Design) Apply ’smart’ - don’t do stuff ’just because of’ - very importad to see the context of the program/code when applying SOLID - Joel On Software advise – use with common sense! 26 augusti 2011 Sida
  • 5. Single Responsibility Principle "There should never be more than one reason for a class to change." — Robert Martin, SRP paper linked from The Principles of OOD My translation: A class should concentrate on doing one thing and one thing only 26 augusti 2011 Sida
  • 6. Single Responsibility Principle Two resposibilities Connection Management + Data Communication 26 augusti 2011 Sida interface Modem { public void dial(String pno); public void hangup(); public void send(char c); public char recv(); }
  • 7. Single Responsibility Principle Separate into two interfaces 26 augusti 2011 Sida interface DataChannel { public void send(char c); public char recv(); } interface Connection { public void dial(String phn); public char hangup(); }
  • 8. Open Closed Principle "Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification." — Robert Martin paraphrasing Bertrand Meyer, OCP paper linked from The Principles of OOD My translation: Change a class' behavior using inheritance and composition 26 augusti 2011 Sida
  • 9. Open Closed Principle 26 augusti 2011 Sida // Open-Close Principle - Bad example class GraphicEditor { public void drawShape(Shape s) { if (s.m_type==1) drawRectangle(s); else if (s.m_type==2) drawCircle(s); } public void drawCircle(Circle r) {....} public void drawRectangle(Rectangle r) {....} } class Shape { int m_type; } class Rectangle extends Shape { Rectangle() { super.m_type=1; } } class Circle extends Shape { Circle() { super.m_type=2; } }
  • 10. Open Closed Principle – a Few Problems…. Impossible to add a new Shape without modifying GraphEditor Important to understand GraphEditor to add a new Shape Tight coupling between GraphEditor and Shape Difficult to test a specific Shape without involving GraphEditor If-Else-/Case should be avoided 26 augusti 2011 Sida
  • 11. Open Closed Principle - Improved // Open-Close Principle - Good example class GraphicEditor { public void drawShape(Shape s) { s.draw(); } } class Shape { abstract void draw(); } class Rectangle extends Shape { public void draw() { // draw the rectangle } } 26 augusti 2011 Sida
  • 12. Liskov Substitution Principle "Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it." — Robert Martin, LSP paper linked from The Principles of OOD My translation: Subclasses should behave nicely when used in place of their base class 26 augusti 2011 Sida
  • 13. Liskov Substitution Principle 26 augusti 2011 Sida // Violation of Liskov's Substitution Principle class Rectangle { int m_width; int m_height; public void setWidth(int width){ m_width = width; } public void setHeight(int h){ m_height = ht; } public int getWidth(){ return m_width; } public int getHeight(){ return m_height; } public int getArea(){ return m_width * m_height; } } class Square extends Rectangle { public void setWidth(int width){ m_width = width; m_height = width; } public void setHeight(int height){ m_width = height; m_height = height; } }
  • 14. Liskov Substitution Principle class LspTest { private static Rectangle getNewRectangle() { // it can be an object returned by some factory ... return new Square(); } public static void main (String args[]) { Rectangle r = LspTest.getNewRectangle(); r.setWidth(5); r.setHeight(10); // user knows that r it's a rectangle. It assumes that he's able to set the width and height as for the base class System.out.println(r.getArea()); // now he's surprised to see that the area is 100 instead of 50. } } 26 augusti 2011 Sida
  • 15. Interface Segregation Principle "Clients should not be forced to depend upon interfaces that they do not use." — Robert Martin, ISP paper linked from The Principles of OOD My translation: Keep interfaces small 26 augusti 2011 Sida
  • 16. Interface Segregation Principle Don’t force classes so implement methods they can’t (Swing/Java) Don’t pollute interfaces with a lot of methods Avoid ’fat’ interfaces 26 augusti 2011 Sida
  • 17. Interface Segregation Principle 26 augusti 2011 Sida //bad example (polluted interface) interface Worker { void work(); void eat(); } ManWorker implements Worker { void work() {…}; void eat() {30 min break;}; } RobotWorker implements Worker { void work() {…}; void eat() {//Not Appliciable for a RobotWorker}; }
  • 18. Interface Segregation Principle Solution - split into two interfaces 26 augusti 2011 Sida interface Workable { public void work(); } interface Feedable{ public void eat(); }
  • 19. Dependency Inversion Principle "A. High level modules should not depend upon low level modules. Both should depend upon abstractions. B. Abstractions should not depend upon details. Details should depend upon abstractions." — Robert Martin, DIP paper linked from The Principles of OOD My translation: Use lots of interfaces and abstractions 26 augusti 2011 Sida
  • 20. Dependency Inversion Principle 26 augusti 2011 Sida //DIP - bad example public class EmployeeService { private EmployeeFinder emFinder //concrete class, not abstract. Can access a SQL DB for instance public Employee findEmployee(…) { emFinder.findEmployee(…) } }
  • 21. Dependency Inversion Principle Now its possible to change the finder to be a XmlEmployeeFinder, DBEmployeeFinder, FlatFileEmployeeFinder, MockEmployeeFinder…. 26 augusti 2011 Sida //DIP - fixed public class EmployeeService { private IEmployeeFinder emFinder //depends on an abstraction, no an implementation public Employee findEmployee(…) { emFinder.findEmployee(…) } }
  • 22. Q&A http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod http://www.oodesign.com http://www.slideshare.net/enbohm Twitter: enbohm 26 augusti 2011 Sida