需求:把字符串"Tdd is a software devolopment technology" 按照单词反转为
"technology devolopment software a is Tdd"
是看了这个帖子后一直有困惑。直接促成了我找了Kent Beck大的Test-Driven Development
By Example来看。自此进入TDD的世界。其实这题目和书后面的那个fibonacci的解法一样。
关键在于:“分解多项式” 且听我忽悠..
import junit.framework.TestCase; public class NewStringReverserTest extends TestCase{ //"a b" - "b a" public void testSimpleReverse(){ assertEquals("b a",reverse("a b"));//从最简单的情况开始 } private String reverse(String string) { return "b a";//最“直接”的实现,绿了吧- -! } }
public class NewStringReverserTest extends TestCase{ ... private String reverse(String string) { return "b"+" "+"a";//拆,为什么这么拆? 往下看 } }
public class NewStringReverserTest extends TestCase{ ... private String reverse(String string) { //要拆得有意义,注意描述= = //其实是一个一个拆的,每步都执行test,边绿边拆...小步快跑 String splitToken = " "; String preWord = "a"; String postWord = "b"; //下面其实是业务逻辑了,看后面的postWord被放在了前面——所谓的交换 return postWord + splitToken + preWord; } }
public class NewStringReverserTest extends TestCase{ ... private String reverse(String inputString) { String splitToken = " "; //实现切词逻辑 String preWord = inputString.substring(0,inputString.indexOf(splitToken)); String postWord = inputString.substring(inputString.indexOf(splitToken)+1); return postWord + splitedToken + preWord; } }
ok 目前为止似乎无法继续了。那么继续添加test描述需求:"a b c"-"c b a"
public class NewStringReverserTest extends TestCase{ //"a b" - "b a" public void testSimpleReverse(){ assertEquals("b a",reverse("a b")); assertEquals("c b a",reverse("a b c"));//红了吧?- - } ... }
Expected:c b a Actual:b c a
好吧 分析下:bc 反了,bc看做一个整体,再“交换”一次就ok了,即: reverse("b c")+"a"
private String reverse(String inputString) { ... return reverse(postWord) + splitToken + preWord; }
还是会红,debug发现“切到剩一个词”了就不能切了嘛= =
private String reverse(String inputString) { int tokenIndex = inputString.indexOf(splitToken); if (tokenIndex == -1) return inputString; String prePart = inputString.substring(0,tokenIndex); String postPart = inputString.substring(tokenIndex + 1); //描述为prePart,postPart 更符合语义,也是一步重构吧 return reverse(postPart)+ splitToken + prePart }
这下绿了,想想其实prePart其实是reverse(prePart)
另外把那个需求的test加上 看看符合需求否?
public class NewStringReverserTest extends TestCase { // "a b" - "b a" public void testSimpleReverse() { assertEquals("b a",reverse("a b")); assertEquals("c b a",reverse("a b c")); assertEquals("Tdd is a software devolopment technology",reverse("technology devolopment software a is Tdd")); } private String reverse(String inputString) { String splitToken = " "; int tokenIndex = inputString.indexOf(splitToken); if (tokenIndex == -1) return inputString; String prePart = inputString.substring(0,tokenIndex); String postPart = inputString.substring(tokenIndex + 1); return reverse(postPart) + splitToken + reverse(prePart); } public void testReverseWithSingleWords() { assertEquals("a",reverse("a")); } public void testReverseWithSplitTokenIn() { assertEquals(" ",reverse(" ")); assertEquals(" ",reverse(" ")); assertEquals(" a",reverse("a ")); assertEquals("b a",reverse("a b")); assertEquals("c b a ",reverse(" a b c")); assertEquals("c b a",reverse("a b c")); } }最后总结:"a b c"到"c b a" 原来是a b c->[b c] a->[c b] a