《Test-Driven development By Example》阅读(三)

前端之家收集整理的这篇文章主要介绍了《Test-Driven development By Example》阅读(三)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
第十三章 万恶之源(The Root of All Evil)——去除多余子类!
1、存在的问题:
(1)汇率转换问题,当汇率为2:1时,5美元 + 10法郎 = 10美元;
(2)5美元 * 2 = 10美元;
(3)将amount定义为私有;
(4)Dollar类有副作用吗?
(5)钱数为整数;
(6)实现equals()函数
(7)实现hashCode()函数
(8)与空对象判等;
(9)与非同类对象判等;
(10)5法郎 * 2 = 10法郎;
(11)Dollar与Franc之间的重复设计问题;
(12)普通判等;
(13)普通相乘;
(14)对象之间的判等;
(15)货币表示?
2、同步工程实践:
去除子类Dollar和Franc:
packagegarbage;
publicclassMoney{
protectedint@H_502_127@amount=0;
protectedString@H_502_127@currancy;
publicMoney(intamount,Stringcurrancy){
this.@H_502_127@amount=amount;
this.@H_502_127@currancy=currancy;
}
publicstaticMoneydollar(intamount){
returnnewMoney(amount,"USD");
}
publicstaticMoneyfranc(intamount){
returnnewMoney(amount,"CHF");
}
publicMoneytimes(intmultiplier){
returnnewMoney(@H_502_127@amount*multiplier,@H_502_127@currancy);
}
publicbooleanequals(Objectobject){
Moneymoney=(Money)object;
return@H_502_127@amount==money.@H_502_127@amount
&&this.@H_502_127@currancy.equals(money.currancy());
}
publicStringcurrancy(){
return@H_502_127@currancy;
}
}
publicclasstestDollarextendsTestCase{
publicvoidtestMultiplication(){
Moneyfive=Money.dollar(5);
assertEquals(Money.dollar(10),five.times(2));
assertEquals(Money.dollar(15),five.times(3));
}
publicvoidtestEquality(){
assertTrue(Money.dollar(5).equals(Money.dollar(5)));
assertFalse(Money.dollar(5).equals(Money.dollar(6)));
assertFalse(Money.franc(5).equals(Money.dollar(5)));
}
publicvoidtestCurrancy(){
assertEquals("USD",Money.dollar(1).currancy());
assertEquals("CHF",Money.franc(1).currancy());
}
}
第十四章 加法,最后的部分(Addition,finally)——!
1、存在的问题:
(1)汇率转换问题,当汇率为2:1时,5美元 + 10法郎 = 10美元;
(2)5美元 + 5美元 = 10美元;——解决问题
2、同步工程实践:
(1)为了实现货币相加、汇率转化问题引入了Bank类、Expression接口;代码如下:
publicinterfaceExpression{
}
publicclassBank{
publicMoneyreduce(Expressionsum,Stringstr){
returnMoney.dollar(10);
}
}
publicclassMoneyimplementsExpression{
protectedint@H_502_127@amount=0;
protectedString@H_502_127@currancy;
publicMoney(intamount,Stringcurrancy){
this.@H_502_127@amount=amount;
this.@H_502_127@currancy=currancy;
}
publicstaticMoneydollar(intamount){
returnnewMoney(amount,"USD");
}
publicstaticMoneyfranc(intamount){
returnnewMoney(amount,@H_502_127@currancy);
}
publicbooleanequals(Objectobject){
Moneymoney=(Money)object;
return@H_502_127@amount==money.@H_502_127@amount
&&this.@H_502_127@currancy.equals(money.currancy());
}
publicStringcurrancy(){
return@H_502_127@currancy;
}
publicMoneyplus(Moneyaddend){
returnnewMoney(@H_502_127@amount+addend.@H_502_127@amount,@H_502_127@currancy);
}
}
第十五章 完成预期目标(Make It)——继续完成相加和汇率转换!
1、存在的问题:
(1)汇率转换问题,当汇率为2:1时,5美元 + 10法郎 = 10美元;
第十六章 变化(change)
第十七章 混合货币
第十八章 抽象
第十九章 实例回顾
(1)下一步做什么?、
(2)比喻
(3)Junit的用法
(4)代码统计
(5)过程
(6)测试质量
第二部分 xUnit实例
第三部分 测试驱动开发模式
1、正反馈环:我的经验积累的越多,我所知道有可能要完成的事情就越多。我知道要完成的事情越多,就对自己手头的工作关注的越少,对手头工作关注的越少,完成的工作就越少,完成的越少,我所知道自己要完成的事情就越多。
2、对正反馈环的处理:列清测试清单。
第二十七章 测试模式
1、子测试(child test)
2、模拟对象(mock test)
3、自分流(self shunt)——让测试对象与测试用例而不是期望的进行交互。
4、日志字符串(log string)
5、清扫测试死角(crash test dummy)
6、不完整的测试(broken test)
第二十八章 可运行模式
1、伪成功——先让设计思路和整个流程能够走通,保证你的设计思路和整个流程正确;
2、三角法——现用简单的、错误的逻辑代替复杂的、正确的逻辑,以保证测试运行通过。
3、鲜明实现——用正确的逻辑实现功能
4、从一到多——实现复杂的逻辑。
第三十章 设计模式
1、命令(command)——把计算作为一个对象而不是消息来调用
2、值对象(value object)——避免别名问题;
3、空对象(null object)——表示一种对象计算的基本情形;
4、模板方法(template method)——用继承来实现子类中不同的实现;
5、插入式对象()——多个条件判断过滤,会使测试变得十分复杂,不容易进行测试,可以使用指定条件进行测试;
6、插入式选择器()——根据不用对象调动不同方法,。使用反射机制取代switch。
7、工厂方法()——使用一个类的静态方法来创建对象;
8、单例模式——不要使用全局模式。
第三十一章 重构
1、调和差异(reconcile differences)
(1)两个循环结构类似——合并、去掉一个;
(2)两个判断分支类似——合并、去掉一个;
(3)两个方法类似——合并、去掉一个;
(4)两个类类似——合并、去掉一个;
2、隔离变化(isolate change)——对要修改的地方进行隔离;
3、数据迁移(migrate data)——迁移出旧的数据格式,换成新的数据格式;
4、提取方法(extract method)
5、内联方法(inline method)
6、提取接口(extract interface)
7、转移方法(move method)——添加方法应该在的归属类中;
8、方法对象(method object)——为方法创建对象;
9、添加参数(add parameter)——把方法中的参数换为构造函数中的参数。
第三十三章 掌握测试驱动开发
1、TDD与XP是相辅相成的!
四、Recite(复述)
五、Review(回顾)
原文链接:https://www.f2er.com/javaschema/286160.html

猜你在找的设计模式相关文章