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.
15. CCooiissaass qquuee nnuunnccaa
ooccoorrrreemm...... nnéé??
class UserCreator
{
public function create($name, $age) { /*... */ }
}
16. CCooiissaass qquuee nnuunnccaa
ooccoorrrreemm...... nnéé??
public function __construct() {
$this->conn = new PDO(
'mysql:host=127.0.0.1;dbname=awesome',
'root',
'ImAGenius'
);
}
17. CCooiissaass qquuee nnuunnccaa
ooccoorrrreemm...... nnéé??
public function create($name, $age) {
$stm = $this->conn->prepare('...');
$stm->execute([$name, $age]);
}
18. CCooiissaass qquuee nnuunnccaa
ooccoorrrreemm...... nnéé??
Agora, temos que logar no
arquivo /tmp/users.log, adicionando
o responsável pelo cadastro
19. CCooiissaass qquuee nnuunnccaa
ooccoorrrreemm...... nnéé??
class UserCreator
{
public function create($name, $age, $responsible) { /*... */ }
}
20. CCooiissaass qquuee nnuunnccaa
ooccoorrrreemm...... nnéé??
public function __construct() {
// (…)
$this->logger = new FileLogger();
}
43. SSiinnggllee RReessppoonnssiibbiilliittyy
class UserRepository
{
public function findByAge($age) { /* … */ }
public function findByName($name) { /* … */ }
public function pdfReport() { /* … */ }
}
44. SSiinnggllee RReessppoonnssiibbiilliittyy
class UserRepository
{
public function findByAge($age) { /* … */ }
public function findByName($name) { /* … */ }
}
class UserReport
{
public function generate() { /* … */ }
}
49. OOppeenn//cclloosseedd
class Authenticator
{
public function authenticate($login, $passwd) { /* … */ }
private function findUser($login) { /* finds on DB */ }
private function matchPasswd(User $user, $passwd) { /* … */ }
}
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. 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...
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. 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
66. IInntteerrffaaccee sseeggrreeggaattiioonn
interface UserManager
{
public function create($name, $email);
public function remove($id);
}
interface UserLocator
{
public function find($id);
public function findByName($name);
}