• 31天重构学习笔记2. 移动方法


    摘要:由于最近在做重构的项目,所以对重构又重新进行了一遍学习和整理,对31天重构最早接触是在2009年10月份,由于当时没有订阅Sean Chambers的blog,所以是在国外的社区上闲逛的时候链接过去的。记得当时一口气看完了整个系列并没有多少感觉,因为这些基本上项目都在使用,只是我们没有专门把它标示和整理出来,所以也没有引起多大的重视。现在突然接手这个重构项目,由于团队成员技术和经验参差不齐,所以有必要专门整理一个重构的纲要,当然这个系列也非常适合做新系统的代码规范参考,只要有代码的地方,这个重构规范就很有价值。周末也不想出去闲逛,因为在刚到这个美丽的城市,没有亲戚或者朋友,所以就才能静下心来两天时间写完这个重构参考规范。同时也感受了Windows Live writer写文章的快感。当然重构的整体的架构得另当别论(整体架构在我的这篇文章有专门的讲解(http://www.cnblogs.com/zenghongliang/archive/2010/06/23/1763438.html)。大的架构设计好了以后,这些重构小点就成了点将之后的大军作战了,所以这些重构小点对整个项目也是至关重要。31天重构这个系列和《代码大全》、《重构:改善既有代码的设计》比较起来最大的特点就是比较简单、浅显易懂。那么我这些文章也都是学习Sean Chambers的31天重构的笔记整理,所以如果大家对这个笔记有任何异议也可以指出,具体也可以通过http://www.lostechies.com/blogs/sean_chambers/archive/2009/07/31/31-days-of-refactoring.aspx查看原文。

    概念:本文所讲的移动方法就是方法放在合适的位置(通常指放在合适的类中)。

    正文:移动方法是一个很简单也很常见的重构,只要是系统就会存在很多类,那么类里面包括很多方法,如果一个方法经常被另外一个类使用(比本身的类使用还多)或者这个方法本身就不应该放在这个类里面,那么这个适合应该考虑把它移到合适的类中。代码如下:

    namespace LosTechies.DaysOfRefactoring.MoveMethod.Before
    {
    public class BankAccount
    {
    public BankAccount(int accountAge, int creditScore, AccountInterest accountInterest)
    {
    AccountAge = accountAge;
    CreditScore = creditScore;
    AccountInterest = accountInterest;
    }

    public int AccountAge { get; private set; }
    public int CreditScore { get; private set; }
    public AccountInterest AccountInterest { get; private set; }

    public double CalculateInterestRate()
    {
    if (CreditScore > 800)
    return 0.02;

    if (AccountAge > 10)
    return 0.03;

    return 0.05;
    }
    }

    public class AccountInterest
    {
    public BankAccount Account { get; private set; }

    public AccountInterest(BankAccount account)
    {
    Account = account;
    }

    public double InterestRate
    {
    get { return Account.CalculateInterestRate(); }
    }

    public bool IntroductoryRate
    {
    get { return Account.CalculateInterestRate() < 0.05; }
    }
    }
    }
     
    移动以后大家可以看到BankAccount类的职责也单一,同时CalculateInterestRate也放到了经常使用且适合它的类中了,所以此重构是一个比较好的重构,能让整个代码变得更加合理。
    namespace LosTechies.DaysOfRefactoring.MoveMethod.After
    {
    public class AccountInterest
    {
    public BankAccount Account { get; private 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.CreditScore > 800)
    return 0.02;

    if (Account.AccountAge > 10)
    return 0.03;

    return 0.05;
    }
    }
    }

    namespace LosTechies.DaysOfRefactoring.MoveMethod.After
    {
    public class BankAccount
    {
    public BankAccount(int accountAge, int creditScore, AccountInterest accountInterest)
    {
    AccountAge = accountAge;
    CreditScore = creditScore;
    AccountInterest = accountInterest;
    }

    public int AccountAge { get; private set; }
    public int CreditScore { get; private set; }
    public AccountInterest AccountInterest { get; private set; }
    }
    }

    这个重构法则在很多时候能让我们把代码组织的结构调整得更合理,同时也能给以后的维护带来方便。

  • 相关阅读:
    使用静态全局对象自动做初始化与清理工作
    ThinkpadR617755BH1安装Mac Leopard10.5.2
    ubuntu常用快捷键
    linux常用命令
    c++对象内存模型【内存对齐】
    将ubuntu引导项加入windowsXP启动菜单中
    ISO C++委员会批准C++0x最终草案
    图片转eps格式
    Latex 点滴记录
    我是一个硬盘
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/1766517.html
Copyright © 2020-2023  润新知