小步快跑是这样玩的(上)
软件的发展规律就是这样的,起初十分简单明了,使我们可以轻松地进行合理的设计。接着开始变更,业务变得越来越复杂,程序也随之变得越来越复杂了。正是因为软件开始由简单软件向复杂软件转变,而我们的设计却没有合理地调整,最后导致了我们的系统越维护越困难,成为了不可被扣的遗留系统——IT攻城狮永远的痛。这就是遗留系统产生的根本原因。
因此,解决遗留系统的根本办法,就是在软件由简单软件向复杂软件转变的关键时刻,适时做出调整,使软件重新回到高质量的状态。这里,我们要做出的调整被称为重构,而做出这种调整的最佳方式,就是“小步快跑”啦。说得那么玄乎,到底什么是“小步快跑”呢?说不尽千言万语,倒不如一个简单的示例:
故事是这样的,当用户登录一个网站时,网站往往需要给用户打一个招呼:“hi, XXX! ”。同时,如果此时是上午则显示“Good morning! ”,如果是下午则显示“Good afternoon! ”,除此显示“Good night! ”。对于这样一个需求我们在一个HelloWorld类中写了十来行代码:
/** * The Refactoring's hello-world program * @author fangang */public class HelloWorld {/** * 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 dayCalendar calendar = Calendar.getInstance();calendar.setTime(now);int hour = calendar.get(Calendar.HOUR_OF_DAY);//Get the right words to say helloString 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;}}
/** * The Refactoring's hello-world program * @author fangang */public class HelloWorld {/** * Say hello to everyone * @param now * @param user * @return the words what to say */public String sayHello(Date now, String user){//这里将原有的代码通过“抽取方法”抽取到3个函数中int hour = getHour(now);return getFirstGreeting(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 first greeting. * @param user * @return the first greeting */private String getFirstGreeting(String user){return "Hi, "+user+". ";}/** * 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!";}}}
/** * The Refactoring's hello-world program * @author fangang */public class HelloWorld {/** * Say hello to everyone * @param now * @param user * @return the words what to say */public String sayHello(Date now, String user){GreetingToUser greetingToUser = new GreetingToUser(user);GreetingAboutTime greetingAboutTime = new GreetingAboutTime(now);return greetingToUser.getGreeting() + greetingAboutTime.getGreeting();}}/** * The greeting to user * @author fangang */public class GreetingToUser {private String user;/** * The constructor with user * @param user */public GreetingToUser(String user){this.user = user;}/** * @return greeting to user */public String getGreeting(){return "Hi, "+user+". ";}}/** * The greeting about time. * @author fangang */public class GreetingAboutTime {private Date date;public GreetingAboutTime(Date date){this.date = date;}/** * @param date * @return the hour of day */private int getHour(Date date){Calendar calendar = Calendar.getInstance();calendar.setTime(date);return calendar.get(Calendar.HOUR_OF_DAY);}/** * @return the greeting about time */public String getGreeting(){int hour = getHour(date);if(hour>=6 && hour<12){return "Good morning!";}else if(hour>=12 && hour<19){return "Good afternoon!";}else{return "Good night!";}}}