重构的简单演示

原代码:

import java.sql.Date;
import java.util.Calendar;

/**
 * The hello-world program
 * @author fangang
 */
public class HelloWorld_1 {
    /**
     * Say hello to everyone
     * @param now
     * @param user
     * @return the words what to say
     */
    public String sayHello(Date now, String user){
        //Get current hour of day
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);

        //Get the right words to say hello
        String words = null;
        if(hour>=6 && hour<12){
            words = "Good morning!";
        }else if(hour>=12 && hour<19){
            words = "Good afternoon!";
        }else{
            words = "Good night!";
        }
        words = "Hi, "+user+". "+words;
        return words;
    }
}

采用“Extract Methods/抽取方法”进行重构后:

import java.sql.Date;
import java.util.Calendar;
/**
 * The Refactoring's hello-world program
 * @author fangang
 */
public class HelloWorld_2 {
    /**
     * Say hello to everyone
     * @param now
     * @param user
     * @return the words what to say
     */
    public String sayHello(Date now, String user){
        int hour = getHour(now);
        return "Hi, "+user+". "+getSecondGreeting(hour);
    }

    /**
     * Get current hour of day.
     * @param now
     * @return current hour of day
     */
    private int getHour(Date now){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

    /**
     * Get the second greeting
     * @param hour
     * @return the second greeting
     */
    private String getSecondGreeting(int hour){
        if(hour>=6 && hour<12){
            return "Good morning!";
        }else if(hour>=12 && hour<19){
            return "Good afternoon!";
        }else{
            return "Good night!";
        }
    }
}

采用“Extract Class/抽取类”进行重构后:

import java.sql.Date;
import java.util.Calendar;

/**
 * The Refactoring's hello-world program
 * @author fangang
 */
public class HelloWorld_3 {
    /**
     * Say hello to everyone
     * @param now
     * @param user
     * @return the words what to say
     */
    public String sayHello(Date now, String user){
        DateUtil dateUtil = new DateUtil();
        int hour = dateUtil.getHour(now);

        Greeting greeting = new Greeting();
        return greeting.getFirstGreeting(user)+
                greeting.getSecondGreeting(hour);
    }
}

/**
 * A utility about time.
 */
class DateUtil {
    /**
     * Get hour of day.
     * @param date
     * @return hour of day
     */
    public int getHour(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.HOUR_OF_DAY);
    }
}

/**
 * All kinds of greeting.
 */
class Greeting {
    /**
     * Get the first greeting.
     * @param user
     * @return Hi, {user}.
     */
    public String getFirstGreeting(String user){
        return "Hi, "+user+". ";
    }

    /**
     * Get the second greeting.
     * @param hour
     * @return if in the morning, say "Good morning!",
     * if in the afternoon, say "Good afternoon!",
     * else say "Good night!".
     */
    public String getSecondGreeting(int hour){
        if(hour>=6 && hour<12){
            return "Good morning!";
        }else if(hour>=12 && hour<19){
            return "Good afternoon!";
        }else{
            return "Good night!";
        }
    }
}

采用“Extract Interface/抽取接口”进行重构后:

/**
 * Greeting rules interface
 * @author fangang
 */
public interface GreetingRule {
    /**
     * @param hour
     * @return whether the rule is right
     */
    public boolean isRight(int hour);

    /**
     * @return the greeting words
     */
    public String getGreeting();
}

/**
 * The greeting in the morning
 * @author fangang
 */
public class MorningGreeting implements GreetingRule {
    /* (non-Javadoc)
     * @see org.refactoring.helloWorld...
     * GreetingRule#getGreeting()
     */
    @override
    public String getGreeting(){
        return "Good morning!";
    }

    /* (non-Javadoc)
     * @see org.refactoring.helloWorld...
     * GreetingRule#isRight(int)
     */
    @override
    public boolean isRight(int hour){
        if(hour>=6&&hour<12){
            return true;
        }
        return false;
    }
}

其他几个类不写了,类似。
最后将getSecondGreeting()方法改成这样:

/**
 * Get the second greeting.
 * @param hour
 * @return if in the morning,say "Good morning!",
 * if in the afternoon,say "Good afternoon!",
 * if in the evening,say "Good evening!",
 * else, say "Good night!"
 */
public String getSecondGreeting(int hour){
    for(GreetingRule greetingRule : greetingRules){
        if(greetingRule.isRight(hour)){
            return greetingRule.getGreeting();
        }
    }
    throw new RuntimeException("Error when greeting!");
}

摘录自人民邮电出版社《大话重构》,范钢著,Page11-18.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值