《重构》中此方法叫做塑造模板函数,在设计模式中,对应的模式就是模板模式。重构中的很多变动比较大的方法都会导致重构,但重构中有非常多的小重构手法。就好像建筑一个房子,设计模式教你厨房客厅怎么搭配以设计出一个什么样的风格,而重构中给出了更多的建议,细小的细节,哪些地方应该怎么处理,会导致程序易读、易维护、易扩展。塑造模板函数是一个非常重要的重构手法,熟练运用此手法能直接将过程化的代码“变得”那么面向对象。

  重构前的主体代码:

  1 package io.nelson;
  2 
  3 import java.util.Enumeration;
  4 import java.util.Vector;
  5 
  6 public class Customer {
  7     
  8     private String _name;
  9     private Vector<Rental> _rentals = new Vector<Rental>();
 10     
 11     public Customer(String name){
 12         _name = name;
 13     }
 14     
 15     public void addRental(Rental arg)
 16     {
 17         _rentals.addElement(arg);
 18     }
 19     
 20     public String getName(){
 21         return _name;
 22     }
 23     
 24     public String TextStatement(){
 25         double totalAmount = 0;
 26         int frequentRenterPoints = 0;
 27         Enumeration<Rental> rentals = _rentals.elements();
 28         String result = "Rental Record for "+getName()+"\n";
 29         
 30         while(rentals.hasMoreElements())
 31         {
 32             double thisAmount = 0;
 33             Rental each = (Rental) rentals.nextElement();
 34             
 35             switch(each.getMoive().getPriceCode())
 36             {
 37             case Movie.REGULAR:
 38                 thisAmount += 2;
 39                 if(each.getDaysRented() > 2)
 40                 {
 41                     thisAmount += (each.getDaysRented()-2)*1.5;
 42                 }
 43                 break;
 44             case Movie.NEW_RELEASE:
 45                 thisAmount += each.getDaysRented()*3;
 46                 break;
 47             case Movie.CHILDRENS:
 48                 thisAmount += 1.5;
 49                 if(each.getDaysRented() > 3)
 50                 {
 51                     thisAmount += (each.getDaysRented()-3)*1.5;
 52                 }
 53                 break;
 54             }
 55             
 56             frequentRenterPoints++;
 57             if((each.getMoive().getPriceCode() == Movie.NEW_RELEASE)&& 
 58                     each.getDaysRented()>1) frequentRenterPoints++;
 59             
 60             result += "\t" + each.getMoive().getTitle()+ "\t"+String.valueOf(thisAmount)+"\n";
 61             totalAmount += thisAmount;
 62         }
 63         
 64         result += "Amount owed is "+String.valueOf(totalAmount)+"\n";
 65         result += "You earned "+String.valueOf(frequentRenterPoints)+" frequent renter points";
 66         
 67         return result;
 68     }
 69 }
 70 
 71 //租赁类
 72 class Rental {
 73     
 74     private Movie _movie;
 75     private int _daysRented;
 76     
 77     public Rental(Movie movie,int daysRented)
 78     {
 79         _movie = movie;
 80         _daysRented = daysRented;
 81     }
 82     
 83     public int getDaysRented()
 84     {
 85         return _daysRented;
 86     }
 87     
 88     public Movie getMoive()
 89     {
 90         return _movie;
 91     }
 92 }
 93 
 94 class Movie
 95 {
 96     public static final int CHILDRENS = 2;
 97     public static final int REGULAR = 0;
 98     public static final int NEW_RELEASE = 1;
 99     
100     private String _title;
101     private int _priceCode;
102     
103     public Movie(String title,int priceCode)
104     {
105         _title = title;
106         _priceCode = priceCode;
107     }
108     
109     public int getPriceCode(){
110         return _priceCode;
111     }
112     
113     public String getTitle()
114     {
115         return _title;
116     }
117 }
View Code

相关文章: