Reconstruction type 29 ⅱ

Source: Internet
Author: User
Tags switch case

Rebuilding Study Notes 11. Using strategy

 

Concept: In this article, the "use strategy class" refers to replacing the original switch case and if else statements with the policy mode in the design mode, which can solve the coupling problem, it also greatly enhances maintainability and system scalability.

 

Body: as shown belowCodeAs shown in, the clientcode class will enumerate the value of State to call different methods of shippinginfo, but this will produce a lot of judgment statements. If the amount of Code increases, the class will become very large, during maintenance, the changes will also become very large. Every time you change a place, you must compile the entire structure (for example, multiple projects), so we thought of restructuring it, split coupling.

Namespace lostechies. daysofrefactoring. switchtostrategy. before {public class clientcode {public decimal calculateshipping () {shippinginfo = new shippinginfo (); Return shippinginfo. calculateshippingamount (state. alaska) ;}} public Enum state {Alaska, NewYork, Florida} public class shippinginfo {public decimal calculateshippingamount (State shiptostate) {Switch (shiptostate) {Case state. alaska: Return getalaskashippingamount (); Case state. newYork: Return getnewyorkshippingamount (); Case state. florida: Return getfloridashippingamount (); default: Return 0 m;} private decimal getalaskashippingamount () {return 15 m;} private decimal getnewyorkshippingamount () {return 10 m ;} private decimal getfloridashippingamount () {return 3 m ;}}}

 

The reconstructed code is as follows: Abstract An ishippingcalculation interface, and then extract the methods of volume, getnewyorkshippingamount, and getfloridashippingamount in the shippinginfo class into three classes, which are inherited from the ishippingcalculation interface, in this way, you can use ienumerable <ishippingcalculation> to cancel the previous switch case statement, which is similar to IOC.

 

Using system; using system. collections. generic; using system. LINQ; namespace reconstruction {public interface ishippinginfo {decimal calculateshippingamount (State state);} public class clientcode {[inject] public ishippinginfo shippinginfo {Get; set;} public decimal calculateshipping () {return shippinginfo. calculateshippingamount (state. alaska) ;}} public Enum state {Alaska, NewYork, Florida} public class shippinginfo: ishippinginfo {private idictionary <state, ishippingcalculation> shippingcalculations {Get; set ;} public shippinginfo (ienumerable <ishippingcalculation> shippingcalculations) {shippingcalculations = shippingcalculations. todictionary (calc => Calc. state);} public decimal calculateshippingamount (State shiptostate) {return shippingcalculations [shiptostate]. calculate () ;}} public interface ishippingcalculation {state {Get;} decimal calculate ();} public class alaskshippingcalculation: ishippingcalculation {public state {get {return state. alaska ;}} public decimal calculate () {return 15 m ;}} public class newyorkshippingcalculation: ishippingcalculation {public state {get {return state. newYork ;}} public decimal calculate () {return 10 m ;}} public class floridashippingcalculation: ishippingcalculation {public state {get {return state. florida ;}} public decimal calculate () {return 3 m ;}}}

Summary: In the design mode, This refactoring takes a separate name-policy mode. The advantage of this method is that the coupling can be separated to implement functions in the form of injection, this makes it easier and easier to add features, and also enhances the stability and robustness of the entire system.

 

 Rebuilding Study Notes 12. decomposing Dependencies

 

Concept: the "decomposition dependency" in this article refers to the dependency decomposition of some classes and methods that do not meet our requirements, and the functions we need can be achieved through the decorator.

 

Body: as shown in the following code, if you want to add a unit test to your code but some code does not want to be tested, then your application uses This refactoring. In the following example, we apply the static class to do some work, but the problem is that we cannot use the mock static class during unit testing, therefore, we can only introduce static class decoration interfaces to break down the dependencies on static classes. So that our call class can complete this operation only by relying on the decoration interface.

Namespace reconstruction {public class animalfeedingservice {private bool foodbowlempty {Get; set;} public void feed () {If (foodbowlempty) feeder. replenishfood (); // more code to feed the animal} public static class feeder {public static void replenishfood () {// fill up bowl }}}

 

After reconstruction, the Code is as follows. We add an interface and an implementation class to call the static class method in the implementation class. Therefore, the specific action is not changed, but the form is changed, but one advantage of this is the increased code testability.

After applying the decomposition dependency mode, we can mock an ifeederservice object during the unit test and pass it through the animalfeedingservice constructor. In this way, we can complete the functions we need.

Namespace reconstruction {public class detail {public ifeederservice feederservice {Get; set;} public response (ifeederservice feederservice) {feederservice = feederservice;} private bool foodbowlempty {Get; set ;} public void feed () {If (foodbowlempty) feederservice. replenishfood (); // more code to feed the animal} public interfce ifeederservice {void replenishfood ();} public class feederservice: ifeederservice {public void replenishfood () {feeder. replenishfood () ;}} public static class feeder {public static void replenishfood () {// fill up bowl }}}

Summary: in many cases, This refactoring is similar to some ideas in the design model. It uses the decoration interface in the middle to break down the dependencies between two classes and describe classes, then make it meet the features we need.

 

 Rebuilding learning notes 13. Extraction Method object 

Concept: the "extraction method object" in this article refers to some methods that can be introduced by refactoring the "extraction method object" when you find that there are too many local variables in a method, each method completes a step of the task, so thatProgramBecome more readable.

 

Body: as shown in the following code, the calculate method in the order class requires many functions. Before that, we used "Extraction Method" for reconstruction. Now we use "extraction method object" for reconstruction.

Namespace reconstruction {public class orderlineitem {public decimal price {Get; private set ;}} public class order {private ilist <orderlineitem> orderlineitems {Get; set ;} private ilist <decimal> discounts {Get; set;} private decimal tax {Get; set;} public decimal calculate () {decimal subtotal = 0 m; // total up line items foreach (orderlineitem lineitem in orderlineitems) {subtotal + = lineitem. price;} // subtract discounts foreach (decimal discount in discounts) Subtotal-= discount; // calculate tax decimal tax = subtotal * tax; // calculate grandtotal decimal grandtotal = subtotal + tax; return grandtotal ;}}}

 

As shown in the following code, we introduce the ordercalculator class, which implements all calculation methods. The order class passes itself to the ordercalculator class and calls the calculate method to complete the calculation process.

Namespace reconstruction {public class orderlineitem {public decimal price {Get; private set ;}} public class order {public ienumerable <orderlineitem> orderlineitems {Get; private set ;} public ienumerable <decimal> discounts {Get; private set;} public decimal tax {Get; private set;} public decimal calculate () {return New ordercalculator (this ). calculate () ;}} public class ordercalculator {private decimal subtotal {Get; set;} private ienumerable <orderlineitem> orderlineitems {Get; set;} private ienumerable <decimal> discounts {Get; set;} private decimal tax {Get; set;} public ordercalculator (order) {orderlineitems = order. orderlineitems; discounts = order. discounts; tax = order. tax;} public decimal calculate () {calculatesubtotal (); subtractdiscounts (); calculatetax (); Return subtotal;} private void calculatesubtotal () {// total up line items foreach (orderlineitem lineitem in orderlineitems) Subtotal + = lineitem. price;} private void subtractdiscounts () {// subtract discounts foreach (decimal discount in discounts) Subtotal-= discount;} private void calculatetax () {// calculate tax subtotal + = subtotal * Tax ;}}}

Summary: The refactoring method in this article is useful sometimes, but this will increase the number of fields and cause some maintenance inconvenience, the biggest difference between this method and "Extraction Method" is that one method returns the required data, and the other is to store the result value of the method through fields, therefore, to a large extent, we will select "extraction method ".

 

 Rebuilding Study Notes 14. separation of duties

Concept: in this article, "separation of duties" refers to separating some of the duties from independent classes when a class has many responsibilities, this also conforms to the single responsibility principle of one of the five characteristics of object-oriented, and can also make the code structure clearer and more maintainability.

Body: as shown in the following code, the video class has two responsibilities: one is to process video marshal, and the other is to calculate the total rent of each customer. We can separate these two responsibilities, because calculating the total rent of each customer can be calculated in the customer, which is also reasonable.

Namespace reconstruction {public class video {public void payrole (decimal role) {} public void publish video (Video video, customer) {customer. videos. add (video);} public decimal calculatebalance (customer) {return customer. latefees. sum () ;}} public class customer {public ilist <decimal> latefees {Get; set;} public ilist <video> videos {Get; Set ;}}}

The restructured code is as follows, so that the video's responsibilities become clear and code maintainability is also better.

 

Namespace reconstruction {public class video {public void Audio Video (Video video, customer) {customer. videos. add (video) ;}} public class customer {public ilist <decimal> latefees {Get; set;} public ilist <video> videos {Get; set ;} public void paypayment (decimal payment) {} public decimal calculatebalance (customer) {return customer. latefees. sum ();}}}

 

Summary: This refactoring is often used. It is similar to the previous "Mobile method", so that the method is placed in the appropriate class and the responsibilities of the class are simplified, at the same time, this is also one of the five principles of object-oriented and an important idea in the design model.

 

 Rebuilding Study Notes 15. Removing duplicate content

 

Concept: "Remove duplicate content" in this article refers to extracting the logic used in many places and providing it to callers for unified calls.

Summary: This refactoring is very simple. Most programmers will use this refactoring method. However, sometimes ignoring it due to habits, time, and schedule will make the entire system messy, there are traces of Ctrl + C and CTRL + V everywhere.

 

 Rebuilding learning notes 16. encapsulation Conditions

Concept: the "encapsulation condition" in this article means that the code is less readable when the condition relationship is complex, therefore, we should extract the conditional expression into a more readable attribute or method based on whether the conditional expression requires a parameter. If the conditional expression does not require a parameter, it can be extracted as an attribute, if a conditional expression requires a parameter, it can be extracted as a method.

 

Body: as shown in the following code, if condition judgment in javasmcoolfunction is complicated and looks a little messy, so we will propose it.

Namespace reconstruction {public class remotecontrol {private string [] functions {Get; set;} private string name {Get; set;} private int createdyear {Get; set ;} public String extends mcoolfunction (string buttonpressed) {// determine if we are controlling some extra function // that requires special conditions if (functions. length> 1 & name = "RCA" & createdyear> datetime. now. year-2) Return "dosomething"; else return "donothing ";}}}

As shown in the following code, we encapsulate the condition expression into the hasextrafunctions attribute, so that the previous condition judgment becomes if (hasextrafunctions), which greatly improves readability.

Namespace reconstruction {public class remotecontrol {private string [] functions {Get; set;} private string name {Get; set;} private int createdyear {Get; set ;} public String extends mcoolfunction (string buttonpressed) {If (hasextrafunctions) Return "dosomething"; else return "donothing";} private bool hasextrafunctions {get {return functions. length> 1 & name = "RCA" & createdyear> datetime. now. year-2 ;}}}}

Conclusion: This refactoring can greatly improve the readability of the Code, especially in a complicated logic application, which encapsulates these condition judgments into a meaningful name, in this way, complicated logic will become simple immediately.

 

Rebuilding learning notes 17. Extracting parent classes

 

Concept: "extract parent class" in this article refers to some fields or methods in the class. You want to extract them to the parent class so that other classes at the same inheritance level can access them, this is similar to many previous refactoring operations.

Summary: This refactoring is a typical inheritance usage. Many programmers will choose to do so. However, pay attention to the correct use and avoid excessive use of inheritance, consider using interfaces, combinations, and aggregation.

 

Rebuilding study notes 18. using conditional judgment instead of exceptions

Concept: in this article, "using conditional judgment instead of exception" refers to changing the conditions that do not need to use exceptions for judgment as much as possible to conditional judgment.

Summary: This refactoring is often used in the project code, because it is difficult for some programmers to determine when to use try catch and where to use try catch. I remember that you have discussed this before, for example, how to make good use of it and what components should be put in large and medium-sized projects.

 

Rebuilding Study Notes 19. Extracting factory classes

Concept: the "extract factory class" in this article means that if there are many objects to be created, the code will become very complicated. A good method is to extract the factory class.

Body: in general, we need to set some objects in the code to get their States and use objects. The so-called setting is usually to create an instance of the object and call the object method. Sometimes, if you want to create many objects, the code will become very complex. This is where the factory model works. The complex application of the factory mode creates an object set using the abstract factory, but here we just use the basic factory class to create a simple application of the object.

The following code shows that the new method contains the entire logic for creating a class. If there are many classes to be created and the logic is complex (for example, when to create an object based on different conditions ), our new method logic will become very large, and the code will become very difficult to maintain. So we will extract the factory class for extraction.

Namespace reconstruction {public class effececarcontroller {public effececar new (INT mileage, bool servicerequired) {effececar = new effececar (); effececar. servicerequired = servicerequired; policecar. mileage = mileage; return policecar;} public class incluecar {public bool servicerequired {Get; Set ;} public int mileage {Get; Set ;}}}

The restructured code is as follows. The new method becomes very simple. It means that the returned object can be returned by calling the javasecarfactory class that implements the ipolicecarfactory interface, thus separating the logic of creating objects, if the requirement is to create different objects according to different conditions, it is easy to create objects at any time. In the future, you can configure all objects in XML, use reflection to create IOC injection.

Namespace reconstruction {public interface ipolicecarfactory {effececar create (INT mileage, bool servicerequired);} public class effececarfactory: ipolicecarfactory {public effececar create (INT mileage, bool servicerequired) {symbol ECAR = new symbol ECAR (); symbol ECAR. servicerequired = servicerequired; policecar. mileage = mileage; return policecar;} public class extends ecarcontroller {public ipolicecarfactory extends ecarfactory {Get; set;} public extends ecarcontroller (ipolicecarfactory extends ecarfactory) {export ecarfactory = export ecarfactory ;} public role ECAR new (INT mileage, bool servicerequired) {return role ecarfactory. create (mileage, servicerequired) ;}} public class incluecar {public bool servicerequired {Get; Set ;}public int mileage {Get; Set ;}}}

Summary: This refactoring is often used in projects. If you want to create an object, you can use a simple factory. However, this method still has a lot of dependencies and is inconvenient to maintain. We recommend that you use the factory method mode to delay Instantiation to subclass. If you want to create a series of objects, it is recommended that you use the abstract factory mode, but be careful not to over-design, as long as it can meet the changing needs and bring convenience to future maintenance and reconstruction.

 

 Rebuilding Study Notes 20. Extracting child classes

 

 Concept: "extract subclass" in this article refers to adjusting some methods in the base class that are not accessed by all subclasses to the subclass.

 

Body: when some methods in your base class do not need to be accessed by all subclasses and you want to adjust them to subclass, This refactoring will become very useful. As shown in the following code, we need a registry class to process the information about the course selection for students. However, when the registration class starts to work, we realize that we will use the registration class in two different contexts. nonregistrationaction and notes will only be used when we process unregistered content.

Summary: This refactoring method is often used for the role class, and is similar to some previous refactoring methods.

 

Reference: Temple knight

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.