重构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 ...
随机推荐
- js set
function ff(){ var sArray = ['a','b','b','b','b','b','d','b']; var iSet = {}; for(var i=0;i<sArra ...
- 小巧实用js倒计时
<script type="text/javascript"> var intDiff = parseInt(15); //倒计时总秒数量 functi ...
- 《高性能MySQL》
<高性能MySQL>(第3版)讲解MySQL如何工作,为什么如此工作? MySQL系统架构.设计应用技巧.SQL语句优化.服务器性能调优.系统配置管理和安全设置.监控分析,以及复制.扩展和 ...
- WSO2 ESB
什么是WSO2 ESB? WSO2 ESB是一个轻量级的易于使用的企业服务资源总线.WSO2 ESB允许系统管理员和SOA架构师,消息路由,虚拟化,中介,转换,日志记录,任务调度,负载平衡,失败了路由 ...
- linq 动态排序,不使用反射
之前网上搜索的相关方法都是使用了反射的方法来动态获取字段,以实现动态linq排序,但是因为项目组觉得此方法效率低下,所以不予采纳. 所以有了以下代码 public interface IBase{ d ...
- 快递查询API接口(trackingmore)
快递查询接口 目前提供快递查询的接口平台有: Trackingmore 快递100 快递网 不同接口的区别: (1)Trackingmore支持380家快递公司,其中有55家为国内的快递,其余325家 ...
- JavaScript闭包——实现
闭包的官方的解释是:一个拥有许多变量和绑定了这些变量的环境的表达式(通常是一个函数),因而这些变量也是该表达式的一部分. 通俗点的说法是: 从理论角度:所有的函数.因为它们都在创建的时候就将上层上下文 ...
- linux命令备忘
yum info softname 查看安装包信息 yum install softname 安装包名 安装 nginx http://jingyan.baidu.com/article/aa6a2c ...
- js设置控件的隐藏与显示的两种方法
js设置控件的隐藏与显示,设置控件style的display和visibility属性就可以了,下面有个示例,需要的朋友可以参考下用JavaScript隐藏控件的方法有两种,分别是通过设置控件的sty ...
- Codeforces Educational Codeforces Round 3 A. USB Flash Drives 水题
A. USB Flash Drives 题目连接: http://www.codeforces.com/contest/609/problem/A Description Sean is trying ...