I'm working on figuring out the best way to design this so that it's well organized and it seems like the factory design pattern makes sense.
Updated: What I'm building: - Pricing calculator - There are 4 products each having their own specific pricing rules that vary from user input - The person can choose between 1 product or all products
Should I have a Calculator class to determine product pricing or leave the calculations in each product type?
/*
* Use Factory Pattern to build objects
*/
class ProductFactory {
public static function createProduct($type) {
switch ($type) {
case 'solution1':
return new Solution1Product();
break;
case 'solution2':
return new Solution2Product();
break;
case 'solution3':
return new Solution3Product();
break;
case 'solution4':
return new Solution4Product();
break;
}
}
}
/*
* Abstract Product
*/
abstract class AbstractProduct {
protected $price;
protected $max_limit;
protected $max_amount;
abstract public function calculatePrice($dob, $gender, $amount);
}
/*
* Solution 1 Product
*/
class Solution1Product extends AbstractProduct {
protected $max_limit = 50000;
function __contruct() {
print "Test";
return "Solution1";
}
function calculatePrice($dob, $gender, $amount) {
$rate = 1.03;
$price = 55;
if ($amount >= $this->max_limit) {
return "Max Limit Reached";
}
//@TODO go lookup price from tables
$this->price = $rate * $price;
return $this->price;
}
}
$solution1 = ProductFactory::createProduct('solution1');
print $solution1->calculatePrice("02/25/1982", "male", 100000);
return; break;
combos. I always do that too. I typecase ...:
andbreak;
and then anything in between. But I always have adefault: break;
too :) \$\endgroup\$$classname = ucfirst( strtolower( $type ) )."Product"; if ( class_exists( $classname ) ) { return new $classname; }
\$\endgroup\$