我正在制定一個方法來計算以PHP編寫的購物車的總數,並且希望獲得有關用於處理不同條件的良好設計模式的一些反饋。我正試圖爲管理員提供多種計算折扣的策略。管理員可以選擇在應用稅款之前或之後應用折扣,以及將折扣應用於運輸或不運輸。這給出了我的任務的概述。基於多個變量狀態的條件語句的一個很好的設計模式
變量
我對這個任務有可能的值以下變量:
$tax_option
: '前' '後'
$shipping_option
,: '是',「否'
除了這兩個變量之外,計算總值的公式也會根據$subtotal
(購物車中物品的數量)和$reduction
(總折扣金額)。
一般來說,我的邏輯是我測試了$tax_option
和$shipping_option
變量的4種組合中的每一種。我還需要更改$subtotal
小於或等於$reduction
的情況的公式。總之,我有8種不同的條件。
的可能性
我的身影,我真的在這裏有3種不同的選擇:if
語句,一個switch
陳述或戰略格局。我將展示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 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',則評估較少。 –