您的位置:首页 > 编程语言

重构—改善既有代码的设计第一天笔记

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;

}

}

三.总结,在负责逻辑处理的方法里面把临时变量用函数替换掉,每次重构的幅度都很小,每次重构过后要经过测试验证之后,才开始进入下一步,这样就可以容易找到修改的出错点,避免一次改动量过多,导致出现较大的错误。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: