A Refactoring Example

类别:软件工程 点击:0 评论:0 推荐:

Consider of the following code segment:

class Apple {
    private Boolean mature;

    public void setMature(Season season) {
        switch (season) {
                ...
        }
    }
};

Firstly, “mature“ is not a correct English word for “Apple”. Normally, we say “An apple is riple other than mature.
Secondly, the method “setMature()”is NOT setting mature. Normally, it is better to give funciton parameters the same name as the member variables you are assigning them to. Therefore, change the member variable to “season”, then change the member function to “setSeason()” and add another member function, namely “isMature()”, to the class.
At last, regarding the season in which an apple is riple may be different from countries to countries, change “isMature()“ to be virtual, so that the sub classes can override it to provide another implementation.

本文地址:http://com.8s8s.com/it/it33287.htm