Don't be STUPID, Grasp SOLID - North East PHP

30,676 views
30,411 views

Published on

Object Oriented Design talk at North East PHP

Published in: Technology
2 Comments
24 Likes
Statistics
Notes
No Downloads
Views
Total views
30,676
On SlideShare
0
From Embeds
0
Number of Embeds
272
Actions
Shares
0
Downloads
92
Comments
2
Likes
24
Embeds 0
No embeds

No notes for slide

Don't be STUPID, Grasp SOLID - North East PHP

  1. 1. Don’t Be STUPID Grasp SOLID! Anthony Ferrara NorthEastPHP 2013
  2. 2. Let’s Talk Object Oriented Programming
  3. 3. What Is An Object?
  4. 4. Classic View Object == Physical “Thing”
  5. 5. Classic View Object == Physical “Thing” Methods == Actions on “Thing”
  6. 6. Classic View Object == Physical “Thing” Methods == Actions on “Thing” Properties == Description of “Thing”
  7. 7. Animal MammalBird Fish CatCow Dog Lion Feline Cheetah
  8. 8. Classic View $lion = new Lion; $lion->roar(); $lion->walkTo($point); $lion->hunt($zebra); $lion->sleep();
  9. 9. Is This Realistic?
  10. 10. Classic View $lion = new Lion; $lion->roar(); $lion->walkTo($point); $lion->hunt($zebra); $lion->sleep();
  11. 11. Classic View $lion = new Lion; $lion->roar(); $lion->walkTo($point); $lion->hunt($zebra); $lion->sleep(); Does A Lion Have A Button To Make It Roar?
  12. 12. Classic View $lion = new Lion; $lion->roar(); $lion->walkTo($point); $lion->hunt($zebra); $lion->sleep(); Does A Lion Have A Button To Make It Roar?What Does It Mean For An Object To “Hunt”?
  13. 13. Classic View $lion = new Lion; $lion->roar(); $lion->walkTo($point); $lion->hunt($zebra); $lion->sleep(); Does A Lion Have A Button To Make It Roar?What Does It Mean For An Object To “Hunt”? What Is A Lion In Relation To Our Application?
  14. 14. The Classical Model Is Easy To Understand
  15. 15. The Classical Model Is Completely Impractical
  16. 16. “Modern” View Object == Collection Of (Related) Behaviors
  17. 17. “Modern” View Object == Collection Of (Related) Behaviors Methods == Behavior
  18. 18. “Modern” View Object == Collection Of (Related) Behaviors Methods == Behavior Properties == Details Of Behavior
  19. 19. Classic View == “(conceptually) is a” Modern View == “behaves as a”
  20. 20. interface Number { function getValue(); function __toString(); function add(Number $n); function subtract(Number $n); function equals(Number $n); function isLessThan(Number $n); function isGreaterThan(Number $n); }
  21. 21. Number IntegerFloat Decimal longshort long long unsigned signed
  22. 22. But Wait!
  23. 23. We Don’t Even Need Inheritance
  24. 24. All We Need Is Polymorphism And Encapsulation
  25. 25. Polymorphism Behavior Is Determined Dynamically “Dynamic Dispatch”
  26. 26. Procedural Code if ($a instanceof Long) { return new Long($a->getValue() + 1); } elseif ($a instanceof Float) { return new Float($a->getValue() + 1.0); } elseif ($a instanceof Decimal) { return new Decimal($a->getValue() + 1.0); }
  27. 27. Polymorphic Code return $int->add(new Integer(1));
  28. 28. Polymorphic Code class Integer implements Number { public function add(Number $a) { return new Integer( $this->getValue() + (int) $a->getValue() ); } }
  29. 29. Polymorphic Code class Float implements Number { public function add(Number $a) { return new Float( $this->getValue() + (float) $a->getValue() ); } }
  30. 30. Encapsulation Behavior Is Completely Contained By The Object’s API (Information Hiding)
  31. 31. Procedural Code if (5 == $number->value) { print “Number Is 5”; } else { print “Number Is Not 5”; }
  32. 32. Encapsulated Code if ($number->equals(new Integer(5))) { print “Number Is 5”; } else { print “Number Is Not 5”; }
  33. 33. Encapsulated Code class Decimal implements Number { protected $intValue; protected $exponent; public function equals(Number $a) { if ($a instanceof Decimal) { // Compare Directly } else { // Cast } } }
  34. 34. Behavior Is Defined By The API
  35. 35. Two Types Of Primitive APIs Interfaces (Explicit)
  36. 36. Two Types Of Primitive APIs Interfaces (Explicit) Duck Typing (Implicit)
  37. 37. If an Object Is A Collection Of Behaviors...
  38. 38. What Is A Collection Of Classes/Objects?
  39. 39. APIs Method
  40. 40. APIs Method MethodMethod Class
  41. 41. APIs Method MethodMethod Class ClassClass Package
  42. 42. APIs Method MethodMethod Class ClassClass Package PackagePackage Library
  43. 43. APIs Method MethodMethod Class ClassClass Package PackagePackage Library Framework LibraryLibrary
  44. 44. What Makes A Good API?
  45. 45. A Good API Does One Thing
  46. 46. A Good API Never Changes
  47. 47. A Good API Behaves Like Its Contract
  48. 48. A Good API Has A Narrow Responsibility
  49. 49. A Good API Depends Upon Abstractions
  50. 50. And That’s SOLID Code
  51. 51. S - Single Responsibility Principle O- L - I - D- A Good API Does One Thing
  52. 52. S - Single Responsibility Principle O- Open / Closed Principle L - I - D- A Good API Never Changes
  53. 53. S - Single Responsibility Principle O- Open / Closed Principle L - Liskov Substitution Principle I - D- A Good API Behaves Like Its Contract
  54. 54. S - Single Responsibility Principle O- Open / Closed Principle L - Liskov Substitution Principle I - Interface Segregation Principle D- A Good API Has A Narrow Responsibility
  55. 55. S - Single Responsibility Principle O- Open / Closed Principle L - Liskov Substitution Principle I - Interface Segregation Principle D- Dependency Inversion Principle A Good API Depends Upon Abstractions
  56. 56. S - Single Responsibility Principle O- Open / Closed Principle L - Liskov Substitution Principle I - Interface Segregation Principle D- Dependency Inversion Principle
  57. 57. Note That SOLID Does Not Dictate What Is Good OOP
  58. 58. SOLID Emerges From Good OOP
  59. 59. So, What Makes A Bad API?
  60. 60. Global Variables (Spooky Action At A Distance)
  61. 61. Depending On Specifics Of An Implementation
  62. 62. Hidden Dependencies
  63. 63. Unhealthy Focus On Performance
  64. 64. Poorly Named APIs
  65. 65. Duplication
  66. 66. And That’s STUPID Code
  67. 67. S - Singletons T - U - P - I - D- Global Variables (Spooky Action At A Distance)
  68. 68. S - Singletons T - Tight Coupling U - P - I - D- Depending On Specifics Of An Implementation
  69. 69. S - Singletons T - Tight Coupling U - Untestable Code P - I - D- Hidden Dependencies
  70. 70. S - Singletons T - Tight Coupling U - Untestable Code P - Premature Optimization I - D- Unhealthy Focus On Performance
  71. 71. S - Singletons T - Tight Coupling U - Untestable Code P - Premature Optimization I - Indescriptive Naming D- Poorly Named APIs
  72. 72. S - Singletons T - Tight Coupling U - Untestable Code P - Premature Optimization I - Indescriptive Naming D- Duplication Duplication Duplication DuplicationDuplication Duplication Duplication Duplication Duplication
  73. 73. S - Singletons T - Tight Coupling U - Untestable Code P - Premature Optimization I - Indescriptive Naming D- Duplication
  74. 74. STUPID Embodies Lessons Learned From Bad OOP
  75. 75. What Good OOP Gives You Modular Code Reusable Code Extendable Code Easy To Read Code Maintainable Code Easy To Change Code Easy To Understand Code Clean Abstractions (mostly)
  76. 76. What Good OOP Costs You Tends To Have More “Layers” Tends To Be Slower At Runtime Tends To Have Larger Codebases Tends To Result In Over-Abstraction Tends To Require More Effort To Write Tends To Require More Tacit Knowledge
  77. 77. Let’s Look At Some Code!
  78. 78. interface Car { public function turnLeft(); public function turnRight(); public function goFaster(); public function goSlower(); public function shiftUp(); public function shiftDown(); public function start(); }
  79. 79. interface Steerable { public function steer($angle); } interface Acceleratable { public function accelerate($amt); } interface Shiftable { public function shiftDown(); public function shiftUp(); }
  80. 80. interface Transmission { public function getNumberOfGears(); public function getGear(); public function shiftToNeutral(); public function shiftToReverse(); public function shiftToGear($gear); } interface Manual extends Transmission { public function engageClutch(); public function releaseClutch(); } interface Automatic extends Transmission { public function shiftToDrive(); }
  81. 81. Principle Of Good Enough
  82. 82. Anthony Ferrara joind.in/8907 @ircmaxell blog.ircmaxell.com me@ircmaxell.com youtube.com/ircmaxell

×