I am reading Test Driven Development: By Example. I am on chapter 13. Chapter 12 and 13 introduced Plus operation to Money object. A Money object can plus by other Money object.
The author added two classes (Bank and Sum) and one interface (IExpression) to the solution. This is the class diagram of the final solution.

Money store amount and currency for example 10 USD, 5 BAHT, 20 CHF. The Plus method returns Sum object.
public class Money : IExpression
{
private const string USD = "USD";
private const string CHF = "CHF";
public int Amount { get; protected set; }
public string Currency { get; private set; }
public Money(int value, string currency)
{
this.Amount = value;
this.Currency = currency;
}
public static Money Dollar(int amount)
{
return new Money(amount, USD);
}
public static Money Franc(int amount)
{
return new Money(amount, CHF);
}
public Money Times(int times)
{
return new Money(this.Amount * times, this.Currency);
}
public IExpression Plus(Money money)
{
return new Sum(this, money);
}
public Money Reduce(string to)
{
return this;
}
public override bool Equals(object obj)
{
var money = obj as Money;
if (money == null)
{
throw new ArgumentNullException("obj");
}
return this.Amount == money.Amount &&
this.Currency == money.Currency;
}
public override string ToString()
{
return string.Format("Amount: {0} {1}", this.Amount, this.Currency);
}
}
Sum store two Money objects which come from constructor arguments. It has one method Reduce that return new Money object (create new object by add amount of two object)
public class Sum : IExpression
{
public Money Augend { get; set; }
public Money Addend { get; set; }
public Sum(Money augend, Money addend)
{
this.Augend = augend;
this.Addend = addend;
}
public Money Reduce(string to)
{
var amount = this.Augend.Amount + this.Addend.Amount;
return new Money(amount, to);
}
}
Bank has one method – Reduce. It just call Reduce method of incoming IExpression argument.
public class Bank
{
public Money Reduce(IExpression source, string to)
{
return source.Reduce(to);
}
}
IExpression is an interface that implemented by Money and Sum.
public interface IExpression
{
Money Reduce(string to);
}
These are my questions.
Bankdoes nothing good for the solution at this stage. Why do I need it?- Why do I need
Sumsince I can create and return Money object insidePlusof the classMoney(Like what the author did withTimes)? - What is the purpose of Bank and Sum? (Right now, it doesn’t make any sense for me)
- I think
Reducesound strange for me as the method name. Do you think it is a good name? (please suggest)
Keep reading. Kent Beck is a very smart guy. He either has a very good reason why he created the example that way, and it will be clear later on, or it’s a poor solution.
“Reduce” is a very good name if map-reduce is the ultimate goal.