什么是重构

什么是重构

在不改变代码外在行为的前提下,对代码做出修改,以改进程序的内部结构
强烈建议亲自阅读一遍Martin Fowler的《重构,改善既有代码的设计》这本书,虽然作者本人有些啰嗦,洋洋洒洒四百多页,但是通俗易懂,特别适合初中级程序员阅读,高级程序员大多数都应该读过或从其他人那里学习过作者的重构手法,需要本书资源私信即可

第一个案例

三个类:电影、顾客、租赁,顾客租赁电影,还能获得积分

重构前的代码:

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;
    }
}

public class Rental {

    private Movie _movie;   //租的电影类型
    private int _daysRented;    //租的天数

    public Rental(Movie movie, int daysRented) {
        _movie = movie;
        _daysRented = daysRented;
    }

    public int getDaysRented() {
        return _daysRented;
    }

    public Movie getMovie() {
        return _movie;
    }
}

public 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 frequentRenterPoints = 0;
        Enumeration rentals = _rentals.elements();

        String result = "租赁记录:" + getName() + "\n";
        while (rentals.hasMoreElements()) {
			double thisAmount = 0;
            Rental each = (Rental) rentals.nextElement();

			switch(each.getMovie().getPriceCode()){
				case Move.REGULAR:
					thisAmount += 2;
					if(each.getDaysRented() > 2){
						thisAmount += (each.getDaysRented() - 2) * 1.5;
					}
					break;
				case Move.NEW_RELEASE:
					thisAmount += each.getDaysRented() * 3;
					break;
				case Move.CHILDRENS:
					thisAmount += 1.5;
					if(each.getDaysRented() > 3){
						thisAmount += (each.getDaysRented() - 3) * 1.5;
					}
					break;
			}

			frequentRenterPoints ++;

			if((each.getMove().getPriceCode() == Movie.NEW_RELEASE) && each.getDaysRented() > 1){
				frequentRenterPoints ++;
			}

            result += "\t" + each.getMovie().getTitle() + "\t" + String.valueOf(thisAmount) + "\n";

        }

        result += "应付金额为:" + String.valueOf(totalAmount) + "\n";
        result += "你获得了" + String.valueOf(frequentRenterPoints) + "会员积分";

        return result;
    }
}

可以看到,重构前Customer类做了太多事情,特别是statement()方法(C语言里又叫函数)做的事情不够单一,很影响阅读,所以接下来的重构将针对类的职责和方法的提炼方面做修改
重构后的代码:

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;
    private Price _price;

    public Movie(String title, int priceCode) {
        _title = title;
        setPriceCode(priceCode);
    }

    public int getPriceCode() {
        return _price.getPriceCode();
    }

    public void setPriceCode(int arg) {
        switch (arg) {
            case REGULAR:
                _price = new RegularPrice();
                break;
            case CHILDRENS:
                _price = new ChildrensPrice();
                break;
            case NEW_RELEASE:
                _price = new NewReleasePrice();
                break;
            default:
                throw new IllegalArgumentException("price code不存在");
        }
    }

    public String getTitle() {
        return _title;
    }

    double getCharge(int daysRented) {
        return _price.getCharge(daysRented);
    }

    int getFrequentRenterPoints(int daysRented) {
        return _price.getFrequentRenterPoints(daysRented);
    }
}

public class Rental {

    private Movie _movie;   //租的电影类型
    private int _daysRented;    //租的天数

    public Rental(Movie movie, int daysRented) {
        _movie = movie;
        _daysRented = daysRented;
    }

    public int getDaysRented() {
        return _daysRented;
    }

    public Movie getMovie() {
        return _movie;
    }

    double getCharge() {
        return _movie.getCharge(_daysRented);
    }

    int getFrequentRenterPoints() {
        return _movie.getFrequentRenterPoints(_daysRented);
    }
}

public 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() {
        Enumeration rentals = _rentals.elements();

        String result = "租赁记录:" + getName() + "\n";
        while (rentals.hasMoreElements()) {
            Rental each = (Rental) rentals.nextElement();

            result += "\t" + each.getMovie().getTitle() + "\t" + String.valueOf(each.getCharge()) + "\n";

        }

        result += "应付金额为:" + String.valueOf(getTotalCharge()) + "\n";
        result += "你获得了" + String.valueOf(getTotalFrequentRenterPoints()) + "会员积分";

        return result;
    }

    private double getTotalCharge() {
        double result = 0;
        Enumeration rentals = _rentals.elements();
        while (rentals.hasMoreElements()) {
            Rental each = (Rental) rentals.nextElement();
            result += each.getCharge();
        }
        return result;
    }

    private int getTotalFrequentRenterPoints() {
        int result = 0;
        Enumeration rentals = _rentals.elements();
        while (rentals.hasMoreElements()) {
            Rental each = (Rental) rentals.nextElement();
            result += each.getFrequentRenterPoints();
        }
        return result;
    }
}

public abstract class Price {
    public abstract int getPriceCode();

    public abstract double getCharge(int daysRented);

    public int getFrequentRenterPoints(int daysRented) {
        return 1;
    }
}

public class NewReleasePrice extends Price {
    @Override
    public int getPriceCode() {
        return Movie.NEW_RELEASE;
    }

    @Override
    public double getCharge(int daysRented) {
        return daysRented * 3;
    }

    @Override
    public int getFrequentRenterPoints(int daysRented) {
        return (daysRented > 1) ? 2 : 1;
    }
}

public class ChildrensPrice extends Price {
    @Override
    public int getPriceCode() {
        return Movie.CHILDRENS;
    }

    @Override
    public double getCharge(int daysRented) {
        double result = 1.5;
        if (daysRented > 3) {
            result += (daysRented - 3) * 1.5;
        }
        return result;
    }
}

public class RegularPrice extends Price {
    @Override
    public int getPriceCode() {
        return Movie.REGULAR;
    }

    @Override
    public double getCharge(int daysRented) {
        double result = 2;
        if (daysRented > 2) {
            result += (daysRented - 2) * 1.5;
        }
        return result;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值