ivaneeo's blog

自由的力量,自由的生活。

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  669 Posts :: 0 Stories :: 64 Comments :: 0 Trackbacks
如果有很多函数已经使用了_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),那么那些引用点是无须修改的。

posted on 2005-08-30 14:59 ivaneeo 阅读(116) 评论(0)  编辑  收藏 所属分类: refactoring-从地狱中重生

只有注册用户登录后才能发表评论。


网站导航: