重构改善既有代码的设计--重构第一个案例

1、将长的代码变短,将每个功能抽出来

2、将变量名规范化,取个容易看懂的名字

3、面向对象的原则,函数使用了来自哪类的信息,就应该放到那个类中。

4、查询函数方法将涉及到该变量的代码抽出成一个函数,在需要调用改变量的地方调用该函数即可。函数取代临时变量,减少了冗长复杂的函数。

5、运用多态取代相关的条件逻辑

/**

 *

 * @author Administrator

 */

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;

    }

    

    public double getCharge(int daysRented){

        double result = 0;

        switch(getPriceCode()){

                case Movie.REGULAR:

                    result +=2;

                    if(daysRented > 2){

                        result += (daysRented -2) * 1.5;

                    }

                break;

                case Movie.NEW_RELEASE:

                    result += daysRented * 3;

                    break;

                case Movie.CHILDRENS:

                    result += 1.5;

                    if(daysRented > 3)

                        result += (daysRented - 3) * 1.5;

                    break;

            }

        return result;

    }

    

    public int getFrequentRenterPoints(int daysRented){

        // add bonus for a two day new releass rental

        if((getPriceCode() == Movie.NEW_RELEASE) && daysRented > 1)

            return 2;

        return 1;

    }

    

}

红色部分设计条件逻辑的变量,该变量具有多态性,因此可以抽离出来,用多态来表示,不同的类型逻辑分别写到自己的类中,使代码更清晰。

/**

 *

 * @author Administrator

 */

abstract class Price {

    abstract int getPriceCode();

    abstract double getCharge(int daysRented);

    

    public int getFrequentRenterPoints(int daysRented){

        return 1;

    }

}

class ChildrensPrice extends Price {

    int getPriceCode() {

        return Movie.CHILDRENS;

    }

    public double getCharge(int daysRented) {

        double result = 1.5;

        if (daysRented > 3) {

            result += (daysRented - 3) * 1.5;

        }

        return result;

    }

}

class NewReleasePrice extends Price {

    int getPriceCode() {

        return Movie.NEW_RELEASE;

    }

    public double getCharge(int daysRented) {

        return daysRented * 3;

    }

    

    public int getFrequentRenterPoints(int daysRented){

        return daysRented > 1 ? 2 : 1;

    }

}

class RegularPrice extends Price {

    int getPriceCode() {

        return Movie.REGULAR;

    }

    public double getCharge(int daysRented) {

        double result = 2;

        if (daysRented > 2) {

            result += (daysRented - 2) * 1.5;

        }

        return result;

    }

}

原文地址:https://www.cnblogs.com/kuyuyingzi/p/4266291.html