Download - Make it SOLID!
![Page 1: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/1.jpg)
Make itMake itSOLID!SOLID!
Luís Otávio Cobucci OblonczykLuís Otávio Cobucci Oblonczyk@lcobucci@lcobucci
![Page 2: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/2.jpg)
Luís OtávioLuís OtávioCobucci OblonczykCobucci Oblonczyk
@lcobucci@lcobucci
![Page 3: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/3.jpg)
PrincípiosPrincípiosee valoresvalores
![Page 4: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/4.jpg)
PrincípiosPrincípiosee valoresvalores
“Working software is the primarymeasure of progress.”
![Page 5: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/5.jpg)
PrincípiosPrincípiosee valoresvalores
“Welcome changing requirements,even late in development.”
![Page 6: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/6.jpg)
PrincípiosPrincípiosee valoresvalores
“Continuous attention to technical excellence and good design enhances agility.”
![Page 7: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/7.jpg)
NossasNossasresponsabilidades?responsabilidades?
![Page 8: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/8.jpg)
NossasNossasresponsabilidades?responsabilidades?
Traduzir desejos
![Page 9: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/9.jpg)
NossasNossasresponsabilidades?responsabilidades?
Escolher linguagem
Traduzir desejos
![Page 10: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/10.jpg)
NossasNossasresponsabilidades?responsabilidades?
Escolher linguagem Libs/frameworks
Traduzir desejos
![Page 11: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/11.jpg)
NossasNossasresponsabilidades?responsabilidades?
Escolher linguagem
Definir arquitetura
Libs/frameworks
Traduzir desejos
![Page 12: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/12.jpg)
NossasNossasresponsabilidades?responsabilidades?
Escolher linguagem
Garantir qualidade
Definir arquitetura
Libs/frameworks
Traduzir desejos
![Page 13: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/13.jpg)
Coisas que nuncaCoisas que nuncaocorrem... né?ocorrem... né?
![Page 14: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/14.jpg)
Coisas que nuncaCoisas que nuncaocorrem... né?ocorrem... né?
Precisamos criar umUsuário com nome e idade
![Page 15: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/15.jpg)
class UserCreator{ public function create($name, $age) { /*... */ }}
Coisas que nuncaCoisas que nuncaocorrem... né?ocorrem... né?
![Page 16: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/16.jpg)
public function __construct() { $this->conn = new PDO( 'mysql:host=127.0.0.1;dbname=awesome', 'root', 'ImAGenius' );}
Coisas que nuncaCoisas que nuncaocorrem... né?ocorrem... né?
![Page 17: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/17.jpg)
public function create($name, $age) { $stm = $this->conn->prepare('...'); $stm->execute([$name, $age]);}
Coisas que nuncaCoisas que nuncaocorrem... né?ocorrem... né?
![Page 18: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/18.jpg)
Coisas que nuncaCoisas que nuncaocorrem... né?ocorrem... né?
Agora, temos que logar no arquivo /tmp/users.log, adicionandoo responsável pelo cadastro
![Page 19: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/19.jpg)
class UserCreator{ public function create($name, $age, $responsible) { /*... */ }}
Coisas que nuncaCoisas que nuncaocorrem... né?ocorrem... né?
![Page 20: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/20.jpg)
public function __construct() { // (…) $this->logger = new FileLogger();}
Coisas que nuncaCoisas que nuncaocorrem... né?ocorrem... né?
![Page 21: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/21.jpg)
public function create($name, $age, $responsible) { $stm = $this->conn->prepare('...'); $stm->execute([$name, $age]);
$this->logger->log(/* … */);}
Coisas que nuncaCoisas que nuncaocorrem... né?ocorrem... né?
![Page 22: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/22.jpg)
Coisas que nuncaCoisas que nuncaocorrem... né?ocorrem... né?
Meu, num dá pra imprimiro último usuário criado?
![Page 23: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/23.jpg)
class UserCreator{ public function create($name, $age, $responsible) { /*... */ } public function print() { /* … */ }}
Coisas que nuncaCoisas que nuncaocorrem... né?ocorrem... né?
![Page 24: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/24.jpg)
ResultadoResultado
![Page 25: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/25.jpg)
ResultadoResultado
Será que “APENAS funcionar” é suficiente?
![Page 26: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/26.jpg)
ResultadoResultado(real)(real)
![Page 27: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/27.jpg)
Bad smellsBad smells
![Page 28: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/28.jpg)
Bad smellsBad smells
Rigidez
![Page 29: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/29.jpg)
Bad smellsBad smells
Rigidez
Fragilidade
![Page 30: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/30.jpg)
Bad smellsBad smells
Rigidez
FragilidadeImobilidade
![Page 31: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/31.jpg)
Bad smellsBad smells
Rigidez
FragilidadeImobilidade
Complexidadedesnecessária
![Page 32: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/32.jpg)
Principles of OODPrinciples of OOD
![Page 33: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/33.jpg)
Principles of OODPrinciples of OOD
Class design
![Page 34: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/34.jpg)
Principles of OODPrinciples of OOD
Class designPackage cohesion
![Page 35: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/35.jpg)
Principles of OODPrinciples of OOD
Class design
Package coupling
Package cohesion
![Page 36: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/36.jpg)
Principles of OODPrinciples of OOD
Class design
Package coupling
Package cohesion
S.O.L.I.D.S.O.L.I.D.
![Page 37: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/37.jpg)
Single Responsibility principle
Open/Closed principle
Liskov substitution principle
Interface segregation principle
Dependency inversion principle
S.O.L.I.D. principlesS.O.L.I.D. principles
![Page 38: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/38.jpg)
Single Responsibility principle
Open/Closed principle
Liskov substitution principle
Interface segregation principle
Dependency inversion principle
S.O.L.I.D. principlesS.O.L.I.D. principles
![Page 39: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/39.jpg)
Single ResponsibilitySingle Responsibility
One does not simplyhave more than a reasonto modify a class...
![Page 40: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/40.jpg)
Single ResponsibilitySingle Responsibility
A class should have one,and only one, reasonto change.
![Page 41: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/41.jpg)
Single ResponsibilitySingle Responsibility
class UserRepository{}
![Page 42: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/42.jpg)
Single ResponsibilitySingle Responsibility
class UserRepository{ public function findByAge($age) { /* … */ } public function findByName($name) { /* … */ }}
![Page 43: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/43.jpg)
Single ResponsibilitySingle Responsibility
class UserRepository{ public function findByAge($age) { /* … */ } public function findByName($name) { /* … */ } public function pdfReport() { /* … */ }}
![Page 44: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/44.jpg)
Single ResponsibilitySingle Responsibilityclass UserRepository{ public function findByAge($age) { /* … */ } public function findByName($name) { /* … */ }}
class UserReport{ public function generate() { /* … */ }}
![Page 45: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/45.jpg)
Single Responsibility principle
Open/Closed principle
Liskov substitution principle
Interface segregation principle
Dependency inversion principle
S.O.L.I.D. principlesS.O.L.I.D. principles
![Page 46: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/46.jpg)
Open/closedOpen/closed
Should be able to extendbehaviors, without modifyingthe class
![Page 47: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/47.jpg)
Open/closedOpen/closed
class Authenticator{}
![Page 48: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/48.jpg)
Open/closedOpen/closed
class Authenticator{ public function authenticate($login, $passwd) { /* … */ }}
![Page 49: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/49.jpg)
Open/closedOpen/closed
class Authenticator{ public function authenticate($login, $passwd) { /* … */ } private function findUser($login) { /* finds on DB */ } private function matchPasswd(User $user, $passwd) { /* … */ }}
![Page 50: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/50.jpg)
Open/closedOpen/closed
class Authenticator{ public function authenticate($login, $passwd) { /* … */ } private function findUser($login) { /* finds on DB */ } private function matchPasswd(User $user, $passwd) { /* … */ }}
E se precisarmos buscarem outro lugar?
![Page 51: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/51.jpg)
Open/closedOpen/closed
class Authenticator{ public function authenticate($login, $passwd) { /* … */ } protected function findUser($login) { /* finds on DB */ } private function matchPasswd(User $user, $passwd) { /* … */ }}
Assim podemos sobrescrevero método de busca...
![Page 52: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/52.jpg)
Single Responsibility principle
Open/Closed principle
Liskov substitution principle
Interface segregation principle
Dependency inversion principle
S.O.L.I.D. principlesS.O.L.I.D. principles
![Page 53: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/53.jpg)
Liskov substitutionLiskov substitution
Derived classes must besubstitutable for their baseclasses
![Page 54: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/54.jpg)
Liskov substitutionLiskov substitution
class Rectangle{ public function setX($x) { /* … */ } public function setY($y) { /* … */ } public function getArea() { /* … */ }}
![Page 55: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/55.jpg)
Liskov substitutionLiskov substitution
function getArea($x, $y, Rectangle $object){ $object->setX($x); $object->setY($y); return $object->getArea(); }
getArea(5, 10, new Rectangle());
![Page 56: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/56.jpg)
Liskov substitutionLiskov substitution
class Square extends Rectangle{ public function setX($x) { /* also setY() */ } public function setY($y) { /* also setX() */ }}
![Page 57: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/57.jpg)
Liskov substitutionLiskov substitution
function getArea($x, $y, Rectangle $object){ $object->setX($x); $object->setY($y); return $object->getArea(); }
getArea(5, 10, new Square());
![Page 58: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/58.jpg)
Liskov substitutionLiskov substitution
Contravariance of method arguments in subtypesCovariance of return types in subtypes
![Page 59: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/59.jpg)
Liskov substitutionLiskov substitution
Contravariance of method arguments in subtypesCovariance of return types in subtypes
Preconditions cannot be strengthened in subtypesPostconditions cannot be weakened in subtypes
![Page 60: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/60.jpg)
Liskov substitutionLiskov substitution
Contravariance of method arguments in subtypesCovariance of return types in subtypes
Preconditions cannot be strengthened in subtypesPostconditions cannot be weakened in subtypes
No new exceptions! Don't violate history constraint
![Page 61: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/61.jpg)
Single Responsibility principle
Open/Closed principle
Liskov substitution principle
Interface segregation principle
Dependency inversion principle
S.O.L.I.D. principlesS.O.L.I.D. principles
![Page 62: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/62.jpg)
Interface segregationInterface segregation
Make fine grained interfacesthat are client specific
![Page 63: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/63.jpg)
Interface segregationInterface segregation
interface UserManager{}
![Page 64: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/64.jpg)
Interface segregationInterface segregation
interface UserManager{ public function create($name, $email); public function remove($id); public function find($id); public function findByName($name);}
![Page 65: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/65.jpg)
Interface segregationInterface segregation
class UserController{ public function find($id) { /* … */ }
/** @return UserManager */ public function getManager();}
![Page 66: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/66.jpg)
Interface segregationInterface segregationinterface UserManager{ public function create($name, $email); public function remove($id);}
interface UserLocator{ public function find($id); public function findByName($name);}
![Page 67: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/67.jpg)
Single Responsibility principle
Open/Closed principle
Liskov substitution principle
Interface segregation principle
Dependency inversion principle
S.O.L.I.D. principlesS.O.L.I.D. principles
![Page 68: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/68.jpg)
Dependency InversionDependency Inversion
Depend on abstractions,not on concretions
![Page 69: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/69.jpg)
Dependency InversionDependency Inversion
public function __construct( DatabasePersister $persister, FileNotifier $notifier) { $this->persister = $persister; $this->notifier = $notifier;}
![Page 70: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/70.jpg)
Dependency InversionDependency Inversion
public function __construct(Persister $persister, Notifier $notifier){ $this->persister = $persister; $this->notifier = $notifier;}
![Page 71: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/71.jpg)
SOLID principlesSOLID principles
![Page 72: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/72.jpg)
SOLID principlesSOLID principles
Classes mais coesas
![Page 73: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/73.jpg)
SOLID principlesSOLID principles
Classes mais coesas
Menos acoplamento
![Page 74: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/74.jpg)
SOLID principlesSOLID principles
Classes mais coesas
Menos acoplamento
Menos mudançasem código estável
![Page 75: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/75.jpg)
SOLID principlesSOLID principles
Classes mais coesas
Menos acoplamento
Menos mudançasem código estável
Testes mais fáceis
![Page 76: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/76.jpg)
Dúvidas?Dúvidas?
![Page 77: Make it SOLID!](https://reader034.vdocuments.us/reader034/viewer/2022052620/5579a2ecd8b42ac1148b48f2/html5/thumbnails/77.jpg)
Obrigado!Obrigado!@lcobucci@lcobucci