"Java Refactoring series" Refactoring 31-style package Collection

Source: Internet
Author: User

Reproduced from: http://blog.csdn.net/lovelion/article/details/17970147

In 2009, Sean Chambers published an article in his blog about the refactoring:useful Refactoring techniques have to know series, which is published every day to introduce a A kind of reconstruction means, 31 consecutive articles, so the name "reconstructed 31 days: You should master the means of refactoring." In addition, Sean chambers the 31 article "31 refactoring means" into an ebook, the following is the original blog link and ebook download address:

Blog Original: http://lostechies.com/seanchambers/2009/07/31/31-days-of-refactoring/

e-Book Download address: Http://lostechies.com/wp-content/uploads/2011/03/31DaysRefactoring.pdf


This series of blogs will be based on Sean Chambers's work, but changing the programming language (C #--> Java) and, more importantly, adding a lot of new content, incorporating a lot of sunny understanding of these refactoring tools, more complete, more in-depth analysis, and A number of new topics are also discussed, hoping to help you write more high-quality program code.


These 31 refactoring methods are listed below "Note: The original is refactoring nth Day , that is, refactoring days N,sunny personally feel that some refactoring is very simple, one day too little, but addiction, so rename (Rename) for refactoring nth,, The plan is to repeat the 31 refactoring methods in the Java language, which are not exactly consistent with the original order, and add a lot of new content, ":

Refactoring 1:encapsulate Collection "Refactoring First: Package Collection"

Refactoring 2:move Method "refactoring the second type: moving methods"

Refactoring 3:pull up Method "refactoring third: Move Up methods"

Refactoring 4:pull up Field "refactoring four: Move Fields up"

Refactoring 5:push Down Method "Refactoring V: Move Down methods"

Refactoring 6:push down field "refactoring type Sixth: move Fields Down"

Refactoring 7:rename (method,class,parameter) refactoring type seventh: renaming (methods, classes, parameters)

Refactoring 8:replace Inheritance with delegation "refactoring eighth: Substituting delegates for inheritance"

Refactoring 9:extract Interface "refactoring Nineth: Extraction Interface"

Refactoring 10:extract Method "Refactoring type Tenth: extraction methods"

Refactoring 11:switch to Strategy "refactoring 11th: Refactoring conditional statement to policy mode"

Refactoring 12:break Dependencies "refactoring 12th: elimination of dependencies"

Refactoring 13:extract Method Object "Refactoring the 13th: extracting Methods Objects"

Refactoring 14:break Responsibilities "refactoring 14th: separating responsibility"

Refactoring 15:remove Duplication "Refactoring type 15th: Removing duplicate code"

Refactoring 16:encapsulate Conditional "Refactoring type 16th: encapsulating conditional expression"

Refactoring 17:extract Superclass "Refactoring type 17th: extracting parent Classes"

Refactoring 18:replace Exception with conditional "refactoring 18th: replacing exceptions with conditional statements"

Refactoring 19:extract Factory Class "Refactoring type 19th: Extracting factory class"

Refactoring 20:extract Subclass "Refactoring type 20th: extracting subclasses"

Refactoring 21:collapse Hierarchy "Refactoring 21st: Merging inheritance hierarchies"

Refactoring 22:break Method "Refactoring the 22nd-type: Decomposition methods"

Refactoring 23:introduce Parameter Object "Refactoring type 23rd: Introducing Parameter Objects"

Refactoring 24:remove Arrowhead Antipattern "refactoring 24th: removing complex nesting condition judgments"

Refactoring 25:introduce Design by Contract checks "refactoring the 25th: introduction of the Contract-designed verification"

Refactoring 26:remove Double Negative "reconstructing the 26th type: eliminating double negation"

Refactoring 27:remove God Classes "reconstructing the 27th type: Removing the Gods Class"

Refactoring 28:rename Boolean method "refactoring the 28th: renaming Boolean methods"

Refactoring 29:remove Middle man "refactoring 29th: removing the Middleman"

Refactoring 30:return ASAP "Refactoring 30th: Return as soon as possible"

Refactoring 31:replace conditional with polymorphism "reconstruction 31st: Using polymorphic substitution conditional statement"


In the original English version, we provide the reconstruction example of C #, and the description of the refactoring method is more concise, sunny these examples into the Java version, and combines the individual understanding to complement and perfect the instance code and refactoring description appropriately. In this series of articles writing process, reference to the Kirin. NET translated version of the 31-day refactoring: You must know the refactoring skills and the Templars (Knights Warrior) of the "31 days of refactoring learning notes," here to express thanks.


--------------------------------------------------------------------------------------------------------------- -------------------------


refactoring the first type: Encapsulation Set (Refactoring 1:encapsulate Collection)

We know that the encapsulation of properties and methods can be done by setting their visibility, but how do you encapsulate the collection?

This refactoring provides a way to hide a collection of classes from the consumer (client) of a class, allowing the client class to access elements of the collection without directly modifying the contents of the collection, especially the Addxxx () method and Removexxx () of the original class. Method also contains some other code logic, exposing the collection to other classes and allowing these classes to modify the collection directly will cause the new business logic to expire in the Addxxx () method and the Removexxx () method.

Here is an example to illustrate:

"Refactoring Instances"

E-commerce Web sites usually have order management features that allow users to view details of each order, or to add and delete order items (OrderItem) on demand. Therefore, a set is defined in the Order class to store multiple OrderItem.

The code snippet before refactoring is as follows:[Java]  View Plain  copy   package sunny.refactoring.one.before;      import  java.util.collection;   import java.util.arraylist;     //Order    class order {       private double ordertotal; //Order Total Amount        private collection<orderitem> orderitems; //Collection Objects, Store all order items in one order               public order ()   {           this.orderitems = new arraylist <OrderItem> ();       }              //return Order item collection        public Collection<OrderItem>  getorderitems ()  {           return  This.orderitems;       }              // Return Order Total amount        public double getordertotal ()  {            return this.orderTotal;       }               //Increase order items while increasing the total order amount   

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.