Code refactoring and unit testing — continue to move the corresponding methods (8)

Time:2022-5-17
 

eight. restructure5: continue to move the corresponding method(Move Method

Pass“Code refactoring and unit testing — refactoring of “extraction method” (3)”To“Code refactoring and unit testing — refactoring the statement () method again with “replacing temporary variables with queries” (7)”After several refactorings of these articles, we observe and analyze the code of the charging item of the power bank again. We find that the code in the getamount() and getfrequentrenterpoints() methods of the rental class has a great dependence on the powerbank class. Because these two methods only use the rentedtime attribute in the rental class, and use the attribute or field in powerbank many times. Through the above analysis, we think it is more reasonable to move the code in these two methods to the powerbank class. Next, we continue to refactor these two methods — moving this part of the code into the powerbank class.

1. This time is the same as before“Code refactoring and unit testing — moving methods to appropriate [dependent] classes (6)”The mobile methods in this article are different. This time, the declarations of these two methods should be kept in the rental. Create a new method with the same method name in the powerbank class. After moving the contents of the method to powerbank, call the methods in the powerbank class in the rental. The following is the content of our powerbank class after this refactoring. The red code is the code we moved over, while the italic code is the parameter that needs to be passed in from the outside. The specific codes are as follows:

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 

namespace LeasePowerBank
{


    /// 
    ///Charging treasure
    /// 
    public class PowerBank
    {

        //Type of passenger flow in the section
        public static  int LowTraffic = 0;// Low traffic section 

        public static int MiddleTraffic = 1;// Medium traffic section
        public static  int HighTraffic = 2; // High traffic section


        public int PriceCode; // Price code
        public string Title;// Name of power bank
        public PowerBank(string title,int priceCode)
        {
            PriceCode = priceCode;
            Title = title;
        }
 
        /// 
        ///Calculate the points according to the consumption amount and the location of the power bank
        /// 
        ///Lease time
        /// 
        public int GetFrequentRenterPoints(int RentedTime)
        {
            int frequentRenterPoints = 0;
 
            decimal amount = GetAmount(RentedTime);
            //Calculate integral
            if (this.PriceCode == HighTraffic && RentedTime > 4)
            {
                frequentRenterPoints += (int)Math.Ceiling(amount * 1.5M);
            }
            else
                frequentRenterPoints += (int)Math.Ceiling(amount);
            return frequentRenterPoints;
        }
        /// 
        ///Calculate the total amount according to the order of power bank
        /// 
        ///Lease time
        /// 
        public decimal GetAmount(int RentedTime)
        {
            decimal amount = 0M;
 
            switch (this.PriceCode)
            {
                case 0:
                    amount = RentedTime;
                    if (RentedTime > 12)
                    {

                       amount = 12;
                    }
                    break;
                case 1:
                    amount = RentedTime * 3;
                    if (RentedTime > 24)
                    {
                        amount = 24;
                    }
                    break;
                case 2:
                    amount = RentedTime * 5;
                    if (RentedTime > 50)
                    {
                        amount = 50;
                    }
                    break;
                default:
                    break;
            }
            return amount;
        }
    }
}

 

2. After moving the corresponding method code into powerbank class, we need to call it in rental. When calling the corresponding method, you can pass in the corresponding parameters. As shown in the following figure, the code in the figure is the code of the rental class after this reconstruction, and the code in the red box is the call to the newly added method in powerbank. The code is as follows:

     

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 

namespace LeasePowerBank
{

    /// 
    ///Leasing
    /// 
    public class Rental
    {

      public  PowerBank Power ; // Name of power bank
        public int RentedTime;// Lease time
        public Rental(PowerBank powerbk,int rentedTime)
        {

            Power = powerbk;
            RentedTime = rentedTime;
        }


        /// 
        ///Calculate the points according to the consumption amount and the location of the power bank
        /// 
        /// 
        public int GetFrequentRenterPoints()
        {           
            return this.Power.GetFrequentRenterPoints(RentedTime);
        }
        /// 
        ///Calculate the total amount according to the order of power bank
        ///        
        /// 
       public decimal GetAmount()
        {
            return this.Power.GetAmount(RentedTime);
        }
    }
}

 

3. Our test cases remain unchanged. After each refactoring, we need to call the above test cases to check whether the refactoring has side effects. At present, the dependencies between our classes have not changed much, but the methods in the corresponding classes have changed. Find the menu item “test — > run all tests” on the menu bar of visual studio 2019. Or select “run all tests in view” button in “test Explorer” to run test cases. Monitor whether the reconstruction results are correct. As shown below.

 

 

Recommended Today

redis day2

persistence mechanism client redis[memory] —–> memory data-data persistence–>disk Redis officially provides two different persistence methods to store data in memory to the hard disk: Snapshot AOF (Append Only File) only appends log files   Snapshot Features:In this way, all data at a certain moment can be written to the hard disk, of course, this is […]