重构同样非常简单,以至于人们并不认为这是一个有价值的重构。迁移方法(Move Method),顾名思义就是将方法迁移到合适的位置。在开始重构前,我们先看看一下代码:

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 > ) return 0.02;
if (AccountAge > ) 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.CalculateInterest方法。当一个方法被其他类使用比在它所在类中的使用还要频繁时,我们就需要使用迁移方法重构了——将方法迁移到更频繁地使用它的类中。由于依赖关系,该重构并不能应用于所有实例,但人们还是经常低估它的价值。 最终的代码应该是这样的:
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 class AccountInterest {
public BankAccount Accountm{ 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 > ) return 0.02;
if (Account.AccountAge > ) return 0.03;
return 0.05;
}
}
简单吧。。
 
 

重构2-Move Method(方法移动)的更多相关文章

  1. 重构第2天:方法搬移(Move Method)

    现在就重构来说是非常普通的,虽然我们经常会漏掉或忽略一些需要重构的地方.方法搬移,正如所定义的那样,把方法搬移到更适合他的位置.让我们看看下面这一段重构前的代码: 理解:方法搬移,正如所定义的那样,把 ...

  2. 『重构--改善既有代码的设计』读书笔记----Move Method

    明确函数所在类的位置是很重要的.这样可以避免你的类与别的类有太多耦合.也会让你的类的内聚性变得更加牢固,让你的整个系统变得更加整洁.简单来说,如果在你的程序中,某个类的函数在使用的过程中,更多的是在和 ...

  3. 重构13天 抽取方法对象(Extract Method Object)

    理解:本文中的“提取方法对象”是指当你发现一个方法中存在过多的局部变量时,你可以通过使用“提取方法对象”重构来引入一些方法,每个方法完成任务的一个步骤,这样可以使得程序变得更具有可读性. 详解:如下代 ...

  4. 在Function对象上扩展method方法

    ;(function() { /** * 在Function对象上扩展method方法 * @param {String} name 扩展的方法名称 * @param {Function} callb ...

  5. QT实现appendSheet(QAxObject的一种Add + Move的方法)

    一般地,熟悉VB.VC的同学都知道,要将新增的excel表单添加到表单的末尾,是很简单的事情,直接调用Add函数,传入对应的函数形参,就能实现将新增表单插入到末尾,但是通过QT的QAxObject实现 ...

  6. 给对象和函数添加method方法

    蝴蝶书中有一个method方法,用来给函数定义方法.看了之后,想着能不能给对象也定义方法呢?. 下面的代码可以实现给函数定义方法: //Function method Function.prototy ...

  7. <s:submit> 指定的method方法不执行

    很多文章在讲一个表单多个提交方法的时候都是在<s:submit>中通过method来指定,但是我在试验中怎么也不对,jsp页面代码如下 <%@page import="or ...

  8. 《Java虚拟机原理图解》1.5、 class文件中的方法表集合--method方法在class文件中是怎样组织的

    0. 前言 了解JVM虚拟机原理是每一个Java程序员修炼的必经之路.但是由于JVM虚拟机中有很多的东西讲述的比较宽泛,在当前接触到的关于JVM虚拟机原理的教程或者博客中,绝大部分都是充斥的文字性的描 ...

  9. android4.4系统解决“ERRORcouldn&#39;t find native method”方法

    android4.4系统解决"ERRORcouldn't find native method"方法 今天笔者在移植一个tv模块从android4.2到android4.4系统的设 ...

  10. [CareerCup] 12.3 Test Move Method in a Chess Game 测试象棋游戏中的移动方法

    12.3 We have the following method used in a chess game: boolean canMoveTo( int x, int y). This metho ...

随机推荐

  1. Codeforces Round #292 (Div. 1) B. Drazil and Tiles (类似拓扑)

    题目链接:http://codeforces.com/problemset/problem/516/B 一个n*m的方格,'*'不能填.给你很多个1*2的尖括号,问你是否能用唯一填法填满方格. 类似t ...

  2. GetQueuedCompletionStatus的返回值

    完成端口GetQueuedCompletionStatus返回值的问题 先看看GetQueuedCompletionStatus函数的完整声明:BOOL GetQueuedCompletionStat ...

  3. HDU 1392 Surround the Trees 构造凸包

    又是一道模板题 #include <iostream> #include <cstring> #include <cstdlib> #include <cst ...

  4. react native listview 一个有用的属性,用作两列布局

    contentContainerStyle:设置listview包裹内容的属性 <ListView contentContainerStyle={{flexDirection:'row',fle ...

  5. Notes on Probabilistic Latent Semantic Analysis (PLSA)

    转自:http://www.hongliangjie.com/2010/01/04/notes-on-probabilistic-latent-semantic-analysis-plsa/ I hi ...

  6. Mahalanobis Distance(马氏距离)

    (from:http://en.wikipedia.org/wiki/Mahalanobis_distance) Mahalanobis distance In statistics, Mahalan ...

  7. Netty高并发原理

        Netty是一个高性能 事件驱动的异步的非堵塞的IO(NIO)框架,用于建立TCP等底层的连接,基于Netty可以建立高性能的Http服务器.支持HTTP. WebSocket .Protob ...

  8. CSS规则整理

    一. 善用css缩写规则 /*注意上.右.下.左的书写顺序*/1. 关于边距(4边):1px 2px 3px 4px (上.右.下.左)1px 2px 3px (省略的左等于右)1px 2px (省略 ...

  9. 配置Outlook Anywhere2010

    防火墙只需要开放CAS的443端口,其他硬件防火墙也是如此,不需要开放其他额外端口(80也没有必要开通,如果都使用https的话) 1.CAS:服务器配置-申请证书(内部.外部CAS名称)2.CAS: ...

  10. c# ActiveX 控件的开发

    关于ActiveX控件的开发,网上很多例子,昨天也整整研究一天才捋顺了. 网上大部分例子都是js调用控件的方法,由于要实现在html页面"相应"控件的事件,整整折腾一天. 关键点在 ...