O'Reilly logo

Test Driven Development: By Example by Kent Beck

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 11. The Root of All Evil

$5 + 10 CHF = $10 if rate is 2:1$5 * 2 = $10Make “amount” privateDollar side effects?Money rounding?equals()hashCode()Equal nullEqual object5 CHF * 2 = 10 CHFDollar/Franc duplicationCommon equalsCommon timesCompare Francs to DollarsCurrency?Delete testFrancMultiplication?

The two subclasses, Dollar and Franc, have only their constructors. But because a constructor is not reason enough to have a subclass, we want to delete the subclasses.

We can replace references to the subclasses with references to the superclass without changing the meaning of the code. First Franc:

Franc
static Money franc(int amount){
   return new Money(amount, "CHF");
					}
				

Then Dollar:

Dollar
static Money dollar(int amount) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required