Make it SOLID!

895 views

Published on

Os princípios SOLID surgiram para trazer as melhores práticas do desenvolvimento de softwares seguindo o paradigma de orientação a objetos. Nesta Palestra vamos entender de forma prática quais são estes princípios e como podemos utilizá-los no nosso dia-a-dia.

Published in: Technology, Business
2 Comments
12 Likes
Statistics
Notes
No Downloads
Views
Total views
895
On SlideShare
0
From Embeds
0
Number of Embeds
25
Actions
Shares
0
Downloads
10
Comments
2
Likes
12
Embeds 0
No embeds

No notes for slide

Make it SOLID!

  1. 1. MMaakkee iitt SSOOLLIIDD!! LLuuííss OOttáávviioo CCoobbuuccccii OObblloonncczzyykk @@llccoobbuuccccii
  2. 2. LLuuííss OOttáávviioo CCoobbuuccccii OObblloonncczzyykk @@llccoobbuuccccii
  3. 3. PPrriinnccííppiiooss ee vvaalloorreess
  4. 4. “Working software is the primary measure of progress.” PPrriinnccííppiiooss ee vvaalloorreess
  5. 5. “Welcome changing requirements, even late in development.” PPrriinnccííppiiooss ee vvaalloorreess
  6. 6. “Continuous attention to technical excellence and good design enhances agility.” PPrriinnccííppiiooss ee vvaalloorreess
  7. 7. NNoossssaass rreessppoonnssaabbiilliiddaaddeess??
  8. 8. NNoossssaass Traduzir desejos rreessppoonnssaabbiilliiddaaddeess??
  9. 9. NNoossssaass Traduzir desejos Escolher linguagem rreessppoonnssaabbiilliiddaaddeess??
  10. 10. Traduzir desejos Escolher linguagem Libs/frameworks NNoossssaass rreessppoonnssaabbiilliiddaaddeess??
  11. 11. Libs/frameworks NNoossssaass Traduzir desejos Escolher linguagem Definir arquitetura rreessppoonnssaabbiilliiddaaddeess??
  12. 12. Garantir qualidade Libs/frameworks NNoossssaass Traduzir desejos Escolher linguagem Definir arquitetura rreessppoonnssaabbiilliiddaaddeess??
  13. 13. CCooiissaass qquuee nnuunnccaa ooccoorrrreemm...... nnéé??
  14. 14. CCooiissaass qquuee nnuunnccaa ooccoorrrreemm...... nnéé?? Precisamos criar um Usuário com nome e idade
  15. 15. CCooiissaass qquuee nnuunnccaa ooccoorrrreemm...... nnéé?? class UserCreator { public function create($name, $age) { /*... */ } }
  16. 16. CCooiissaass qquuee nnuunnccaa ooccoorrrreemm...... nnéé?? public function __construct() { $this->conn = new PDO( 'mysql:host=127.0.0.1;dbname=awesome', 'root', 'ImAGenius' ); }
  17. 17. CCooiissaass qquuee nnuunnccaa ooccoorrrreemm...... nnéé?? public function create($name, $age) { $stm = $this->conn->prepare('...'); $stm->execute([$name, $age]); }
  18. 18. CCooiissaass qquuee nnuunnccaa ooccoorrrreemm...... nnéé?? Agora, temos que logar no arquivo /tmp/users.log, adicionando o responsável pelo cadastro
  19. 19. CCooiissaass qquuee nnuunnccaa ooccoorrrreemm...... nnéé?? class UserCreator { public function create($name, $age, $responsible) { /*... */ } }
  20. 20. CCooiissaass qquuee nnuunnccaa ooccoorrrreemm...... nnéé?? public function __construct() { // (…) $this->logger = new FileLogger(); }
  21. 21. CCooiissaass qquuee nnuunnccaa ooccoorrrreemm...... nnéé?? public function create($name, $age, $responsible) { $stm = $this->conn->prepare('...'); $stm->execute([$name, $age]); $this->logger->log(/* … */); }
  22. 22. CCooiissaass qquuee nnuunnccaa ooccoorrrreemm...... nnéé?? Meu, num dá pra imprimir o último usuário criado?
  23. 23. CCooiissaass qquuee nnuunnccaa ooccoorrrreemm...... nnéé?? class UserCreator { public function create($name, $age, $responsible) { /*... */ } public function print() { /* … */ } }
  24. 24. RReessuullttaaddoo
  25. 25. RReessuullttaaddoo Será que “APENAS funcionar” é suficiente?
  26. 26. RReessuullttaaddoo ((rreeaall))
  27. 27. BBaadd ssmmeellllss
  28. 28. BBaadd ssmmeellllss Rigidez
  29. 29. BBaadd ssmmeellllss Rigidez Fragilidade
  30. 30. BBaadd ssmmeellllss Rigidez Fragilidade Imobilidade
  31. 31. BBaadd ssmmeellllss Rigidez Fragilidade Imobilidade Complexidade desnecessária
  32. 32. PPrriinncciipplleess ooff OOOODD
  33. 33. PPrriinncciipplleess ooff OOOODD Class design
  34. 34. PPrriinncciipplleess ooff OOOODD Class design Package cohesion
  35. 35. PPrriinncciipplleess ooff OOOODD Class design Package cohesion Package coupling
  36. 36. PPrriinncciipplleess ooff OOOODD Class design Package cohesion Package coupling SS..OO..LL..II..DD..
  37. 37. SS..OO..LL..II..DD.. pprriinncciipplleess Single Responsibility principle Open/Closed principle Liskov substitution principle Interface segregation principle Dependency inversion principle
  38. 38. SS..OO..LL..II..DD.. pprriinncciipplleess Single Responsibility principle Open/Closed principle Liskov substitution principle Interface segregation principle Dependency inversion principle
  39. 39. SSiinnggllee RReessppoonnssiibbiilliittyy One does not simply have more than a reason to modify a class...
  40. 40. SSiinnggllee RReessppoonnssiibbiilliittyy A class should have one, and only one, reason to change.
  41. 41. SSiinnggllee RReessppoonnssiibbiilliittyy class UserRepository {}
  42. 42. SSiinnggllee RReessppoonnssiibbiilliittyy class UserRepository { public function findByAge($age) { /* … */ } public function findByName($name) { /* … */ } }
  43. 43. SSiinnggllee RReessppoonnssiibbiilliittyy class UserRepository { public function findByAge($age) { /* … */ } public function findByName($name) { /* … */ } public function pdfReport() { /* … */ } }
  44. 44. SSiinnggllee RReessppoonnssiibbiilliittyy class UserRepository { public function findByAge($age) { /* … */ } public function findByName($name) { /* … */ } } class UserReport { public function generate() { /* … */ } }
  45. 45. SS..OO..LL..II..DD.. pprriinncciipplleess Single Responsibility principle Open/Closed principle Liskov substitution principle Interface segregation principle Dependency inversion principle
  46. 46. OOppeenn//cclloosseedd Should be able to extend behaviors, without modifying the class
  47. 47. OOppeenn//cclloosseedd class Authenticator {}
  48. 48. OOppeenn//cclloosseedd class Authenticator { public function authenticate($login, $passwd) { /* … */ } }
  49. 49. OOppeenn//cclloosseedd class Authenticator { public function authenticate($login, $passwd) { /* … */ } private function findUser($login) { /* finds on DB */ } private function matchPasswd(User $user, $passwd) { /* … */ } }
  50. 50. OOppeenn//cclloosseedd class Authenticator { public function authenticate($login, $passwd) { /* … */ } private function findUser($login) { /* finds on DB */ } private function matchPasswd(User $user, $passwd) { /* … */ } } E se precisarmos buscar em outro lugar?
  51. 51. OOppeenn//cclloosseedd class Authenticator { public function authenticate($login, $passwd) { /* … */ } protected function findUser($login) { /* finds on DB */ } private function matchPasswd(User $user, $passwd) { /* … */ } } Assim podemos sobrescrever o método de busca...
  52. 52. SS..OO..LL..II..DD.. pprriinncciipplleess Single Responsibility principle Open/Closed principle Liskov substitution principle Interface segregation principle Dependency inversion principle
  53. 53. LLiisskkoovv ssuubbssttiittuuttiioonn Derived classes must be substitutable for their base classes
  54. 54. LLiisskkoovv ssuubbssttiittuuttiioonn class Rectangle { public function setX($x) { /* … */ } public function setY($y) { /* … */ } public function getArea() { /* … */ } }
  55. 55. LLiisskkoovv ssuubbssttiittuuttiioonn function getArea($x, $y, Rectangle $object) { $object->setX($x); $object->setY($y); return $object->getArea(); } getArea(5, 10, new Rectangle());
  56. 56. LLiisskkoovv ssuubbssttiittuuttiioonn class Square extends Rectangle { public function setX($x) { /* also setY() */ } public function setY($y) { /* also setX() */ } }
  57. 57. LLiisskkoovv ssuubbssttiittuuttiioonn function getArea($x, $y, Rectangle $object) { $object->setX($x); $object->setY($y); return $object->getArea(); } getArea(5, 10, new Square());
  58. 58. LLiisskkoovv ssuubbssttiittuuttiioonn Contravariance of method arguments in subtypes Covariance of return types in subtypes
  59. 59. LLiisskkoovv ssuubbssttiittuuttiioonn Contravariance of method arguments in subtypes Covariance of return types in subtypes Preconditions cannot be strengthened in subtypes Postconditions cannot be weakened in subtypes
  60. 60. LLiisskkoovv ssuubbssttiittuuttiioonn Contravariance of method arguments in subtypes Covariance of return types in subtypes Preconditions cannot be strengthened in subtypes Postconditions cannot be weakened in subtypes No new exceptions! Don't violate history constraint
  61. 61. SS..OO..LL..II..DD.. pprriinncciipplleess Single Responsibility principle Open/Closed principle Liskov substitution principle Interface segregation principle Dependency inversion principle
  62. 62. IInntteerrffaaccee sseeggrreeggaattiioonn Make fine grained interfaces that are client specific
  63. 63. IInntteerrffaaccee sseeggrreeggaattiioonn interface UserManager {}
  64. 64. IInntteerrffaaccee sseeggrreeggaattiioonn interface UserManager { public function create($name, $email); public function remove($id); public function find($id); public function findByName($name); }
  65. 65. IInntteerrffaaccee sseeggrreeggaattiioonn class UserController { public function find($id) { /* … */ } /** @return UserManager */ public function getManager(); }
  66. 66. IInntteerrffaaccee sseeggrreeggaattiioonn interface UserManager { public function create($name, $email); public function remove($id); } interface UserLocator { public function find($id); public function findByName($name); }
  67. 67. SS..OO..LL..II..DD.. pprriinncciipplleess Single Responsibility principle Open/Closed principle Liskov substitution principle Interface segregation principle Dependency inversion principle
  68. 68. DDeeppeennddeennccyy IInnvveerrssiioonn Depend on abstractions, not on concretions
  69. 69. DDeeppeennddeennccyy IInnvveerrssiioonn public function __construct( DatabasePersister $persister, FileNotifier $notifier ) { $this->persister = $persister; $this->notifier = $notifier; }
  70. 70. DDeeppeennddeennccyy IInnvveerrssiioonn public function __construct(Persister $persister, Notifier $notifier) { $this->persister = $persister; $this->notifier = $notifier; }
  71. 71. SSOOLLIIDD pprriinncciipplleess
  72. 72. SSOOLLIIDD pprriinncciipplleess Classes mais coesas
  73. 73. SSOOLLIIDD pprriinncciipplleess Classes mais coesas Menos acoplamento
  74. 74. SSOOLLIIDD pprriinncciipplleess Classes mais coesas Menos acoplamento Menos mudanças em código estável
  75. 75. SSOOLLIIDD pprriinncciipplleess Classes mais coesas Menos acoplamento Menos mudanças em código estável Testes mais fáceis
  76. 76. DDúúvviiddaass??
  77. 77. OObbrriiggaaddoo!! @@llccoobbuuccccii

×