Complete classic reconstruction using Eclipse

xiaoxiao2021-03-06  26

I believe that many people have read the book "Reconstruction" book, taught us a lot of excellent reconstruction methods, so that we can write our object-oriented procedures more effective, more convenient . That's all kinds of good books that are known as "design patterns".

In the book, the author is still in emotion that there is no excellent reconstruction tool. Most of the reconstruction work can only be completed by finding / replace, but fortunately with Unit test tools to ensure quality, otherwise it is unimaginable.

However, we are now very lucky, in Eclipse and the latest version of JBuilder has perfect reconstruction feature, let's take a look at how to use how to complete the classic program reconstruction in Eclipse.

First, we established a package called "refactoring" based on the example in "Reconstruction", which established a class named "Movie", "Rental" and "Customer", and typed for initial Code.

From the above figure we can see that we have established initial code, let us use Eclipse to reconstruct this program together.

(One)

Decompose and recombinantly statement ()

The first one needs to reconstruct the statement () function, the code is too long, it is very difficult to understand, we first extract a part of the method and perform related processing.

Select the entire Switch statement, then right click, select "Refactor-> Extract Method" in the pop-up menu, as shown below.

At this point, the Extract Method window will pop up. We now want to build the name of the new method is Amountfor, so type name in the "Method Name" text box, below the window we can see the instructions for the reconstructed method.

If you click the "Preview" button, we can preview the reconstructed code, and Eclipse is very imaged to show us how to draw a method. Click the "OK" button, Eclipse will automatically complete the work.

The parameter THISAMOUNT in the method is that we don't need, so we have to modify the declaration of the method. Click Right-click to select "Refactor-> Change Method Signature" in the menu.

In the pop-up dialog, we can see the currently definition of Amountfor, which lists the current two parameters "thisamount" and "EACH", select the first parameter, click the "Remove" button, you can delete it.

But now it is now deleted this parameter, and the Eclipse automatically pops up the dialog, prompting the user.

We confirm that you want to delete this parameter, so click the "Continue" button to continue modification.

Eclipse will automatically detect errors and marked with red wavy lines under the problematic statement.

The reason why this error is not because this variable has not been defined in the method, and the modification error can also be used to quickly complete the functionality provided by Eclipse.

Position the cursor on thisamount, then press "Ctrl-1", Eclipse will automatically pop up the modification suggest, here we select "Create Local Variable THISAMOUNT" so that Eclipse will automatically define this variable for us.

But at this time, we will still see the red wavy line below thisamount, because our variable is not initialized after defining, and then press "Ctrl-1" below thisamount, select "Initialization variable in the pop-up menu. ", Eclipse will automatically initialize this variable for us.

We see that we have not type a letter, but press several keys, then in the menu, we have completed a lot of processing, which not only improved the efficiency of our coding, but also avoided some The appearance of low-level errors. Then we discover the type of thisamount, and we have to return a Double type data, and the system automatically defines a integer, which will perform automatic fetch operation when processing. So we need to change THISAMOUNT type.

Next, we found that the name of the variable we used is not very good, because there is no clear meaning. "Refactoring" in the book "The programmer can prepare the programmer that can understand the program is a real outstanding programmer, so we need to reconstruct an operation again - change the name of the variable.

Select thisamount variable, then right click, select "Refactor-> Rename" in the pop-up environment menu, and change this variable.

The pop-ups of the renamed dialog box is very simple, showing the current name, next we have to change this name.

Type our new name, "Result" in the text box, click the "OK" button, the system will automatically find the content related to thisamount variable throughout the program and replace it.

With the same method, we rename the variable of EACH to Arental, which is more clear. The code has been changed as shown below, so that we have completed the first reconstruction step.

转载请注明原文地址:https://www.9cbs.com/read-40522.html

New Post(0)