重构——一个小例子
菜鸟区域,老鸟绕路!
原代码,这是一个可以借阅影片的小程序,你可以想象成某个大型系统,我想代码应该都能很容易看懂:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace Reconsitution
{
public class Movie
{
/// <summary>
/// 常规的
/// </summary>
public const int Regular = 0;
/// <summary>
/// 新发布
/// </summary>
public const int NewRelease = 1;
/// <summary>
/// 儿童版
/// </summary>
public const int Childrens = 2; public string Title { get; private set; }
public int PriceCode { get; set; } public Movie(string title, int priceCode)
{
Title = title;
PriceCode = priceCode;
}
} public class Rental
{
public Movie @Movie { get; private set; }
public int DaysRented { get; private set; } public Rental(Movie movie, int daysRented)
{
@Movie = movie;
DaysRented = daysRented;
}
} public class Customer
{
public string Name { get; private set; } public List<Rental> Rentals
{
get { return _rentals; }
}
private readonly List<Rental> _rentals = new List<Rental>(); public Customer(string name)
{
Name = name;
} public void AddRental(Rental rental)
{
_rentals.Add(rental);
} public string Statement()
{
double totalAmount = 0;
int frequentRenterPoints = 0;
string result = "Rental Record for " + Name + "\n"; foreach (Rental rental in _rentals)
{
double thisAmount = 0; switch (rental.Movie.PriceCode)
{
case Movie.Regular:
thisAmount += 2;
if (rental.DaysRented > 2)
thisAmount += (rental.DaysRented - 2)*1.5;
break;
case Movie.NewRelease:
thisAmount += rental.DaysRented*3;
break;
case Movie.Childrens:
thisAmount += 1.5;
if (rental.DaysRented > 3)
thisAmount += (rental.DaysRented - 3) * 1.5;
break;
} frequentRenterPoints ++;
if ((rental.Movie.PriceCode == Movie.NewRelease) && rental.DaysRented > 1)
frequentRenterPoints ++; result += "\t" + rental.Movie.Title + "\t" + thisAmount + "\n";
totalAmount += thisAmount;
} result += "Amount owed is " + totalAmount + "\n";
result += "You earned " + frequentRenterPoints + " frequent renter points";
return result;
}
}
}
Customer customer = new Customer("Dennis Ding");
customer.AddRental(new Rental(new Movie("Water World", Movie.Childrens), 5));
customer.AddRental(new Rental(new Movie("Roman Holiday", Movie.Regular), 3));
customer.AddRental(new Rental(new Movie("The First Blood", Movie.NewRelease), 6));
TextBox1.Text = customer.Statement();
一、拆解主体方法
简要预览代码,我们很直观的发现 Customer 中的方法 Statement 过长,不便于阅读,于是我们首先便考虑解体方法 Statement,我们可以这样考虑,为什么要重构,重构再于便于后期维护和功能拓展,对于Statement方法,生成一个文字版的影片借阅结果单,如果我们版本更新,要生成一个Html的结果单,或者直接返回数据Json,形成接口供第三方调用呢,我们不得不重新写一个叫HtmlStatement 或者 JsonStatement的方法,因为我们把细节到计算的整个生成逻辑多放在了这一个方法中了!这是一种很不合理的设计!
public string Statement()
{
double totalAmount = 0;
int frequentRenterPoints = 0;
string result = "Rental Record for " + Name + "\n"; foreach (Rental rental in _rentals)
{
var thisAmount = AmountFor(rental); frequentRenterPoints ++;
if ((rental.Movie.PriceCode == Movie.NewRelease) && rental.DaysRented > 1)
frequentRenterPoints ++; result += "\t" + rental.Movie.Title + "\t" + thisAmount + "\n";
totalAmount += thisAmount;
} result += "Amount owed is " + totalAmount + "\n";
result += "You earned " + frequentRenterPoints + " frequent renter points";
return result;
} private static double AmountFor(Rental rental)
{
double thisAmount = 0; switch (rental.Movie.PriceCode)
{
case Movie.Regular:
thisAmount += 2;
if (rental.DaysRented > 2)
thisAmount += (rental.DaysRented - 2)*1.5;
break;
case Movie.NewRelease:
thisAmount += rental.DaysRented*3;
break;
case Movie.Childrens:
thisAmount += 1.5;
if (rental.DaysRented > 3)
thisAmount += (rental.DaysRented - 3)*1.5;
break;
}
return thisAmount;
}
这时候,我们发现,方法 AmountFor 并没有使用到和 Customer 相关的数据,但却放在 Customer 中,于是我们先考虑吧方法移到 Rental 中,
public class Rental
{
public Movie Movie { get; private set; }
public int DaysRented { get; private set; } public Rental(Movie movie, int daysRented)
{
Movie = movie;
DaysRented = daysRented;
} public double AmountFor()
{
double thisAmount = 0; switch (this.Movie.PriceCode)
{
case Movie.Regular:
thisAmount += 2;
if (DaysRented > 2)
thisAmount += (DaysRented - 2) * 1.5;
break;
case Movie.NewRelease:
thisAmount += DaysRented * 3;
break;
case Movie.Childrens:
thisAmount += 1.5;
if (DaysRented > 3)
thisAmount += (DaysRented - 3) * 1.5;
break;
}
return thisAmount;
}
}
原 Statement 方法变成 :
public string Statement()
{
double totalAmount = 0;
int frequentRenterPoints = 0;
string result = "Rental Record for " + Name + "\n"; foreach (Rental rental in _rentals)
{
var thisAmount = rental.AmountFor(); frequentRenterPoints ++;
if ((rental.Movie.PriceCode == Movie.NewRelease) && rental.DaysRented > 1)
frequentRenterPoints ++; result += "\t" + rental.Movie.Title + "\t" + thisAmount + "\n";
totalAmount += thisAmount;
} result += "Amount owed is " + totalAmount + "\n";
result += "You earned " + frequentRenterPoints + " frequent renter points";
return result;
}
(为了让方法符合实际功能,我们可以把方法 AmountFor 命名为 GetCharge)
在方法Statement中,此时我们看到这样的代码
var thisAmount = rental.GetCharge(); //……被省略的代码
result += "\t" + rental.Movie.Title + "\t" + thisAmount + "\n";
totalAmount += thisAmount;
由于 rental.GetCharge() 被使用两次,为了避免二次计算,代码中使用了一个临时变量 thisAmount,于是乎就产生了一个《重构与性能》的话题,这是一个很大的话题,所以我暂时也没深入学习,在重构中,我们可以去掉这样的临时变量(一两个不可怕,但是在一个项目中过度的临时变量使得代码无比混乱),实际上我们去掉这样的临时变量,可以尝试从 Rental 类中寻求优化方法。至于《重构与性能》后面介绍!
代码如下:
//……被省略的代码
result += "\t" + rental.Movie.Title + "\t" + rental.GetCharge() + "\n";
totalAmount += rental.GetCharge();
再往后,Statement方法中,有这样的代码:
frequentRenterPoints ++;
if ((rental.Movie.PriceCode == Movie.NewRelease) && rental.DaysRented > 1)
frequentRenterPoints ++;
frequentRenterPoints 是一个在方法中定义的临时变量(阿飘),这小部分代码的作用也不过是计算积分(现在的各种商店都有积分),这两句代码只是积分的计算方式,和实际生成逻辑没有关联,于是我们考虑把它拆出来。
public string Statement()
{
double totalAmount = 0;
int frequentRenterPoints = 0;
string result = "Rental Record for " + Name + "\n"; foreach (Rental rental in _rentals)
{
frequentRenterPoints += GetFrequentRenterPoints(rental); result += "\t" + rental.Movie.Title + "\t" + rental.GetCharge() + "\n";
totalAmount += rental.GetCharge();
} result += "Amount owed is " + totalAmount + "\n";
result += "You earned " + frequentRenterPoints + " frequent renter points";
return result;
} private int GetFrequentRenterPoints(Rental rental)
{
if ((rental.Movie.PriceCode == Movie.NewRelease) && rental.DaysRented > 1)
return 2;
return 1;
}
接下来,我们接着处理“阿飘”的问题,对于临时变量,能少用则少用,这是某某牛人给出的重构名言一则。
代码中 totalAmount 的计算可以单独封装为:
private double GetTotalAmount()
{
return _rentals.Sum(rental => rental.GetCharge());
}
到此时,Statement方法已经比较简洁了,我们Copy再稍加修改就可以提供HtmlStatement和JsonStatement了,因为方法里面的计算逻辑已经被我们提取出来!
二、拆分后的方法调整
下一步,对于刚刚拆解的方法GetCharge,主体为一个switch,而switch却依赖对象 Movie的一个成员PriceCode,既然这样,为什么不把计算方式迁移到Movie类呢:
public double GetCharge()
{
double thisAmount = 0; switch (this.Movie.PriceCode)
{
case Movie.Regular:
thisAmount += 2;
if (DaysRented > 2)
thisAmount += (DaysRented - 2) * 1.5;
break;
case Movie.NewRelease:
thisAmount += DaysRented * 3;
break;
case Movie.Childrens:
thisAmount += 1.5;
if (DaysRented > 3)
thisAmount += (DaysRented - 3) * 1.5;
break;
}
return thisAmount;
}
于是我们尝试在Movie中新建:
public double GetCharge(int daysRented)
{
double thisAmount = 0; switch (PriceCode)
{
case Movie.Regular:
thisAmount += 2;
if (daysRented > 2)
thisAmount += (daysRented - 2) * 1.5;
break;
case Movie.NewRelease:
thisAmount += daysRented * 3;
break;
case Movie.Childrens:
thisAmount += 1.5;
if (daysRented > 3)
thisAmount += (daysRented - 3) * 1.5;
break;
}
return thisAmount;
}
原方法则可用:
public double GetCharge(int daysRented)
{
return Movie.GetCharge(DaysRented);
}
然后看Customer中方法:
private int GetFrequentRenterPoints(Rental rental)
{
if ((rental.Movie.PriceCode == Movie.NewRelease) && rental.DaysRented > 1)
return 2;
return 1;
}
我们可以迁移到Rental中:
public int GetFrequentRenterPoints()
{
if ((Movie.PriceCode == Movie.NewRelease) && DaysRented > 1)
return 2;
return 1;
}
接着发现和上面同样使用了Movie的成员,尝试移动到Movie中:
public int GetFrequentRenterPoints(int daysRented)
{
if ((PriceCode == Movie.NewRelease) && daysRented > 1)
return 2;
return 1;
}
Rental中可以这样:
public int GetFrequentRenterPoints()
{
return Movie.GetFrequentRenterPoints(DaysRented);
}
和之前的GetTotalAmount方法一样,我们也可以有个GetTotalFrequentRenterPoints:
private double GetTotalFrequentRenterPoints()
{
return _rentals.Sum(rental => rental.GetFrequentRenterPoints());
}
于是Statement就表示为:
public string Statement()
{
string result = "Rental Record for " + Name + "\n"; foreach (Rental rental in _rentals)
{
result += "\t" + rental.Movie.Title + "\t" + rental.GetCharge() + "\n";
} result += "Amount owed is " + GetTotalAmount() + "\n";
result += "You earned " + GetTotalFrequentRenterPoints() + " frequent renter points";
return result;
}
三、大的变化——继承
例子中,我们的Movie类分成三个类型
/// <summary>
/// 常规的
/// </summary>
public const int Regular = 0;
/// <summary>
/// 新发布
/// </summary>
public const int NewRelease = 1;
/// <summary>
/// 儿童版
/// </summary>
public const int Childrens = 2;
从而出现了方法 GetCharge 中计算的条件判断,当我们做同一件事使用不同的方式时候,我们想到了继承!对不同的电影类型,有不同的价格,我们认为价格是一个基类,每种电影的价格为子类,最终代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace Reconsitution
{
public class Movie
{
#region 常量电影分类 /// <summary>
/// 常规的
/// </summary>
public const int Regular = 0;
/// <summary>
/// 新发布
/// </summary>
public const int NewRelease = 1;
/// <summary>
/// 儿童版
/// </summary>
public const int Childrens = 2; #endregion public string Title { get; private set; } public int PriceCode
{
get
{
return _price.GetPriceCode();
}
private set
{
switch (value)
{
case Regular:
_price = new RegularPrice();
break;
case Childrens:
_price = new ChidrensPrice();
break;
case NewRelease:
_price = new NewReleasePrice();
break;
default:
throw new Exception("Incorrect Price Code!");
}
}
}
private Price _price ; public Movie(string title, int priceCode)
{
Title = title;
PriceCode = priceCode;
} public double GetCharge(int daysRented)
{
return _price.GetCharge(daysRented);
} public int GetFrequentRenterPoints(int daysRented)
{
return _price.GetFrequentRenterPoints(daysRented);
}
} #region 价格类Price public abstract class Price
{
public abstract int GetPriceCode();
public abstract double GetCharge(int daysRented);
public abstract int GetFrequentRenterPoints(int daysRented);
} public class ChidrensPrice : Price
{
public override int GetPriceCode()
{
return Movie.Childrens;
}
public override double GetCharge(int daysRented)
{
double thisAmount = 1.5;
if (daysRented > 3)
thisAmount += (daysRented - 3) * 1.5;
return thisAmount;
}
public override int GetFrequentRenterPoints(int daysRented)
{
return 1;
}
} public class NewReleasePrice : Price
{
public override int GetPriceCode()
{
return Movie.NewRelease;
}
public override double GetCharge(int daysRented)
{
double thisAmount = daysRented * 3;
return thisAmount;
}
public override int GetFrequentRenterPoints(int daysRented)
{
return (daysRented > 1) ? 2 : 1;
}
} public class RegularPrice : Price
{
public override int GetPriceCode()
{
return Movie.Regular;
}
public override double GetCharge(int daysRented)
{
double thisAmount = 2;
if (daysRented > 2)
thisAmount += (daysRented - 2) * 1.5;
return thisAmount;
}
public override int GetFrequentRenterPoints(int daysRented)
{
return 1;
}
} #endregion public class Rental
{
public Movie Movie { get; private set; }
public int DaysRented { get; private set; } public Rental(Movie movie, int daysRented)
{
Movie = movie;
DaysRented = daysRented;
} public double GetCharge()
{
return Movie.GetCharge(DaysRented);
} public int GetFrequentRenterPoints()
{
return Movie.GetFrequentRenterPoints(DaysRented);
}
} public class Customer
{
public string Name { get; private set; } public List<Rental> Rentals
{
get { return _rentals; }
}
private readonly List<Rental> _rentals = new List<Rental>(); public Customer(string name)
{
Name = name;
} public void AddRental(Rental rental)
{
_rentals.Add(rental);
} private double GetTotalAmount()
{
return _rentals.Sum(rental => rental.GetCharge());
}
private double GetTotalFrequentRenterPoints()
{
return _rentals.Sum(rental => rental.GetFrequentRenterPoints());
} public string Statement()
{
string result = "Rental Record for " + Name + "\n"; foreach (Rental rental in _rentals)
{
result += "\t" + rental.Movie.Title + "\t" + rental.GetCharge() + "\n";
} result += "Amount owed is " + GetTotalAmount() + "\n";
result += "You earned " + GetTotalFrequentRenterPoints() + " frequent renter points";
return result;
} }
}
可能有人会问了, 为什么要这样做,是不是没有必要,我想你可以这样设想:正如之前提及的,对于同一一件事,如果使用的不同的方式去解决,就应该尝试通过继承分离出来,好处在于,如果我有新的电影分类出现,将只需要再继承一个Movie产生新的子类,就能解决问题,或者某类型电影价格、积分计算方式变动,我们也只用更改相应子类的计算方式,不影响整体方式!
重构——一个小例子的更多相关文章
- java连接mysql的一个小例子
想要用java 连接数据库,需要在classpath中加上jdbc的jar包路径 在eclipse中,Project的properties里面的java build path里面添加引用 连接成功的一 ...
- java操作xml的一个小例子
最近两天公司事比较多,这两天自己主要跟xml打交道,今天更一下用java操作xml的一个小例子. 原来自己操作xml一直用这个包:xstream-1.4.2.jar.然后用注解的方式,很方便,自己只要 ...
- MVVM模式的一个小例子
使用SilverLight.WPF也有很长时间了,但是知道Binding.Command的基本用法,对于原理性的东西,一直没有深究.如果让我自己建一个MVVM模式的项目,感觉还是无从下手,最近写了一个 ...
- 使用Trinity拼接以及分析差异表达一个小例子
使用Trinity拼接以及分析差异表达一个小例子 2017-06-12 09:42:47 293 0 0 Trinity 将测序数据分为许多独立的de Brujin grap ...
- 从一个小例子认识SQL游标
1 什么是游标: 关系数据库中的操作会对整个行集起作用. 例如,由 SELECT 语句返回的行集包括满足该语句的 WHERE 子句中条件的所有行. 这种由语句返回的完整行集称为结果集. 应用程序 ...
- 关于SVN配置文件的一个小例子
1 背景假设 厦门央瞬公司是一家电子元器件设备供应商,其中有个ARM部门,专门负责ARM芯片的方案设计.销售,并在北京.上海各设立了一个办事处.对于工作日志,原先采用邮件方式发给经理,但是这种方式 ...
- Vue2.x源码学习笔记-从一个小例子查看vm实例生命周期
学习任何一门框架,都不可能一股脑儿的从入口代码从上到下,把代码看完, 这样其实是很枯燥的,我想也很少有人这么干,或者这么干着干着可能干不下去了. 因为肯定很无聊. 我们先从一个最最简单的小例子,来查看 ...
- Spring和Hibernate结合的一个小例子
1.新建一个SpringHibernate的maven项目 2.pom文件的依赖为 <dependency> <groupId>junit</groupId> &l ...
- Spring.Net在ASP.NET Mvc里使用的一个小例子
就贴个小例子,就不注意格式了. 1.下载dll NuGet的下载地址:http://docs.nuget.org/docs/start-here/installing-nuget 在vs的NuGet里 ...
随机推荐
- 记一次Weblogic发布Springboot遇到的坑
项目使用的是Springboot,之前直接使用JAR包的方式发布,但在客户这边实施发布的时候,客户使用的容器是weblogic,版本为 10.3.6. 痛苦就此开始! 不过项目组还有另外一个也同样使 ...
- oracle RAC
RAC安装步骤 1 配置共享存储 2 Grid Infrastructure软件的安装,GI主要用于cluster ,storage的管理 3 安装数据库软件 ...
- Jenkins 定时构建语法规则
1.Jenkins自由风格任务定时构建 2.语法规则 定时构建语法 * * * * * 第一个*表示分钟,取值0~59 第二个*表示小时,取值0~23 第三个*表示一个月的第几天,取值1~31 第四个 ...
- 如何利用sqoop将hive数据导入导出数据到mysql
运行环境 centos 5.6 hadoop hive sqoop是让hadoop技术支持的clouder公司开发的一个在关系数据库和hdfs,hive之间数据导入导出的一个工具. 上海尚学堂 ...
- 本地语音识别开源软件pocketsphinx调试总结
1问题一: fatal error: pocketsphinx.h: No such file or directory 解决方法: $ cd /usr/include $ sudo ln -s /m ...
- 工作随笔—Elasticsearch大量数据提交优化
问题:当有大量数据提交到Elasticsearch时,怎么优化处理效率? 回答: 批量提交 当有大量数据提交的时候,建议采用批量提交. 比如在做 ELK 过程中 ,Logstash indexer 提 ...
- 删除github上面的项目
1.进入github 2.点击Repositories,看到你所有的repository 3.点击进入你想要删除的repository,点击settings 4.在options选项中,下拉到底看到“ ...
- [NOI2009] 管道取珠
sum a[i]*a[i]可以理解为两个独立但同时进行的游戏得到同一个输出序列的方案数. 设f[l,i,j]为每个游戏都已经推出了l个珠子时,第一个游戏里上边儿的管道已经推出了i个,第二个游戏中上边儿 ...
- 【翻译】JavaScript中5个值得被广泛使用的数组方法
原文地址:http://colintoh.com/blog/5-array-methods-that-you-should-use-today?utm_source=javascriptweekly& ...
- vue 国际化i18n 多语言切换
安装 npm install vue-i18n 新建一个文件夹 i18n ,内新建 en.js zh.js index.js 三个文件 准备翻译信息 en.js export default { ho ...