Created
April 26, 2013 15:31
-
-
Save Bolinha1/5468148 to your computer and use it in GitHub Desktop.
refactoring example for composition, include dependency injection for low coupling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ItensVenda | |
{ | |
private $qtd, $itens; | |
public function __construct() | |
{ | |
} | |
public function setQtd($qtd) | |
{ | |
$this->qtd = $qtd; | |
} | |
public function getQtd() | |
{ | |
return $this->qtd; | |
} | |
public function addItens(Produtos $produto) | |
{ | |
$this->itens[] = $produto; | |
} | |
public function getItens() | |
{ | |
return $this->itens; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Produtos | |
{ | |
private $nome, $valor; | |
public function __construct($nome, $valor) | |
{ | |
$this->nome = $nome; | |
$this->valor = $valor; | |
} | |
public function getNome() | |
{ | |
return $this->nome; | |
} | |
public function getValor() | |
{ | |
return $this->valor; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require 'Produtos.php'; | |
require 'Venda.php'; | |
require 'ItensVenda.php'; | |
$p1 = new Produtos('computador', '2.000,00'); | |
$p2 = new Produtos('impressora', '600,00'); | |
$iten = new ItensVenda(); | |
$v1 = new Venda('24/04/2013', $iten); | |
$v1->addItensVenda('1', $p1); | |
$v1->addItensVenda('2', $p2); | |
var_dump($v1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Venda | |
{ | |
private $data, $itens, $itensVenda ; | |
public function __construct($data, ItensVenda $itens) | |
{ | |
$this->data = $data; | |
$this->itens = $itens; | |
} | |
public function getData() | |
{ | |
return $this->data; | |
} | |
public function addItensVenda($qtd,$produto) | |
{ | |
$this->itens->setQtd($qtd); | |
$this->itens->addItens($produto); | |
$this->itensVenda[] = $this->itens; | |
} | |
public function getItens() | |
{ | |
return $this->itensVenda; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment