Vinícius B. Alonso
vba321@hotmail.com
class Product {
private $value;
private $name;
public function __construct($name, $value) {
$this->name = $name;
$this->value = $value;
}
public function getValue() {
return $this->value;
}
}
class Item {
private $product;
private $quantity;
public function __construct($product, $quantity) {
$this->product = $product;
$this->quantity = $quantity;
}
public function getProduct() {
return $this->product;
}
public function getQuantity() {
return $this->quantity;
}
}
class Cart {
private $owner;
private $items;
public function __construct($owner, $items) {
$this->owner = $owner;
$this->items = $items;
}
public function totalValueOfItems() {
$total = 0;
foreach ($this->items as $item) {
$total += $item->getProduct()->getValue() * $item->getQuantity();
}
return $total;
}
}
Quem deve retornar o valor de cada produto é a classe Item
class Cart {
private $owner;
private $items;
public function __construct($owner, $items) {
$this->owner = $owner;
$this->items = $items;
}
public function totalValueOfItems() {
$total = 0;
foreach ($this->items as $item) {
$total += $item->getProduct()->getValue() * $item->getQuantity();
}
return $total;
}
}
class Item {
private $product;
private $quantity;
public function __construct($product, $quantity) {
$this->product = $product;
$this->quantity = $quantity;
}
public function getProduct() {
return $this->product;
}
public function getQuantity() {
return $this->quantity;
}
public function totalValue() {
return $this->product->getValue() * $this->quantity;
}
}
class Cart {
private $owner;
private $items;
public function __construct($owner, $items) {
$this->owner = $owner;
$this->items = $items;
}
public function totalValueOfItems() {
$total = 0;
foreach ($this->items as $item) {
$total += $item->totalValue();
}
return $total;
}
}
Evitamos um bad smell chamado Feature Envy
Menos lugares para alterar em mudanças futuras