zoukankan      html  css  js  c++  java
  • 31天重构学习笔记22. 分解方法

    概念:本文中的”分解方法”是指把我们所做的这个功能不停的分解方法,直到将一个大方法分解为名字有意义且可读性更好的若干个小方法。

    正文:如下代码所示,因为现实中AcceptPayment方法不会做这么多的事情。,所以我们通过几次分解将 AcceptPayment拆分成若干个名字有意义且可读性更好的小方法。

    using System.Collections.Generic;
    using System.Linq;

    namespace LosTechies.DaysOfRefactoring.SampleCode.BreakMethod.Before
    {
    public class CashRegister
    {
    public CashRegister()
    {
    Tax = 0.06m;
    }

    private decimal Tax { get; set; }

    public void AcceptPayment(Customer customer, IEnumerable<Product> products, decimal payment)
    {
    decimal subTotal = 0m;
    foreach (Product product in products)
    {
    subTotal += product.Price;
    }

    foreach (Product product in products)
    {
    subTotal -= product.AvailableDiscounts;
    }

    decimal grandTotal = subTotal * Tax;

    customer.DeductFromAccountBalance(grandTotal);
    }
    }

    public class Customer
    {
    public void DeductFromAccountBalance(decimal amount)
    {
    // deduct from balance
    }
    }

    public class Product
    {
    public decimal Price { get; set; }
    public decimal AvailableDiscounts { get; set; }
    }
    }


    重构后的代码如下,我们把AcceptPayment的内部逻辑拆分成了CalculateSubtotal、SubtractDiscounts、AddTax、SubtractFromCustomerBalance四个功能明确且可读性更好的小方法。

    using System.Collections.Generic;

    namespace LosTechies.DaysOfRefactoring.SampleCode.BreakMethod.After
    {
    public class CashRegister
    {
    public CashRegister()
    {
    Tax = 0.06m;
    }

    private decimal Tax { get; set; }
    private IEnumerable<Product> Products { get; set; }

    public void AcceptPayment(Customer customer, IEnumerable<Product> products, decimal payment)
    {
    decimal subTotal = CalculateSubtotal();

    subTotal = SubtractDiscounts(subTotal);

    decimal grandTotal = AddTax(subTotal);

    SubtractFromCustomerBalance(customer, grandTotal);
    }

    private void SubtractFromCustomerBalance(Customer customer, decimal grandTotal)
    {
    customer.DeductFromAccountBalance(grandTotal);
    }

    private decimal AddTax(decimal subTotal)
    {
    return subTotal * Tax;
    }

    private decimal SubtractDiscounts(decimal subTotal)
    {
    foreach (Product product in Products)
    {
    subTotal -= product.AvailableDiscounts;
    }
    return subTotal;
    }

    private decimal CalculateSubtotal()
    {
    decimal subTotal = 0m;
    foreach (Product product in Products)
    {
    subTotal += product.Price;
    }
    return subTotal;
    }
    }

    public class Customer
    {
    public void DeductFromAccountBalance(decimal amount)
    {
    // deduct from balance
    }
    }

    public class Product
    {
    public decimal Price { get; set; }
    public decimal AvailableDiscounts { get; set; }
    }
    }

    总结:其实这个重构和我们前面讲的“提取方法”和“提取方法对象”如出一辙,尤其是“提取方法”,所以大家只要知道用这种思想重构就行。

  • 相关阅读:
    互联网搜索引擎——文本预处理
    nltk的pos_tag
    nltk.stem 词干提取(stemming)
    python3的encode和decode涉及的str和bytes转换
    power rails 'GND' and 'VCC/VDD' are interconnected in net VCC
    impot不能导入自己写的python文件【爆红】
    No module named 'tensorflow.contrib'
    解决spark-submit的There is insufficient memory for the Java Runtime Environment to continue.(老顽固问题) failed; error='Cannot allocate memory' (errno=12)
    spark的standalone模式下:查看任务结束后的历史记录
    只是为了保存一些有用链接而已
  • 原文地址:https://www.cnblogs.com/ywsoftware/p/2892612.html
Copyright © 2011-2022 走看看