其他分享
首页 > 其他分享> > 重构之函数对象取代函数

重构之函数对象取代函数

作者:互联网

最近在看Martin Fowler的《重构 改善既有代码的设计》和鲍勃大叔的《clean code》,两本书里面都提到一种重构代码的手段--函数对象取代函数。看了只能双击666,mark一下。

printGuessStatistics方法里面的局部变量很多,分解起来很很困难,第一眼看到这个方法时,这些变量的含义完全不清楚。

需要重构的代码片段:

/**
 * @Description
 * @Date 2021/9/20
 * @Created by Jonathan
 */
public class SemanticProblemDemo01 {

    public void printGuessStatistics(char candidate, int count) {
        // 重构下面这一段
        String number;
        String verb;
        String pluralModifier;
        if (count == 0) {
            number = "no";
            verb = "are";
            pluralModifier = "s";
        } else if (count == 1) {
            number = "l";
            verb = "is";
            pluralModifier = "";
        } else {
            number = Integer.toString(count);
            verb = "are";
            pluralModifier = "s";
        }
        String guessMessage = String.format("There %s %s %s%s", verb, number, candidate, pluralModifier);
        print(guessMessage);
    }

    public void print(String guessMessage) {
        System.out.printf("the message is:%s", guessMessage);
    }

}

重构之后的代码:

新建一个类,原来的局部变量是新类的局部变量,这样就可以拆原来很长的方法。

/**
 * @Description
 * @Date 2021/9/20
 * @Created by Jonathan
 */
public class GuessStatisticsMessage {
    private final SemanticProblemDemo01 semanticProblem;
    private String number;
    private String verb;
    private String pluralModifier;

    /**
     * // 调用原来的类里面的某些函数
     *
     * @param semanticProblem 被改造的类
     */
    public GuessStatisticsMessage(SemanticProblemDemo01 semanticProblem) {
        this.semanticProblem = semanticProblem;
    }

    private void thereAreManyLetters(int count) {
        number = Integer.toString(count);
        verb = "are";
        pluralModifier = "s";
    }

    private void thereIsOneLetter() {
        number = "l";
        verb = "is";
        pluralModifier = "";
    }

    private void thereAreNoLetters() {
        number = "no";
        verb = "are";
        pluralModifier = "s";
    }

    private void createPluralDependentMessageParts(int count) {
        if (count == 0) {
            thereAreNoLetters();
        } else if (count == 1) {
            thereIsOneLetter();
        } else {
            thereAreManyLetters(count);
        }
    }

    public String make(char candidate, int count) {
        createPluralDependentMessageParts(count);
        return String.format("There %s %s %s%s", verb, number, candidate, pluralModifier);
    }

    public void print(char candidate, int count) {
        semanticProblem.print(make(candidate, count));
    }
}
/**
 * @Description
 * @Date 2021/9/20
 * @Created by Jonathan
 */
public class SemanticProblemDemo01 {
    public void printGuessStatistics(char candidate, int count) {
        // 重构之后的代码引入了下面这一句
        new GuessStatisticsMessage(this).print(candidate, count);
    }

    public void print(String guessMessage) {
        System.out.printf("the message is:%s", guessMessage);
    }

}
/**
 * @Description
 * @Date 2021/9/20
 * @Created by Jonathan
 */
public class SemanticClient {
    public static void main(String[] args) {
        new SemanticProblemDemo01().printGuessStatistics('A', 3);

    }
}

标签:count,重构,取代,函数,void,number,pluralModifier,public,String
来源: https://www.cnblogs.com/d9e84208/p/15314962.html