Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Geecon09: SOLID Design Principles


Published on

These are the slides of my Geecon09 speech about SOLID principles

Published in: Technology
  • Dating for everyone is here: ♥♥♥ ♥♥♥
    Are you sure you want to  Yes  No
    Your message goes here
  • Dating direct: ❶❶❶ ❶❶❶
    Are you sure you want to  Yes  No
    Your message goes here
  • Worth reading
    Are you sure you want to  Yes  No
    Your message goes here
  • Great presentation! Thanks Bruno :-)
    Are you sure you want to  Yes  No
    Your message goes here
  • It was a great pleasure to watch that fantastic presentation. It's hard to make an enjoyable presentation on design principle... you know... design... architecture... oh it's so boring. But no! I haven't been bored at all! Thank you.
    Are you sure you want to  Yes  No
    Your message goes here

Geecon09: SOLID Design Principles

  1. 1. SOLID Design Principles Bruno Bossola
  2. 2. About me <ul><li>C Developer since 1988
  3. 3. Java developer since 1996
  4. 4. XP Coach during 2000-2001
  5. 5. Lead coordinator and co-founder of JUG Torino in 2001
  6. 6. Sun Java Champion since 2005
  7. 7. Speaker at Javapolis and Jazoon conferences </li></ul>
  8. 8. Agenda <ul><li>What design exactly is about?
  9. 9. Bad design
  10. 10. Good design
  11. 11. SOLID principles
  12. 12. Conclusions </li></ul>
  13. 13. Warning! <ul><li>Most of those slides maps directly various articles available to the web, expecially from (thanks Bob!)
  14. 14. Note that those articles are dated...
  15. 15. 1992-1998!!! </li></ul> please don't force me to get here next year!!!!
  16. 16. Design <ul><li>What's the meaning of design?
  17. 17. What's the difference if compared to analysis? </li></ul>Analysis is about what Design is about how
  18. 18. <ul><li>Why do we need (good) design? </li></ul>Design to manage change to deal with complexity to deliver faster
  19. 19. Design <ul><li>How do we know a design is bad? </li></ul>„ Hey, this is not the way I would have done this!” „ I can't understand this piece of code!” „ WTF?” „ WTF?” „ WTF?” „ WTF??” „ WTF??” „ WTF???”
  20. 20. Design <ul><li>Ok, we probably need better criterias :)
  21. 21. Are there any „symptoms” of bad design? </li></ul>Rigidity Fragility Immobility Viscosity
  22. 22. Rigidity <ul><li>the impact of a change is unpredictable
  23. 23. every change causes a cascade of changes in dependent modules
  24. 24. a nice „two days” work become a kind of endless marathon
  25. 25. costs become unpredictable </li></ul>
  26. 26. Fragility <ul><li>the software tends to break in many places on every change
  27. 27. the breakage occurs in areas with no conceptual relationship
  28. 28. on every fix the software breaks in unexpected ways </li></ul>
  29. 29. Immobility <ul><li>it's almost impossible to reuse interesting parts of the software
  30. 30. the useful modules have too many dependencies
  31. 31. the cost of rewriting is less compared to the risk faced to separate those parts </li></ul>
  32. 32. Viscosity <ul><li>a hack is cheaper to implement than the solution within the design
  33. 33. preserving-design moves are difficult to think and to implement
  34. 34. it's much easier to do the wrong thing rather than the right one </li></ul>
  35. 35. Design <ul><li>What's the reason why a design becomes rigid, fragile, immobile, and viscous? </li></ul>improper dependencies between modules
  36. 36. Good design <ul><li>So, what are the characteristics of a good design? </li></ul>high coesion low coupling
  37. 37. Good design <ul><li>How can we achieve a good design? </li></ul>Let's go SOLID !!! S RP O CP L SP I SP D IP
  38. 39. SOLID <ul><li>An acronym of acronyms!
  39. 40. It recalls in a single word all the most important pricinple of design </li><ul><li>SRP Single Responsabiity Principle
  40. 41. OCP Open Closed Principle
  41. 42. LSP Liskov Substitution Principle
  42. 43. ISP Interface Segregation Principle
  43. 44. DIP Dependency Inversion Principle </li></ul></ul>
  44. 45. BREAK! <ul>(source: </ul><ul><li>What is the best comment in source code you have ever encountered? (part 1) </li></ul>
  45. 47. Single Responsability Principle <ul><li>A software module should have one and only one responsability
  46. 48. Easier: a software module should have one reason only to change
  47. 49. It translates directly in high coesion
  48. 50. It's usually hard to see different responsabilities </li></ul>
  49. 51. SRP <ul><li>Is SRP violated here? </li></ul>
  50. 52. SRP <ul><li>Is SRP violated here? </li></ul>
  51. 53. SRP <ul><li>identify things that are changing for different reasons
  52. 54. group together things that change for the same reason
  53. 55. note the bias compared to &quot;classical&quot; OO
  54. 56. smells? *Manager, *Controller, *Handler </li><ul><li>you really don't know how to name those
  55. 57. SRP requires very precise names, very focused classes </li></ul></ul>
  56. 59. Open Closed Principle <ul><li>Modules should be open for extension but closed to modification
  57. 60. theorized in 1998 by Bertrand Meyer in a classical OO book
  58. 61. you should be able to extend the behavior of a module without changing it! </li></ul>
  59. 62. OCP?
  60. 63. OCP?
  61. 64. OCP!
  62. 65. OCP <ul><li>Abstraction is the key!
  63. 66. Uncle Bob's recipe
  64. 67. keep the things that change frequently away from things that don't change
  65. 68. if they depend on each other, things that change frequently should depend upon things don't change </li></ul>
  66. 69. BREAK! <ul>(source: </ul><ul><li>What is the best comment in source code you have ever encountered? (part 2) </li></ul>
  67. 71. Liskov Substitution Principle <ul><li>If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o1 is substituted for o2, then S is a subtype of T.
  68. 72. uh? </li></ul>
  69. 73. LSP <ul><li>Try #2:
  70. 74. Function that use pointers or references to base classes must be able to use objects of derived classes without knowing it </li></ul>
  71. 75. LSP <ul><li>Try #3:
  72. 76. Given an entity with a behavior and some possible sub-entities that could implement the original behavior, the caller should not be surprised by anything if one of the sub entities are substituted to the original entity </li></ul>
  73. 77. LSP (by example) <ul><li>How would you model the relationship between a square and a rectangle?
  74. 78. Should the square class extends rectangle? </li></ul>
  75. 79. LSP (by example) <ul><li>Of course, isn't the Square a kind of Rectangle, after all?
  76. 80. It seems an obvious ISA </li></ul>
  77. 81. LSP (by example) <ul><li>But... what about: </li><ul><li>rectangle has two attributes, width and height: how can we deal with that?
  78. 82. how do we deal with setWidth() and setHeight() ? </li></ul><li>Is it safe? </li></ul>
  79. 83. LSP (by example) <ul><li>No, behavior is different
  80. 84. If I pass a Square to a Rectangle aware function, then this may fail as it may assume that width and heigth are managed separately
  81. 85. Geometry != Code </li></ul>
  82. 87. Interface Segregation Principle <ul><li>fat classes may happen :(
  83. 88. usually there are many clients each using a subset of the methods of such classes
  84. 89. such client classes depend upon things they don't use </li><ul><li>what happens when the big class changes? all depending modules must also change </li></ul></ul>
  85. 90. ISP <ul><li>ISP states that clients should not know about fat classes
  86. 91. instead they should rely on clean cohesive interfaces
  87. 92. you don't want to depend upon something you don't use </li></ul>
  88. 93. BREAK! <ul>(source: </ul><ul><li>What is the best comment in source code you have ever encountered? (part 3) </li></ul>
  89. 95. Dependency Inversion Principle <ul><li>High level modules should not depend upon low level modules, both should depend upon abstractions
  90. 96. Abstractions should not depend upon details, details should depend upon abstractions </li></ul>
  91. 97. DIP?
  92. 98. DIP!
  93. 99. DIP <ul><li>don't depend on anything concrete, depend only upon abstraction
  94. 100. high level modules should not be forced to change because of a change in low level / technology layers
  95. 101. drives you towards low coupling </li></ul>
  96. 102. Conclusion <ul><li>good design is needed to successfully deal with change
  97. 103. the main forces driving your design should be high cohesion and low coupling
  98. 104. SOLID principles put you on the right path
  99. 105. warning: these principles cannot be applied blindly :) </li></ul>
  100. 106. Q&A [email_address] Credits and links <ul><li>All about design principles: </li></ul><ul><li>SOLID in motivational pictures: ( </li></ul><ul><li>My original OO coach, Francesco Cirillo </li></ul>