zoukankan      html  css  js  c++  java
  • 重构改善既有代码设计--重构手法11:Move Field (搬移字段)

    你的程序中,某个字段被其所驻类之外的另一个类更多的用到。在目标类建立一个新字段,修改源字段的所有用户,令它们改用新字段。

           动机:在类之间移动状态和行为,是重构过程中必不可少的措施。随着系统发展,你会发现自己需要新的类,并需要将现有的工作责任拖到新的类中。在这个星期看似合理而正确的设计决策,到了下个星期可能不再正确。这没问题,如果你从来没遇到这种情况,那才有问题。

           如果发现对于一个字段,在其所驻类之外的另一个类中有更多函数使用了它,就考虑搬移这个字段。上述所谓“使用”可能是通过设值/取值函数间接进行的。也可能移动该字段的用户(某个函数),这取决于是否需要保持接口不受变化。如果这些函数看上去很适合待在原地,就选择搬移字段。

           使用Extract Class (提炼类)时,也可能需要搬移字段。此时可以先搬移字段,然后搬移函数。

    做法:1、如果字段的访问级别是public,使用 Encapsulated Field (封装字段)将它们封装起来。如果你有可能移动那些频繁访问该字段的函数,或如果有许多函数访问某个字段,先使用 Self Encapsulate Field (自封装字段)也许会有帮助。

          2、编译、测试。

          3、在目标类中建立于源字段相同的字段,并同时建立相应的设值/取值函数。

          4、编译目标类。

          5、决定如何在源对象中引用目标对象。首先看是否有一个现成的字段或函数可以助你得到目标对象。如果没有,就看能否轻易建立这样一个函数。如果还不行,就得在源类中新建一个字段来存放目标对象。这可能是个永久性修改,但你也可以让它是暂时的。因为后续重构可能会把这个新建字段除掉。

          6、删除源字段。

          7、将所有对源字段的引用替换为某个目标函数的调用。如果需要读取该变量,就把对源字段的引用替换为对设值函数的调用。如果源字段不是private的,就必须在源类的所有子类中查找源字段的引用点,并进行相应替换。

         8、编译、测试。

    下面是Account class的部分代码:
    class Account...
        private AccountType _type;
        private double _interestRate;
        double interestForAmount_days(double amount, int days) {
           return _interestRate * amount * days / 365;
        }

    我想把表示利率的_interestRate搬移到AccountType class去。目前已有数个函数引用了它,interestForAmount_days()就是其一。下一步我要在AccountType中建立_interestRate field以及相应的访问函数:
    class AccountType...
        private double _interestRate;

        void setInterestRate(double arg) {
           _interestRate = arg;
        }
        double getInterestRate() {
           return _interestRate;
        }

    这时候我可以编译新的 AccountType class。
    现在,我需要让Account class中访问_interestRate field的函数转而使用AccountType对象,然后删除Account class中的_interestRate field。我必须删除source field,才能保证其访问函数的确改变了操作对象,因为编译器会帮我指出未正确获得修改的函数。

    class Account...
        private double _interestRate;
        double interestForAmount_days(double amount, int days) {
           return _type.getInterestRate() * amount * days / 365;
        }

    如果有很多函数已经使用了_interestRate field,我应该先运用Self Encapsulate Field(171):

    class Account...
        private AccountType _type;
        private double _interestRate;
        double interestForAmount_days(double amount, int days) {
           return getInterestRate() * amount * days / 365;
        }
        private void setInterestRate(double arg) {
           _interestRate = arg;
        }
        private double getInterestRate() {
           return _interestRate;
        }

    这样,在搬移field之后,我就只需要修改访问函数就行了:

        double interestForAmount_days(double amount, int days) {
           return getInterestRate() * amount * days / 365;
        }
        private void setInterestRate(double arg) {
           _type.setInterestRate(arg);
        }
        private double getInterestRate() {
           return _type.getInterestRate();
        }

    如果以后有必要,我可以修改访问函数(accessors)的用户,让它们使用新对象。Self Encapsulate Field(171)使我得以保持小步前进。如果我需要对class做许多处理,保持小步前进是有帮助的。特别值得一提的是:首先使用Self Encapsulate Field(171)使我得以更轻松使用Move Method(142)将函数搬移到target class中。如果待移函数引用了field的访问函数(accessors),那么那些引用点是无须修改的。

  • 相关阅读:
    Kubernetes List-Watch
    Go 模板语法
    vRA7 Business error “Untrusted certificate chain”
    Centos 7/8 安装 Harbor
    Kubernetes Headless Service
    Kubernetes addon-manager
    Kubernetes Metrics-Server
    Kubernetes Heapster
    容器rootfs
    Kubernetes lxcfs
  • 原文地址:https://www.cnblogs.com/pony1223/p/7530450.html
Copyright © 2011-2022 走看看