• Post Reply Bookmark Topic Watch Topic
  • New Topic

Object Orientation Question  RSS feed

 
Tom Hill
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have made a Weight class inside a package.
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?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Wayne L Johnson
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another approach is something like the following:

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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Wayne L Johnson:
So by implementing the "add()" method as shown above, you can add two "Weight" instances together but you get a new instance in return.

Still calling it "add" is a little bit confusing, though. I'd rather call it "plus" in this case.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!