Refactoring Day 13 : Extract Method Object

Refactoring Day 13 : Extract Method Object 
 
Today's refactoring comes from Martin Fowlers list of refactorings. You can find his original article here with a brief description. 
This is a more infrequent refactoring that I see myself using but it comes in handy at times. When trying to apply an Extract Method refactoring, and multiple methods are needing to be introduced, it is sometimes gets ugly because of multiple local variables that are being used within a method. Because of this reason, it is better to introduce an Extract Method Object refactoring and to segregate the logic required to perform the task.  
   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: } 
 
This entails passing a reference to the class that will be returning the computation to a new object that has the multiple methods via the constructor, or passing the individual parameters to the constructor of the method object. I will be showing the former here. 
   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; } 
31 Days of Refactoring  Sean Chambers  25 
  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: }   

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值