PHPで書かれたショッピングカートの合計を計算する方法を検討しています。さまざまな条件を処理するための良いデザインパターンに関するフィードバックがあります。私は管理者に割引を計算するための複数の戦略を提供しようとしています。管理者は、税金が適用される前または後に割引を適用するか、配送料に割引を適用するかを選択することができます。これは私の仕事の概要を示しています。複数の変数のステータスに基づいた条件文の良いデザインパターン
変数
私は可能な値と、このタスクのために、以下の変数があります。
$tax_option
を:
$shipping_option
'の後に'、 '前': 'はい'、「いいえ'
これらの2つの変数の他に、合計値を計算する式は、$subtotal
(カート内のアイテムの量)と$reduction
(割引総額)を入力します。
一般的に言えば、$tax_option
と$shipping_option
変数の4つの組み合わせごとにテストします。 $subtotal
が$reduction
以下である場合の式を変更する必要もあります。私には8つの異なる条件があります。 if
文、switch
声明、または戦略パターン:私は私は本当にここ3異なるオプションを持っていることを把握
可能性
。 if
ステートメントとストラテジパターンの構造を示しますが、switch
の可能性は除外されます。なぜなら、この文脈では正しいとは思えないからです。ここで
if
書
私は検討していますif文の一般的なパターンです。私はこれをわずかに再構成できることを知っていることに注意してください。何らかの理由でこれは私にとって読みやすいものです。
if($tax_option == 'after' && $shipping_option == 'yes')
{
if($subtotal <= $reduction)
{
}
else
{
}
}
elseif($tax_option == 'before' && $shipping_option == 'yes')
{
if($subtotal <= $reduction)
{
}
else
{
}
}
elseif($tax_option == 'before' && $shipping_option == 'no')
{
if($subtotal <= $reduction)
{
}
else
{
}
}
elseif($tax_option == 'after' && $shipping_option == 'no')
{
if($subtotal <= $reduction)
{
}
else
{
}
}
else
$new_total = $total;
Strategyパターン
この問題の解決のためのオンライン周りを探した後、私は素晴らしい見えた、Strategy Patternについて学びました。私はここで作業を開始し、それに関するいくつかのフィードバックを気にしません。これまでのところ、いくつかのルーチンを明白に削除して、次のコードのように見えます。
class DPPCalculateTotal
{
protected $formulas = array();
public function DPPCalculateTotal($tax_option, $shipping_option, $total, $subtotal, $shipping, $tax, $coupons_amount, $reduction)
{
foreach($this->formulas as $formula)
{
if($formula->test($tax_option, $shipping_option, $total, $subtotal, $shipping, $tax, $coupons_amount, $reduction))
{
return $formula->calculate_total($tax_option, $shipping_option, $total, $subtotal, $shipping, $tax, $coupons_amount, $reduction);
}
}
}
function add_formula(DPPFormula $formula)
{
$this->formulas = $formula;
}
}
interface DPPFormula
{
public function test($tax_option, $shipping_option, $total, $subtotal, $shipping, $tax, $coupons_amount, $reduction);
public function calculate_total($tax_option, $shipping_option, $total, $subtotal, $shipping, $tax, $coupons_amount, $reduction);
}
class AfterTaxesYesShippingGreaterSubotal implements DPPFormula
{
public function test($tax_option, $shipping_option, $total, $subtotal, $shipping, $tax, $coupons_amount, $reduction)
{
}
public function calculate_total($tax_option, $shipping_option, $total, $subtotal, $shipping, $tax, $coupons_amount, $reduction)
{
}
}
class AfterTaxesYesShippingLesserSubotal implements DPPFormula
{
public function test($tax_option, $shipping_option, $total, $subtotal, $shipping, $tax, $coupons_amount, $reduction)
{
}
public function calculate_total($tax_option, $shipping_option, $total, $subtotal, $shipping, $tax, $coupons_amount, $reduction)
{
}
}
class AfterTaxesNoShippingGreaterSubotal implements DPPFormula
{
public function test($tax_option, $shipping_option, $total, $subtotal, $shipping, $tax, $coupons_amount, $reduction)
{
}
public function calculate_total($tax_option, $shipping_option, $total, $subtotal, $shipping, $tax, $coupons_amount, $reduction)
{
}
}
class AfterTaxesNoShippingLesserSubotal implements DPPFormula
{
public function test($tax_option, $shipping_option, $total, $subtotal, $shipping, $tax, $coupons_amount, $reduction)
{
}
public function calculate_total($tax_option, $shipping_option, $total, $subtotal, $shipping, $tax, $coupons_amount, $reduction)
{
}
}
class BeforeTaxesYesShippingGreaterSubotal implements DPPFormula
{
public function test($tax_option, $shipping_option, $total, $subtotal, $shipping, $tax, $coupons_amount, $reduction)
{
}
public function calculate_total($tax_option, $shipping_option, $total, $subtotal, $shipping, $tax, $coupons_amount, $reduction)
{
}
}
class BeforeTaxesYesShippingLesserSubotal implements DPPFormula
{
public function test($tax_option, $shipping_option, $total, $subtotal, $shipping, $tax, $coupons_amount, $reduction)
{
}
public function calculate_total($tax_option, $shipping_option, $total, $subtotal, $shipping, $tax, $coupons_amount, $reduction)
{
}
}
class BeforeTaxesNoShippingGreaterSubotal implements DPPFormula
{
public function test($tax_option, $shipping_option, $total, $subtotal, $shipping, $tax, $coupons_amount, $reduction)
{
}
public function calculate_total($tax_option, $shipping_option, $total, $subtotal, $shipping, $tax, $coupons_amount, $reduction)
{
}
}
class BeforeTaxesNoShippingLesserSubotal implements DPPFormula
{
public function test($tax_option, $shipping_option, $total, $subtotal, $shipping, $tax, $coupons_amount, $reduction)
{
}
public function calculate_total($tax_option, $shipping_option, $total, $subtotal, $shipping, $tax, $coupons_amount, $reduction)
{
}
}
あなたの意見で質問
1)、ここで何が続行するための最良の方法だろうか?
2)この場合の戦略パターンの利点は何ですか?
3)戦略パターンを試してみるのは初めてですが、正しい方向に向かっているように見えますか?
は、あなたの入力をありがとうございました!私はこのパターンを学ぶのが楽しいですし、私が得ることができるフィードバックを感謝します!
個々の数式があまり複雑でない場合は、if文を探します。特にあなたがそれをより読みやすいと思うならば。 if文の中に$ shipping_option == 'yes'を入れれば、評価が減ります。 –