拆分阶段

未匹配的标注

重构动机

如果一块代码在同时处理两件或两件以上不同的事(各自使用不同的一组数据和函数),这时可以把它拆分成各自独立的模块。这样在修改的时候,可以单独处理每个模块,而不必同时考虑其他模块,免去回忆其他模块细节的烦恼。

重构前

下面代码实现的功能:计算订单价格:

<?php

namespace app\controller;

class Index
{

    /**
     * 测试
     *
     * @return float|int|mixed
     */
    public function test ()
    {
        // 商品数据:折扣条件、单价、折扣
        $product = ['discount_threshold' => 100, 'unit_price' => 3, 'discount_rate' => 0.8];
        // 快递数据:免费条件、折扣
        $shippingMethod = ['free_threshold' => 370, 'discount_rate' => 0.1];

        return $this->calculateOrderPrice($product, 120, $shippingMethod);
    }


    /**
     * 计算订单价格
     *
     * @param array $product        商品
     * @param int   $quantity       数量
     * @param array $shippingMethod 快递价格
     * @return float|int|mixed
     */
    public function calculateOrderPrice (array $product, int $quantity, array $shippingMethod)
    {
        // 基本价格
        $basePrice = $product['unit_price'] * $quantity;

        // 计算折扣
        $discount = max($quantity - $product['discount_threshold'], 0) // 计算可以打折的商品数量
            * $product['unit_price']       // 乘以商品单价
            * $product['discount_rate'];   // 乘以折扣

        // 全部商品的价格数据
        $productData = [
            'base_price' => $basePrice, // 基本价格
            'quantity'   => $quantity,  // 商品数量
            'discount'   => $discount   // 折扣
        ];

        $orderPrice = $this->applyShipping($productData, $shippingMethod);
        return $orderPrice;
    }


    /**
     * 计算订单的快递费
     *
     * @param array $productData    商品数据
     * @param array $shippingMethod 快递价格数据
     * @return float|int|mixed
     */
    public function applyShipping (array $productData, array $shippingMethod)
    {
        // 如果商品总价格不符合免快递费的要求,则按照商品件数计算快递费
        $shippingPrice = $productData['base_price'] > $shippingMethod['free_threshold']
            ? 0 : $productData['quantity'] * $shippingMethod['discount_rate'];

        // 订单价格 = 商品总价格 - 折扣 + 快递费
        return $productData['base_price'] - $productData['discount'] + $shippingPrice;
    }

}

重构后

calculateOrderPrice()计算基本价格、折扣的逻辑提取出来。

<?php

namespace app\controller;


class Index
{

    /**
     * 测试
     *
     * @return float|int|mixed
     */
    public function test ()
    {
        $product = ['discount_threshold' => 100, 'unit_price' => 3, 'discount_rate' => 0.8];
        $shippingMethod = ['free_threshold' => 370, 'discount_rate' => 0.1];

        return $this->calculateOrderPrice($product, 120, $shippingMethod);
    }


    /**
     * 计算订单价格
     *
     * @param array $product        商品
     * @param int   $quantity       数量
     * @param array $shippingMethod 快递价格
     * @return float|int|mixed
     */
    public function calculateOrderPrice (array $product, int $quantity, array $shippingMethod)
    {
        $productData = $this->calculateProductData($product, $quantity);
        $orderPrice = $this->applyShipping($productData, $shippingMethod);
        return $orderPrice;
    }


    /**
     * 计算商品的数据
     *
     * @param array $product  商品
     * @param int   $quantity 数量
     * @return array
     */
    public function calculateProductData (array $product, int $quantity)
    {
        // 基本价格
        $basePrice = $product['unit_price'] * $quantity;

        // 计算折扣
        $discount = max($quantity - $product['discount_threshold'], 0) // 计算可以打折的商品数量
            * $product['unit_price']                                        // 乘以商品单价
            * $product['discount_rate'];                                    // 乘以折扣

        // 全部商品的价格数据
        $productData = [
            'base_price' => $basePrice, // 基本价格
            'quantity'   => $quantity,  // 商品数量
            'discount'   => $discount   // 折扣
        ];

        return $productData;
    }


    /**
     * 计算订单的快递费
     *
     * @param array $productData    商品数据
     * @param array $shippingMethod 快递价格数据
     * @return float|int|mixed
     */
    public function applyShipping (array $productData, array $shippingMethod)
    {
        // 如果商品总价格不符合免快递费的要求,则按照商品件数计算快递费
        $shippingPrice = $productData['base_price'] > $shippingMethod['free_threshold']
            ? 0 : $productData['quantity'] * $shippingMethod['discount_rate'];

        // 订单价格 = 商品总价格 - 折扣 + 快递费
        return $productData['base_price'] - $productData['discount'] + $shippingPrice;
    }
}

本文章首发在 LearnKu.com 网站上。

上一篇 下一篇
讨论数量: 0
发起讨论 只看当前版本


暂无话题~