zoukankan      html  css  js  c++  java
  • 重构:Move Method 笔记

    重构:Move Method

               Move Method就是将方法迁移到合适的位置。

               条件:当一个方法被其他类使用比在它所在类中的使用还要频繁时,我们就需要将方法迁移到更频繁地使用它的类中(Move Method重构)

               范例如下:

    修改前代码
      public class BankAccount
        {
            
    public BankAccount(int accountNum)
            {
                AccountNum 
    = accountNum;
            }
            
    public int AccountNum { getprivate set; }
          
      public double CalculateInterestRate()
            {
                
    if (AccountNum > 800)
                    
    return 0.02;
                
    if (AccountNum > 10&&AccountNum<=800)
                    
    return 0.03;
                
    return 0.05;
            }
        }

        
    public class AccountInterest
        {
            
    public BankAccount Account { getprivate set; }
            
    public AccountInterest(BankAccount account)
            {
                Account 
    = account;
            }
            
    public double InterestRate
            {
                
    get { return Account.CalculateInterestRate(); }

            }
            
    public bool IntroductoryRate
            {
                
    get { return Account.CalculateInterestRate() < 0.05; }
            }

        }

          发现后者比前者使用标注中的方法的频率更高,因此将该方法移动至后者中。 

          修改后代码:

    修改后代码
        public class BankAccount
        {
            
    public BankAccount(int accountNum)
            {
                AccountNum 
    = accountNum;     
            }
            
    public int AccountNum { getprivate set; }
        }
        
    public class AccountInterest
        {
            
    public BankAccount Account { getprivate set; }
            
    public AccountInterest(BankAccount account)
            {
                Account 
    = account;
            }
            public double InterestRate
            {
                
    get { return CalculateInterestRate(); }
            }
            
    public bool IntroductoryRate
            {
                
    get { return CalculateInterestRate() < 0.05; }
            }
            
    public double CalculateInterestRate()
            {
                
    if (Account.AccountNum > 800)
                    
    return 0.02;
                
    if (Account.AccountNum > 10 && Account.AccountNum <= 800)
                    
    return 0.03;
                
    return 0.05;
            }
        }

          代码操作更加清晰,直观。功能单一化。

  • 相关阅读:
    ORA-28000 帐户已被锁定问题处理
    德邦总管 修改oracle数据库用户密码的方法
    测试面试题目
    python 把一文件包含中文的字符写到另外文件乱码 UnicodeDecodeError: 'gbk' codec can't decode byte 0xac in position
    python version 2.7 required,which was not found in the registry
    ImportError: No module named dateutil.parser
    解决python2.x用urllib2证书验证错误, _create_unverified_context
    python多版本兼容性问题:当同时安装Python2和Python3后,如何兼容并切换
    MAC OS git客户端安装及操作
    Pycharm连接gitlab
  • 原文地址:https://www.cnblogs.com/jasenkin/p/1799382.html
Copyright © 2011-2022 走看看