重构13天 抽取方法对象(Extract Method Object)

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

详解:如下代码所示,Order 类中的Calculate方法要完成很多功能,在之前我们用“提取方法”来进行重构,现在我们采取“提取方法对象”来完成重构。

重构前代码:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ReflectorDemo
 7 {
 8    public class OrderLineItem
 9     {
10        public decimal Price { get;private set; }
11        
12     }
13 
14     public class Order
15     {
16         private IList<OrderLineItem> OrderLineItems { get; set; }
17 
18         private IList<decimal> Discounts { get; set; }
19 
20         private decimal Tax { get; set; }
21 public decimal Calculate()
22         {
23             decimal subTotal = 0m;
24 
25             //Total up line items
26 
27             foreach (OrderLineItem lineItem  in OrderLineItems)
28             {
29 
30                 subTotal += lineItem.Price;
31             }
32 
33 
34             //substract Discounts
35             foreach (decimal discount in Discounts)
36             {
37                 subTotal -= discount;
38             }
39 
40             //Calculate Tax
41 
42             decimal tax = subTotal*Tax;
43 
44             //calculate GrandTotal
45 
46             decimal grandtotal = subTotal + tax;
47 
48             return grandtotal;
49         }
50     }
51 }

提取方法对象重构后代码:

 1 using System;
 2 using System.Collections;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 
 7 namespace ReflectorDemo
 8 {
 9     public class OrderLineItem
10     {
11         public decimal Price { get; private set; }
12 
13     }
14 
15     public class Order
16     {
17         public IEnumerable<OrderLineItem> OrderLineItems { get; private set; }
18 
19         public IEnumerable<decimal> Discounts { get; private set; }
20 
21         public decimal Tax { get; private set; }
22 
23         public decimal Calculate()
24         {
25 
26             return new OrderCaculator(this).Calculate();
27         }
28 
29     }
30  public class OrderCaculator
31     {
32         private decimal SubTotal { get; set; }
33 
34         private IEnumerable<OrderLineItem> OrderLineItems { get; set; }
35 
36         private IEnumerable<decimal> Discounts { get; set; }
37 
38         private decimal Tax { get; set; }
39 
40         public OrderCaculator(Order order)
41         {
42             OrderLineItems = order.OrderLineItems;
43 
44             Discounts = order.Discounts;
45 
46             Tax = order.Tax;
47 
48         }
49 
50         public decimal Calculate()
51         {
52 
53             CalculateSubTotal();
54 
55             SubstractDiscounts();
56 
57             CalculateTax();
58 
59             return SubTotal;
60         }
61 private void CalculateSubTotal()
62         {
63 
64             foreach (OrderLineItem lineItem in OrderLineItems)
65             {
66                 SubTotal += lineItem.Price;
67             }
68         }
69 
70         private void SubstractDiscounts()
71         {
72             foreach (decimal discount in Discounts)
73             {
74                 SubTotal -= discount;
75             }
76         }
77 
78         private void CalculateTax()
79         {
80             SubTotal += SubTotal * Tax;
81         }
82     }
83 }

这种重构在我工作中用的还是比较少,很大程度上我们都会选择“提取方法”。

posted @ 2016-04-07 10:19  IT少年  阅读(632)  评论(0编辑  收藏  举报