重构2-Move Method(方法移动)
重构同样非常简单,以至于人们并不认为这是一个有价值的重构。迁移方法(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;
}
}
}
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(方法移动)的更多相关文章
- 重构第2天:方法搬移(Move Method)
现在就重构来说是非常普通的,虽然我们经常会漏掉或忽略一些需要重构的地方.方法搬移,正如所定义的那样,把方法搬移到更适合他的位置.让我们看看下面这一段重构前的代码: 理解:方法搬移,正如所定义的那样,把 ...
- 『重构--改善既有代码的设计』读书笔记----Move Method
明确函数所在类的位置是很重要的.这样可以避免你的类与别的类有太多耦合.也会让你的类的内聚性变得更加牢固,让你的整个系统变得更加整洁.简单来说,如果在你的程序中,某个类的函数在使用的过程中,更多的是在和 ...
- 重构13天 抽取方法对象(Extract Method Object)
理解:本文中的“提取方法对象”是指当你发现一个方法中存在过多的局部变量时,你可以通过使用“提取方法对象”重构来引入一些方法,每个方法完成任务的一个步骤,这样可以使得程序变得更具有可读性. 详解:如下代 ...
- 在Function对象上扩展method方法
;(function() { /** * 在Function对象上扩展method方法 * @param {String} name 扩展的方法名称 * @param {Function} callb ...
- QT实现appendSheet(QAxObject的一种Add + Move的方法)
一般地,熟悉VB.VC的同学都知道,要将新增的excel表单添加到表单的末尾,是很简单的事情,直接调用Add函数,传入对应的函数形参,就能实现将新增表单插入到末尾,但是通过QT的QAxObject实现 ...
- 给对象和函数添加method方法
蝴蝶书中有一个method方法,用来给函数定义方法.看了之后,想着能不能给对象也定义方法呢?. 下面的代码可以实现给函数定义方法: //Function method Function.prototy ...
- <s:submit> 指定的method方法不执行
很多文章在讲一个表单多个提交方法的时候都是在<s:submit>中通过method来指定,但是我在试验中怎么也不对,jsp页面代码如下 <%@page import="or ...
- 《Java虚拟机原理图解》1.5、 class文件中的方法表集合--method方法在class文件中是怎样组织的
0. 前言 了解JVM虚拟机原理是每一个Java程序员修炼的必经之路.但是由于JVM虚拟机中有很多的东西讲述的比较宽泛,在当前接触到的关于JVM虚拟机原理的教程或者博客中,绝大部分都是充斥的文字性的描 ...
- android4.4系统解决“ERRORcouldn't find native method”方法
android4.4系统解决"ERRORcouldn't find native method"方法 今天笔者在移植一个tv模块从android4.2到android4.4系统的设 ...
- [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 ...
随机推荐
- Keil MDK Code、RO-data、RW-data、ZI-data数据段
Program Size: Code=10848 RO-data=780 RW-data=372 ZI-data=868 Code 表示程序代码指令部分 存放在Flash区 RO-data 表 ...
- System.Data.SqlTypes.SqlNullValueException: 数据为空。不能对空值调用此方法或
有可能读出的数据为NULL,可以这样改: 方法一:while (reader.Read()){ for (int i = 0; i < 7; i++) { if (reader.IsDBNull ...
- springAOP配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- hdu 3397 Sequence operation(很有意思的线段树题)
Sequence operation Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- Django官方文档学习2——数据库及模板
网址:https://docs.djangoproject.com/en/1.10/intro/tutorial02/ 1.扫描installed_apps,创建需要的数据库table python ...
- MFC中关于子对话框中编辑框不能编辑的问题
最近在用MFC写程序.发现子对话框中的编辑框不能编辑.具体问题是这样的: 我有一个对话框YhglDlg,创建了这个对话框的子对话框ZjyhxxDlg,子对话框的Style属性为Child,Border ...
- 从零开始学C++之虚函数与多态(一):虚函数表指针、虚析构函数、object slicing与虚函数
一.多态 多态性是面向对象程序设计的重要特征之一. 多态性是指发出同样的消息被不同类型的对象接收时有可能导致完全不同的行为. 多态的实现: 函数重载 运算符重载 模板 虚函数 (1).静态绑定与动态绑 ...
- 2014年QS世界大学排名
新浪教育[微博]讯 近日2014QS世界大学排行榜发布,榜单前十强均为英美名校.其中麻省理工大学以绝对优势位居榜首:英国剑桥大学及帝国理工学院并列排名第二:哈佛大学较去年而言名次略微下降,跌至第四. ...
- 【转】Google推荐的命名规则——Android图片资源
http://blog.csdn.net/yy1300326388/article/details/45443477 1.译 资产类型 前缀 例子 图标 ic_ ic_star.png 启动图标 ic ...
- hdu1213 How Many Tables
How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...