Rebuilding serialization 11 in big talk: this is the way small steps run.

Source: Internet
Author: User
Tags date now

Rebuilding serialization 11 in big talk: this is the way small steps run.

After talking so much about it, I believe you have a preliminary impression on the concept of Small-step running, but your understanding is not very deep. Let's take a look at an example in actual work. Let's take a look at what is big layout, what is big design, and what is small design.

Back to the previous HelloWorld example, the initial requirements are always simple and clear. When a user logs on to a website, the website usually needs to say "hi, XXX!" to the user! ". At the same time, if it is morning, "Good morning!" is displayed! ". If it is afternoon," Good afternoon! "is displayed! ". In addition," Good night! "is displayed! ". For such a requirement, we have written 10 lines of code in a HelloWorld class:

/** * 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;}}


If the demand is not changed, everything will be fine. But this is always the case. When the software is submitted for the first time, the change begins. Instead of getting the user name directly, the system first obtains the userId and then obtains the user name from the database through the userId. The following greetings may need to be more refined, for example, "Good noon! "And greetings" Good evening! "in the evening! ", Midnight greetings" Good midnight! ". In addition, the user wishes to greet "Happy new year!" on some special holidays, such as the new year! ", Valentine's day greetings" Happy valentine's day! ", 38 women's day greetings" Happywomen's day! And so on. In addition to the listed festivals, they also want to temporarily add some special days. Therefore, the greetings need to form a library and support dynamic addition. In addition, this greeting library should support multiple languages. If you select English, "Good morning!" is displayed! ", And select Chinese to display" Good Morning !"...... In short, different needs are constantly raised by users, so our designers start to have a hot head, congestion, and confused thinking. Yes. If you expect that you can meet all these requirements in one step, you will surely feel confused and choose between sides, and then make wrong designs. However, if you have learned the development mode of "small step", everything will become less complex.

First, we observe the original program and find that it contains three relatively independent functional code segments. Therefore, we use the "Extraction Method" in refactoring to extract them to the three functions getHour (), getFirstGreeting (), getSecondGreeting (), and let the original function reference it:

/** * 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){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!";}}}

Although the program structure has undergone great changes, the Code actually executed remains unchanged. Then, we checked the requirements and found that the user requirements were divided into two different branches: the change of user greetings and the change of greetings about time. To this end, we split the HelloWorld program again and split the user greeting program into the GreetingToUser class using the "extraction class" in the refactoring, split the time-related greeting program into the GreetingAboutTime class:

/** * 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!";}}}

After restructuring the system, let's take a look at the user's requirements for changing the time greetings section: Greetings need to be more refined, for example, greeting "Good noon!" at noon! "And greetings" Good evening! "in the evening! ", Midnight greetings" Good midnight! ". In addition, the user wishes to greet "Happy new year!" on some special holidays, such as the new year! ", Valentine's day greetings" Happy valentine's day! ", 38 women's day greetings" Happywomen's day! And so on. At this point, we found that we no longer need to modify HelloWorld or other classes for the change of the time greeting, but only focus on modifying GreetingAboutTime. This is the improvement caused by refactoring.

At the same time, we found that getHour () was enough in the past, but now getMonth () and getDay () are required (). As the complexity of the Program increases, we refactored it at the right time and extracted the time-related program into a new type of DateUtil, so that we can smoothly rewrite the original time greeting program:

/** * The utility of time * @author fangang */public class DateUtil {private Calendar calendar;/** * @param date */public DateUtil(Date date){calendar = Calendar.getInstance();calendar.setTime(date);}/** * @return the hour of day */public int getHour(){return calendar.get(Calendar.HOUR_OF_DAY);}/** * @return the month of date */public int getMonth(){return calendar.get(Calendar.MONTH)+1;}/** * @return the day of month */public int getDay(){return calendar.get(Calendar.DAY_OF_MONTH);}}/** * The greeting about time. * @author fangang */public class GreetingAboutTime {private Date date;public GreetingAboutTime(Date date){this.date = date;}/** * @return the greeting about time */public String getGreeting(){DateUtil dateUtil = new DateUtil(date);int month = dateUtil.getMonth();int day = dateUtil.getDay();int hour = dateUtil.getHour();if(month==1 && day==1) return "Happy new year! ";if(month==1 && day==14) return "Happy valentine's day! ";if(month==3 && day==8) return "Happy women's day! ";if(month==5 && day==1) return "Happy Labor day! ";......if(hour>=6 && hour<12) return "Good morning!";if(hour==12) return "Good noon! ";if(hour>=12 && hour<19) return "Good afternoon! ";if(hour>=19 && hour<22) return "Good evening! ";return "Good night! ";}}

Finally, we create a user table to store user information, create the UserDao interface and its implementation class, and provide the user information access service for GreetingToUser. We use the greetingRule table to store Various greetings, create the GreetingRuleDao interface and its implementation class to provide an extensible and multilingual greeting library for GreetingAboutTime (3.1 ). All of this is based on the existing situation and evolved step by step through small steps.

Figure 3.1 HelloWorld design diagram

Small steps are a step-by-step evolutionary process of program optimization, which is an important core of restructuring ideas. In the future, we will also use more examples in practical work to let you truly understand the development process of the small step.

Big talk rebuilding serialization home: http://blog.csdn.net/mooodo/article/details/32083021

Note: I hope that the author or source should be noted to show my respect for the author when I reprint this article. Thank you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.