IMZRH的日志

努力成为一个有用的人

导航

31天重构指南之十三:提取方法对象

Posted on 2009-09-29 15:41  张荣华  阅读(431)  评论(0编辑  收藏  举报

这篇文章要说的重构来自于Martin Fowlwrs的重构列表,你可以在这里查看原始描述

这是一个不常用的重构。当你发现一个方法中存在过多的局部变量时,你可以通过使用“提取方法对象”重构来引入一些方法,每个方法完成任务的一个步骤,这样可以使得程序更具有可读性。

    1: public class OrderLineItem

   2: {
   3:     public decimal Price { get; private set; }
   4: }
   5:  
   6: public class Order
   7: {
   8:     private IList<OrderLineItem> OrderLineItems { get; set; }
   9:     private IList<decimal> Discounts { get; set; }
  10:     private decimal Tax { get; set; }
  11:  
  12:     public decimal Calculate()
  13:     {
  14:         decimal subTotal = 0m;
  15:  
  16:         // Total up line items
  17:         foreach (OrderLineItem lineItem in OrderLineItems)
  18:         {
  19:             subTotal += lineItem.Price;
  20:         }
  21:  
  22:         // Subtract Discounts
  23:         foreach (decimal discount in Discounts)
  24:             subTotal -= discount;
  25:  
  26:         // Calculate Tax
  27:         decimal tax = subTotal * Tax;
  28:  
  29:         // Calculate GrandTotal
  30:         decimal grandTotal = subTotal + tax;
  31:  
  32:         return grandTotal;
  33:     }
  34: }
 

下面是重构后的代码中,我们引入了OrderCalculator类,该类实现了所有的计算方法,Order类将自身传递给OrderCalculator类并调用Calculate方法完成计算。

   1: public class OrderLineItem
   2: {
   3:     public decimal Price { get; private set;}
   4: }
   5:  
   6: public class Order
   7: {
   8:     public IEnumerable<OrderLineItem> OrderLineItems { get; private set;}
   9:     public IEnumerable<decimal> Discounts { get; private set; }
  10:     public decimal Tax { get; private set; }
  11:  
  12:     public decimal Calculate()
  13:     {
  14:         return new OrderCalculator(this).Calculate();
  15:     }
  16: }
  17:  
  18: public class OrderCalculator
  19: {
  20:     private decimal SubTotal { get; set;}
  21:     private IEnumerable<OrderLineItem> OrderLineItems { get; set; }
  22:     private IEnumerable<decimal> Discounts { get; set; }
  23:     private decimal Tax { get; set; }
  24:  
  25:     public OrderCalculator(Order order)
  26:     {
  27:         OrderLineItems = order.OrderLineItems;
  28:         Discounts = order.Discounts;
  29:         Tax = order.Tax;
  30:     }
  31:  
  32:     public decimal Calculate()
  33:     {
  34:         CalculateSubTotal();
  35:  
  36:         SubtractDiscounts();
  37:  
  38:         CalculateTax();
  39:  
  40:         return SubTotal;
  41:     }
  42:  
  43:     private void CalculateSubTotal()
  44:     {
  45:         // Total up line items
  46:         foreach (OrderLineItem lineItem in OrderLineItems)
  47:             SubTotal += lineItem.Price;
  48:     }
  49:  
  50:     private void SubtractDiscounts()
  51:     {
  52:         // Subtract Discounts
  53:         foreach (decimal discount in Discounts)
  54:             SubTotal -= discount;
  55:     }
  56:  
  57:     private void CalculateTax()
  58:     {
  59:         // Calculate Tax
  60:         SubTotal += SubTotal * Tax;
  61:     }
  62: }
 

原文链接:http://www.lostechies.com/blogs/sean_chambers/archive/2009/08/13/refactoring-day-13-extract-method-object.aspx

 

PS:我个人不是很喜欢重构,我还是喜欢提取方法这个重构。