您的位置:首页 > 其它

第一章 重构第一个案例

2011-10-02 21:58 393 查看
1.1起点

Movie(影片)

public class Movie{
public static final int CHILDRENS = 2;
public static final int REGULAR = 0;
public static final int NEW_RELEASE = 1;
private String _title;
private int _priceCode;
public Movie(String title, int priceCode){
_title = title;
_priceCode = priceCode;
}
public int getPriceCode{
return _priceCode;
}
public void setPriceCode(int arg){}
_priceCode = arg;
}
public String getTitle(){
return _title;
}
};
Rental(租赁)
Class Rental{
private Movie _movie;
private int _dayRented;
public Rental(Movie movie, int daysRented){
_movie = movie;
_daysRented = daysRented;
}
public int getDayRented(){
return _daysRented;
}
public Movie getMovie(){
return _movie;
}
};
1.1起点
Movie(影片)
public class Movie{
public static final int CHILDRENS = 2;
public static final int REGULAR = 0;
public static final int NEW_RELEASE = 1;
private String _title;
private int _priceCode;
public Movie(String title, int priceCode){
_title = title;
_priceCode = priceCode;
}
public int getPriceCode{
return _priceCode;
}
public void setPriceCode(int arg){}
_priceCode = arg;
}
public String getTitle(){
return _title;
}
};
Rental(租赁)
Class Rental{
private Movie _movie;
private int _dayRented;
public Rental(Movie movie, int daysRented){
_movie = movie;
_daysRented = daysRented;
}
public int getDayRented(){
return _daysRented;
}
public Movie getMovie(){
return _movie;
}
};
Customer(顾客)
class Customer{
private String _name;
private Vector _rentals = new Vector();
public Customer (String name){
_name = name;
}
public void addRental(Rental arg){
_rentals.addElement(arg);
}
public String getName(){
return _name;
}
public String statement(){
double totalAmount = 0;
int freguentRentalPoints = 0;
Enumeration rentals = _rentals.elements();
String result = "Rental Record for " + getName() + "\n";
while(rentals.hasMoreElements()){
double thisAmount = 0;
Rental each = (Rental)Rentals.nextElement();
switch(each.getMovie().getPriceCode()){
case Movie.REGULAR:
thisAmount += 2;
if(each.getDaysRented() > 2)
thisAmount += (each.getDaysRented() - 2) * 1.5;
break;
case Movie.NEW_RELEASE:
thisAmount += each.getDaysRented() * 3;
break;
case Movie.CHILDRENS
thisAmount += 1.5;
if(each.getDaysRented() > 3)
thisAmount += (each.getDaysRented() - 3) * 1.5;
break;
}
frequentRenterPoints ++;
if((each.getMovie().getPriceCode() == Movie.NEW_RELEASES)&&(each.getDaysRented() > 1))
frequentRenterPoints ++;
result += "\t" + each.getMovie().getTitle() + "\t" + String.valueof(thisAmount) + "\n";
totalAmount += thisAmount;
}
result += "Amount owed is " + String.valueof(totalAmount) + "\n";
result += "You earned " + String.valueof(frequentRenterPoints) + " frequent renter points";
return result;
}
}

如果你发现自己需要为程序添加一个特性,而代码结构使你无法很方便地达成目的,那就先重构那个程序,使特性的添加比较容易进行,然后再添加特性。

1.2重构的第一步

为即将修改的代码建立一组可靠的测试环境。

测试程序对结果的报告方式,ok,或者列出错误清单。实现测试程序的自我检验。

好的测试是重构的根本。

*重构之前,首先检查自己是否有一套可靠的测试机制。这些测试必须有自我检验能力。

1.3分解并重组statement()

*重构技术就是以微小的步伐修改程序。如果你犯下错误,很容易便可发现它。

重构工具extract method.Eclipse也有相应的工具。CDT

*任何一个傻瓜都能写出计算机可以理解的代码,唯有写出人类容易理解的代码,才是优秀的程序员。

move method

Replace Temp with Query

1.4运用多态取代与价格相关的条件逻辑

最好不要在另一个对象的属性基础上运用switch语句。如果不得不使用,也应该在对象自己的数据上使用,而不是在别人的数据上使用。

可以使用sate模式,或者stratge模式。

*如果你发现自己需要为程序添加一个特性,而代码结构使你无法很方便地达成目的,那就先重构那个程序,使特性的添加比较容易进行,然后再添加特性。

1.2重构的第一步

为即将修改的代码建立一组可靠的测试环境。

测试程序对结果的报告方式,ok,或者列出错误清单。实现测试程序的自我检验。

好的测试是重构的根本。

*重构之前,首先检查自己是否有一套可靠的测试机制。这些测试必须有自我检验能力。

1.3分解并重组statement()

*重构技术就是以微小的步伐修改程序。如果你犯下错误,很容易便可发现它。

重构工具extract method.Eclipse也有相应的工具。CDT

*任何一个傻瓜都能写出计算机可以理解的代码,唯有写出人类容易理解的代码,才是优秀的程序员。

move method

Replace Temp with Query

1.3分解并重组statement()

*重构技术就是以微小的步伐修改程序。如果你犯下错误,很容易便可发现它。

重构工具extract method.Eclipse也有相应的工具。CDT

*任何一个傻瓜都能写出计算机可以理解的代码,唯有写出人类容易理解的代码,才是优秀的程序员。

move method

Replace Temp with Query

1.4运用多态取代与价格相关的条件逻辑

1.3分解并重组statement()*重构技术就是以微小的步伐修改程序。如果你犯下错误,很容易便可发现它。重构工具extract method.Eclipse也有相应的工具。CDT*任何一个傻瓜都能写出计算机可以理解的代码,唯有写出人类容易理解的代码,才是优秀的程序员。move methodReplace Temp with Query

最好不要在另一个对象的属性基础上运用switch语句。如果不得不使用,也应该在对象自己的数据上使用,而不是在别人的数据上使用。

可以使用sate模式,或者stratge模式。









                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: