Write this book before writing, but there is always no chance to find it, now you have gotten it, and you read more excited, there is an excitement when you read the design mode of the year. Lele, is not as happy. I have no Chinese version, so the younger brother is not talented, and the translation is reading with everyone. This place is in the tiger-Tang Dragon, English is good, I don't win, if there is any translation, please I forgive. Supporter UP can be. Chapter 1: Reconstruction, First Example
How to start writing to refactor this book? The traditional way is to list historical outline, extensive principles, etc. When some people start this at the meeting, I have some slight sleep. In the face of the speaker's grant, my mind began to think about the lower level, until she or he gave an example. This will wake me from drowsiness, there is an example to make me know what should be done next. It is too simple to use the principle, it is too difficult to point out how it applies. And an example will make the problem clearer.
Therefore, I intend to start this book from a reconstructed example. In this process, I will tell you how to reconstruct and let you process a sense of sensibility. This will then give me a usual introduction.
However, as an example of introduction, I will encounter a big problem. If I choose a big program, describe how to reconstruct it, this is too complicated for playing most readers so that you can't do it. (I have tried that even a slight complicated example also exceeds 100 pages) On the other hand, if I choose a small program that is easy to understand, it will make the reconstructed look do not value.
Therefore, I will describe this technology using a class of classics to describe this technology by introducing procedures in a real world. Frank, I showed it to you, I plan to use the applause, I don't have any value for her, but if I show you a code in a large system, then the rework will soon become important . So I have to ask you to see it as part of a large system.
Start point
The program is very simple. He calculates and prints a fee of a customer's tape rental store. The program enters the movie and borrowing time of the client borrow. Charges are calculated by borrowing the date length of the borrowing and the movie type of the borrow. There are three types of movies here: usually, children, new film. In addition, by calculating the cost, it can be estimated that the hot spot can be estimated, and it is not dependent on whether the movie is a new version.
The following classes will represent different elements, and the class diagrams are as follows:
Figure 1.1 Start a category diagram, only the most important feature is described. Use UML symbols.
I will showcase the code of these classes.
Movie
Movie is a simple class.
public class Movie {public static final int SHILDRENS = 2; public static final int REGULAR = 0; public static final int NEW_RELEASEE = 1; private String _title; private int _priceCode; public Movie (String title, int priceCode) {_title = title; _pricecode = pricecode;} public int getpricecode () {return _pricecode;} public void setpricecode (int arg) {_pricecode = arg;} public void gettitle () {return_title;}}
Rental
Rental represents a movie
class Rental {private Movie _movie; private int _daysRented; public Rental (Movie movie, int daysRented) {_movie = movie; _daysRented = daysRented;} public int getDaysRented () {return _daysRented;} public Movie getMovie () {return _movie;} } CustomerCustomer represents a customer in the video store, like her class, like her class, and the accessor.
Class customer {
private String _name; private Vector _rentals = new Vector (); public Customer (String name) {_name = name;} public void addRental (Rental arg) {_rentals.addElement (arg);} public String getName () {return _name; }
Customer has a method to implement Statement. Figure: 1.2 shows the interaction of these methods. The main body of this method is an interface.
Figure 1.2Statement method interaction map.
Public string stat () {
double totalAmount = 0; int frequentRenterPoints = 0; Enumeration rentals = _rentals.elements (); String result = "Rental Record for" getName () "/ n"; while (rentals.hasMoreElements ()) {double thisAmount = 0 Rental Each = (Rental) Rentals.NexTelement (); Switch (Each.getMovie (). GetPriceCode ()) {Case Movie.Regular: Thisamount = 2; IF (Each.getDaysRented ()> 2) THISAMOUNT = (Each. getDaysRented () - 2) * 1.5; break; case Movie.NEW_RELEASE: thisAmount = each.getDaysRented () * 3; break; case Movie.CHILDRENS: thisAmount = 1.5; if (each.getDaysRented ()> 3) thisAmount = ( each.getDaysRented () - 3) * 1.5; break;} frequentRenterPoints ; if ((each.getMovie.getPriceCode () == Movie.NEW_RELEASE) && each.getDaysRented ()> 1) frequentRenterPoints ; // show figures for this rental Result = "/ t" Each.getMovie (). gettitle () "/ t" string.valueof (thisamount) "/ n"; TOT alAmount = thisAmount;} // add footer lines result = "Amount owed is" String .valueOf (totalAmount) "/ n"; result = "You earned" String.valueOf (frequentRenterPoints) "frequent renter points" RETURN RESULT;} What impression do you have for this program? My opinion on him is that he is not a good design and is not an object-oriented. This is not important for a simple program like this. As an agile and unclear simple example, it does not have any errors. But if this is part of a large system, then this program will have some problems. The StateMetn method is too far from the Customer class. Many work should be done in other classes.
Even if the program can work. Are you willing to make beauty about ugly code? Or until we change the system. The compiler will not know if the code is ugly or clear. At that time, when I changed a system, we will be involved in the system, we will notice this. A bad design is difficult to have changed, because it is difficult to point out that it needs to be changed. If it is difficult to point out what to change, the programmer will make mistakes or introduce new bugs. In this example, we will transform the user's ideas. First of all: The user wants to publish information about tariff information on the Web to meet the complaints of customers. Considering this change, you have to view your source code to see if there is a possibility to reuse your current Statement method to meet HTMLStatement. Your only way is to rewrite the entire method of the same characteristics, of course, this is not expensive, you can achieve your requirements by copying.
But when the charging rule changes? You have to modify Statement and HtmlStatement to make sure they are consistent. The problem is that when you change again, you have to copy the code again. If you expect your programs that will not be changed again, then copy is very pasted is a good method, but if your program will be modified, then the copy is very pasted will bring confusion.
Let us introduce the second change. The user wants to change the movie classification method, but they can't decide immediately, but will be decided later. They have an idea in this mind. These changes will affect the calculation of the rental mode and the number of rental frequency points. As an experienced developer, you should be more user plans, rather than let your plan stay within 6 months.
The Statement method should be able to cope with changes in non-class methods and toll rules. But if I take the copy from the StateMet method, it is very pasted to HTMLSTATEMTN our consistency. In the future, as the rules are further complicated, we will hardly point out that there is changed, it is difficult to guarantee that no new mistakes are introduced.
You may try to make a small change, after all, she is already able to work. Remember an old engineering proverb: "If he breaks, don't try to modify her." This program may not break, but he is harmful. When you find that he can't adapt to the user's request, it will bring you difficulties. That's why you want to reconstruct it.
Tip When you find the structure of the program inconvenient to add a feature, you have to add a feature to the program, the reconstructor is easy to add new features, then add your new features.
Step by refactoring. to be continued. . . . . . . . . . . . . . . . . . . . . . . . . .