Basically the question is: Should a Weight have an add() method to add itself to another Weight, or should the Weight class come with an abstract WeightArithmetic class that has a function add?
I dont think a Weight has the behaviour or ability to add itself to another weight and a 2nd Class is needed, or am I just being OO pedantic?
Philosophically once you've created a "Weight", you have an object that represents something that weighs 50 pounds (for example). It probably doesn't make sense to change the state once it's been created.
So by implementing the "add()" method as shown above, you can add two "Weight" instances together but you get a new instance in return.
It really depends on how you intend to use it.
Originally posted by fred rosenberger:
i am no expert, but i see nothing wrong with having an add() method. if you think of a weight as something like a barbell, you do have the ability to add(), subtract(), zero(), getWeight() to it, so these methods make sense to me.
just my humble opinion...
I agree - though I am not sure wether I'd qualify as an expert...
Perhaps Martin Fowler would qualify? http://www.martinfowler.com/ap2/quantity.html