Skip to main content
added 134 characters in body
Source Link
Steve
  • 111
  • 2

Updated: What I'm building:

  • Pricing calculator

    Pricing calculator
  • There are 4 products each having their own specific pricing rules that vary from user input

    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

    /*

    • 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) { $this->price = $price; $price = 55; //@TODO go lookup price from tables, figure out dob, gender, stuff

       $rate = 1.03;
       $amount = $rate * $price;
       return $amount;
      

      } }

    $solution1 = ProductFactory::createProduct('solution1'); print $solution1->calculatePrice("02/25/1982", "male", "100000"); print $solution1::$price;

    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);

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

    /*

    • 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) { $this->price = $price; $price = 55; //@TODO go lookup price from tables, figure out dob, gender, stuff

       $rate = 1.03;
       $amount = $rate * $price;
       return $amount;
      

      } }

    $solution1 = ProductFactory::createProduct('solution1'); print $solution1->calculatePrice("02/25/1982", "male", "100000"); print $solution1::$price;

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);
Source Link
Steve
  • 111
  • 2

PHP design pattern factory input for products and quotes

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.

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

    /*

    • 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) { $this->price = $price; $price = 55; //@TODO go lookup price from tables, figure out dob, gender, stuff

       $rate = 1.03;
       $amount = $rate * $price;
       return $amount;
      

      } }

    $solution1 = ProductFactory::createProduct('solution1'); print $solution1->calculatePrice("02/25/1982", "male", "100000"); print $solution1::$price;