重构—改善既有代码的设计第一天笔记
2014-04-03 10:27
239 查看
一.笔记内容:
1.重构的步骤的本质:由于每次修改的幅度都很小,所以任何错误都很容易发现。这样就不用耗费大把时间调试。
2.重构技术就是以微小的步伐修改程序。如果你犯下错误,很容易便可以发现它。
3.尽量除去临时变量,临时变量往往引发问题,它们会导致大量参数被传来传去。
二.实例代码:
1.Movie.java
package com.test.movie.demo;
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) {
this.title = _title;
this.priceCode = _priceCode;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getPriceCode() {
return priceCode;
}
public void setPriceCode(int priceCode) {
this.priceCode = priceCode;
}
}
2.Rental.java
package com.test.movie.demo;
public class Rental {
private Movie movie;
private int daysRented;
public Rental(Movie _movie, int _daysRented){
this.movie = _movie;
this.daysRented = _daysRented;
}
public int getDaysRented(){
return this.daysRented;
}
public double getcharge() {
double result = 0.0;
switch (this.getMovie().getPriceCode()) {
case Movie.REGULAR:
result += 2;
if(this.getDaysRented() > 2){
result += (this.getDaysRented() - 2)*1.5;
}
break;
case Movie.NEW_RELEASE:
result += this.getDaysRented()*3;
break;
case Movie.CHILDRENS:
result += 1.5;
if(this.getDaysRented() >3){
result += (this.getDaysRented() - 3)* 1.5;
}
}
return result;
}
public Movie getMovie(){
return this.movie;
}
//重构过的提前方法
public int getFrequentRenterPoints(){
if((this.getMovie().getPriceCode() == Movie.NEW_RELEASE) && getDaysRented() > 1){
return 2;
}else {
return 1;
}
}
}
3.重构的代码
package com.test.movie.demo;
import java.util.Enumeration;
import java.util.Vector;
public class Customer {
private String name;
private Vector rentals = new Vector();
public Customer(String _name){
this.name = _name;
}
public void addRental(Rental arg){
this.rentals.addElement(arg);
}
public String getName(){
return this.name;
}
public String statement(){
double totalAmount = 0;
int frequentRenterPoints = 0;
Enumeration rentals = this.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;
}
}
frequentRenterPoints += each.getFrequentRenterPoints();
// frequentRenterPoints++;
//
// if((each.getMovie().getPriceCode() == Movie.NEW_RELEASE) && (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;
}
public String statementByRc(){
// double totalAmount = 0;
// int frequentRenterPoints = 0;
Enumeration rentals = this.rentals.elements();
String result = "Rental Record for " + getName() + "\n";
while (rentals.hasMoreElements()) {
Rental each = (Rental) rentals.nextElement();
// frequentRenterPoints++;
//
// if((each.getMovie().getPriceCode() == Movie.NEW_RELEASE) && (each.getDaysRented() > 1)){
// frequentRenterPoints++;
// }
// frequentRenterPoints += each.getFrequentRenterPoints();
result += "\t" + each.getMovie().getTitle() + "\t" + String.valueOf(amountFor(each)) + "\n";
// totalAmount += amountFor(each);
}
// result += "Amount owed is " + String.valueOf(totalAmount) + "\n";
result += "Amount owed is " + String.valueOf(getTotalCharge()) + "\n";
// result += "You earned " + String.valueOf(frequentRenterPoints) + " frequent renter points";
result += "You earned " + String.valueOf(getTotalFrequentRenterPoints()) + " frequent renter points";
return result;
}
private int getTotalFrequentRenterPoints(){
int result = 0;
Enumeration rentals = this.rentals.elements();
while (rentals.hasMoreElements()) {
Rental each = (Rental) rentals.nextElement();
result += each.getFrequentRenterPoints();
}
return result;
}
private double getTotalCharge(){
double result = 0;
Enumeration rentals = this.rentals.elements();
while (rentals.hasMoreElements()) {
Rental each = (Rental)rentals.nextElement();
result += each.getcharge();
}
return result;
}
private double amountFor(Rental aRental) {
// double result = 0.0;
// switch (aRental.getMovie().getPriceCode()) {
// case Movie.REGULAR:
// result += 2;
// if(aRental.getDaysRented() > 2){
// result += (aRental.getDaysRented() - 2)*1.5;
// }
// break;
// case Movie.NEW_RELEASE:
// result += aRental.getDaysRented()*3;
// break;
// case Movie.CHILDRENS:
// result += 1.5;
// if(aRental.getDaysRented() >3){
// result += (aRental.getDaysRented() - 3)* 1.5;
// }
// }
//
// return result;
return aRental.getcharge();
}
}
4.未重构的代码:
package com.test.movie.demo;
import java.util.Enumeration;
import java.util.Vector;
public class Customer {
private String name;
private Vector rentals = new Vector();
public Customer(String _name){
this.name = _name;
}
public void addRental(Rental arg){
this.rentals.addElement(arg);
}
public String getName(){
return this.name;
}
public String statement(){
double totalAmount = 0;
int frequentRenterPoints = 0;
Enumeration rentals = this.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;
}
}
frequentRenterPoints++;
if((each.getMovie().getPriceCode() == Movie.NEW_RELEASE) && (each.getDaysRented() > 1)){
frequentRenterPoints++;
}
result += "\t" + each.getMovie().getTitle() + "\t" + String.valueOf(thisAmount) + "\n";
totalAmount += thisAmount;
}
result += "Amount owed is " + String.valueOf(totalAmount) + "\n";
return null;
}
}
三.总结,在负责逻辑处理的方法里面把临时变量用函数替换掉,每次重构的幅度都很小,每次重构过后要经过测试验证之后,才开始进入下一步,这样就可以容易找到修改的出错点,避免一次改动量过多,导致出现较大的错误。
1.重构的步骤的本质:由于每次修改的幅度都很小,所以任何错误都很容易发现。这样就不用耗费大把时间调试。
2.重构技术就是以微小的步伐修改程序。如果你犯下错误,很容易便可以发现它。
3.尽量除去临时变量,临时变量往往引发问题,它们会导致大量参数被传来传去。
二.实例代码:
1.Movie.java
package com.test.movie.demo;
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) {
this.title = _title;
this.priceCode = _priceCode;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getPriceCode() {
return priceCode;
}
public void setPriceCode(int priceCode) {
this.priceCode = priceCode;
}
}
2.Rental.java
package com.test.movie.demo;
public class Rental {
private Movie movie;
private int daysRented;
public Rental(Movie _movie, int _daysRented){
this.movie = _movie;
this.daysRented = _daysRented;
}
public int getDaysRented(){
return this.daysRented;
}
public double getcharge() {
double result = 0.0;
switch (this.getMovie().getPriceCode()) {
case Movie.REGULAR:
result += 2;
if(this.getDaysRented() > 2){
result += (this.getDaysRented() - 2)*1.5;
}
break;
case Movie.NEW_RELEASE:
result += this.getDaysRented()*3;
break;
case Movie.CHILDRENS:
result += 1.5;
if(this.getDaysRented() >3){
result += (this.getDaysRented() - 3)* 1.5;
}
}
return result;
}
public Movie getMovie(){
return this.movie;
}
//重构过的提前方法
public int getFrequentRenterPoints(){
if((this.getMovie().getPriceCode() == Movie.NEW_RELEASE) && getDaysRented() > 1){
return 2;
}else {
return 1;
}
}
}
3.重构的代码
package com.test.movie.demo;
import java.util.Enumeration;
import java.util.Vector;
public class Customer {
private String name;
private Vector rentals = new Vector();
public Customer(String _name){
this.name = _name;
}
public void addRental(Rental arg){
this.rentals.addElement(arg);
}
public String getName(){
return this.name;
}
public String statement(){
double totalAmount = 0;
int frequentRenterPoints = 0;
Enumeration rentals = this.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;
}
}
frequentRenterPoints += each.getFrequentRenterPoints();
// frequentRenterPoints++;
//
// if((each.getMovie().getPriceCode() == Movie.NEW_RELEASE) && (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;
}
public String statementByRc(){
// double totalAmount = 0;
// int frequentRenterPoints = 0;
Enumeration rentals = this.rentals.elements();
String result = "Rental Record for " + getName() + "\n";
while (rentals.hasMoreElements()) {
Rental each = (Rental) rentals.nextElement();
// frequentRenterPoints++;
//
// if((each.getMovie().getPriceCode() == Movie.NEW_RELEASE) && (each.getDaysRented() > 1)){
// frequentRenterPoints++;
// }
// frequentRenterPoints += each.getFrequentRenterPoints();
result += "\t" + each.getMovie().getTitle() + "\t" + String.valueOf(amountFor(each)) + "\n";
// totalAmount += amountFor(each);
}
// result += "Amount owed is " + String.valueOf(totalAmount) + "\n";
result += "Amount owed is " + String.valueOf(getTotalCharge()) + "\n";
// result += "You earned " + String.valueOf(frequentRenterPoints) + " frequent renter points";
result += "You earned " + String.valueOf(getTotalFrequentRenterPoints()) + " frequent renter points";
return result;
}
private int getTotalFrequentRenterPoints(){
int result = 0;
Enumeration rentals = this.rentals.elements();
while (rentals.hasMoreElements()) {
Rental each = (Rental) rentals.nextElement();
result += each.getFrequentRenterPoints();
}
return result;
}
private double getTotalCharge(){
double result = 0;
Enumeration rentals = this.rentals.elements();
while (rentals.hasMoreElements()) {
Rental each = (Rental)rentals.nextElement();
result += each.getcharge();
}
return result;
}
private double amountFor(Rental aRental) {
// double result = 0.0;
// switch (aRental.getMovie().getPriceCode()) {
// case Movie.REGULAR:
// result += 2;
// if(aRental.getDaysRented() > 2){
// result += (aRental.getDaysRented() - 2)*1.5;
// }
// break;
// case Movie.NEW_RELEASE:
// result += aRental.getDaysRented()*3;
// break;
// case Movie.CHILDRENS:
// result += 1.5;
// if(aRental.getDaysRented() >3){
// result += (aRental.getDaysRented() - 3)* 1.5;
// }
// }
//
// return result;
return aRental.getcharge();
}
}
4.未重构的代码:
package com.test.movie.demo;
import java.util.Enumeration;
import java.util.Vector;
public class Customer {
private String name;
private Vector rentals = new Vector();
public Customer(String _name){
this.name = _name;
}
public void addRental(Rental arg){
this.rentals.addElement(arg);
}
public String getName(){
return this.name;
}
public String statement(){
double totalAmount = 0;
int frequentRenterPoints = 0;
Enumeration rentals = this.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;
}
}
frequentRenterPoints++;
if((each.getMovie().getPriceCode() == Movie.NEW_RELEASE) && (each.getDaysRented() > 1)){
frequentRenterPoints++;
}
result += "\t" + each.getMovie().getTitle() + "\t" + String.valueOf(thisAmount) + "\n";
totalAmount += thisAmount;
}
result += "Amount owed is " + String.valueOf(totalAmount) + "\n";
return null;
}
}
三.总结,在负责逻辑处理的方法里面把临时变量用函数替换掉,每次重构的幅度都很小,每次重构过后要经过测试验证之后,才开始进入下一步,这样就可以容易找到修改的出错点,避免一次改动量过多,导致出现较大的错误。
相关文章推荐
- 《重构 改善既有代码的设计》学习笔记 2 -- Extract Method(提炼函数)
- 重构--改善既有代码的设计--读书笔记1
- “重构-改善既有代码的设计”笔记
- 读重构-改善既有代码的设计笔记
- 笔记-重构-改善既有代码的设计(Refactoring - Improving the Design of Existing Code)
- 《重构-改善既有代码的设计》8.13笔记
- 《重构-改善既有代码的设计》笔记
- 《重构-改善既有代码的设计》学习笔记(二)
- 《重构 改善即有代码的设计》笔记
- 重构-改善既有代码设计学习笔记(一)—代码的坏味道
- 代码坏味道 重构: 改善有效代码的设计--阅读笔记
- 《重构 改善既有代码的设计》学习笔记 1
- 03重构_改善既有代码的设计阅读笔记之三
- 【笔记】《重构-改善既有代码的设计》
- 重构_改善既有代码的设计第二天笔记
- 01重构_改善既有代码的设计阅读笔记之一
- 阅读《重构 改善既有代码的设计》学习笔记
- 重构(改善既有代码的设计)-- 读书笔记1
- 《重构--改善代码的既有设计》阅读笔记之代码的坏味道
- 02重构_改善既有代码的设计阅读笔记之二