php - Removing too many IF's from object -
edit:
i should have mentioned wanted more object oriented. , don't think code here anywhere near oo , neither using switches, it?
op:
first of all, in below example working dutch units, calculations might seem off, you'll idea.
basically, have grocery list products. in database store prices in "price piece" or "price pound", example. in order calculating total price of each product, based on amount selected, working below class.
example:
in grocerylist have few products, , behind product text field , dropdown. in textfield can enter amount want have, , in dropdown select if needs ounces, pounds, , on. based upon values, , initial price database (price per piece , on), can calculate total price each product.
class calculation { protected $price; protected $amount; protected $unit; public function __construct($price, $amount, $unit) { $this->price = $price; $this->amount = $amount; $this->unit = $unit; } public function calculate() { if($this->unit === 'ounce') { return $this->formatounce(); } if($this->unit === 'pound') { return $this->formatpound(); } return $this->formatone(); } public function formatone() { return $this->price * $this->amount / 100; } public function formatounce() { return $this->price / 1000 * $this->amount / 100; } public function formatpound() { return $this->price / 1000 * 500 * $this->amount / 100; } } the problem have this:
public function calculate() { if($this->unit === 'ounce') { return $this->formatounce(); } if($this->unit === 'pound') { return $this->formatpound(); } return $this->formatone(); } how change above code in order oo? use repository or interface that? or can within particular class keep simple? feel there way many if's.
a oo oriented approach (as requested after editing question):
you make base class handles output total(). calls protected method calculation. calculate():
class item { protected $price; protected $amount; public function __construct($price, $amount) { $this->price = $price; $this->amount = $amount; } protected function calculate() { return $this->price * $this->amount; } public function total($format = true) { if ($format) { return number_format($this->calculate(), 2); } return $this->calculate(); } } now can extend base item pound version of item. pound version override calculate() method because calculation done differently.
class pounditem extends item { protected function calculate($format = true) { return $this->price / 1000 * 500 * $this->amount / 100; } } to produce objects you'll need either constructor method or called factory produce them. here factory class. have been implemented on basket class.
class itemfactory { static public function create($price, $amount, $type) { // implemented in numerous ways // method on basket $class = $type . "item"; return new $class($price, $amount); } } creating new item of pound type:
$a = itemfactory::create(49.99, 25, "pound"); since pounditem item can use total() method. since we've changed implementation of calculate() calculates pounds.
echo $a->total();
Comments
Post a Comment