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.

Implementing The Open/Closed Principle

1,648 views

Published on

Published in: Technology
  • Be the first to comment

Implementing The Open/Closed Principle

  1. 1. Implementing The Open/Closed Principle
  2. 2. Sam Hennessy • Originally from the UK • Now live in Denver, CO • Software Architect at i3logix • Ex-Pro Services Consultant For Zend • Regular at Front Range PHP Users Group2
  3. 3. i3logix3
  4. 4. What is it? Part of SOLID Protect your system from change Do you already do it?4
  5. 5. SOLID S Single responsibility principle O Open/closed principle L Liskov substitution principle I Interface segregation principle5 D Dependency inversion principle
  6. 6. Open/Closed Principle "Be open for extension, but closed for modification."6
  7. 7. And I Care Why? Maintenance Let your Customer users bridge a customization feature gap Competitive Preventing edge the "FORK"! A-la-cart feature offerings7
  8. 8. What about YAGNI and KISS?8
  9. 9. Where Can It Be Applied? Class Application Enterprise9
  10. 10. Classes
  11. 11. Classes Inheritance Polymorphism Interfaces11
  12. 12. Small Methods12
  13. 13. No Public Properties Always be private? Encapsulation Information hiding Its cheap to do13
  14. 14. Globals and OCP14
  15. 15. De-coupling15
  16. 16. class Calculator{ public function __invoke($calc, $left, $right) { if ($calc == add) { return $left + $right; } else if ($calc == sub) { return $left - $right; } }}
  17. 17. class Calculator{ public function __invoke(Calc $calc, $left, $right){ return $calc($left, $right); }}$calculator = new Calculator();echo $calculator(new AddCalc(), 2, 1), "n";echo $calculator(new SubCalc(), 2, 1), "n";
  18. 18. interface Calc { public function __invoke($left, $right);}class AddCalc implements Calc { public function __invoke($left, $right) { return $left + $right; }}class SubCalc implements Calc { public function __invoke($left, $right) { return $left - $right; }}
  19. 19. if ($logType == echo) { echo $message;} else if ($logType == file) { $logger->writeToFile($message);}
  20. 20. Composition Over Inheritance20
  21. 21. Behavior vs. Dependencies21
  22. 22. Application
  23. 23. Plugins23
  24. 24. Service Locator24
  25. 25. Dependency Injection25
  26. 26. Event Driven Programming26
  27. 27. Aspect Oriented Programming27
  28. 28. Rules Engine28
  29. 29. Build Process29
  30. 30. Enterprise
  31. 31. Public API31
  32. 32. Service Oriented Architecture (SOA)32
  33. 33. Global Broadcast33
  34. 34. When to break OCP!?
  35. 35. When to break OCP!? Separation of concerns Excessive Single duplication responsibility35
  36. 36. Everyday vs. the Revolutionary Facts dont Feature fit the doesnt fit theory the design36
  37. 37. What’s Your Strategy?
  38. 38. Questions?
  39. 39. Thanks You!

×