Created
April 21, 2014 14:21
-
-
Save chukShirley/11144149 to your computer and use it in GitHub Desktop.
Calculation best practice
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
class Product | |
{ | |
protected $unitPrice; | |
protected $discount; | |
protected $finalPrice; | |
public function __construct($unitPrice, $discount) | |
{ | |
$this->setUnitPrice($unitPrice); | |
$this->setDiscount($discount); | |
} | |
//...getters and setters | |
} |
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
class ProductController | |
{ | |
public function storeAction() | |
{ | |
$product = new Product(12,4); | |
$this->getProductFacade()->calculateFinalPrice($product); | |
// ...store $product in db | |
} | |
public function getProductFacade() | |
{ | |
return new ProductFacade(); | |
} | |
} | |
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
class ProductFacade | |
{ | |
public function calculateFinalPrice(Product $product) | |
{ | |
$finalPrice = $product->getUnitPrice() - $product->getDiscount(); | |
$product->setFinalPrice($finalPrice); | |
return $product->getFinalPrice(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment