views:

54

answers:

1

I have a requirement to use a Fund model in my code. It will contain a fund name and fund code. In the interest of reuse I've poked around the package containing the other models used and found an existing Fund model. However the issue here is that, in addition to fund name and code, it also contains an amount. Amount isn't directly relevant in my context. So, do I:

1) Use the existing Fund model as is, ignoring the setters/getters for fund amount.

2) Put a FundDescription interface onto the existing Fund model for accessing only the information I'm interested in.

3) Make a FundDescription base class from which the existing Fund model could now extend

4) Create a whole new seperate model since the two are slightly contextually different

+2  A: 

Option 1 could confuse a person that reads the code, and could tempt someone who doesn't understand your code fully, to use those setters/getters due to some misunderstanding. The other options sort of forces him to understand your code better.

Option 2 Sort of second-place-solution but to me it wouldn't make perfect sense logically.

Option 3 It is the cleanest and most logical way of solving the problem among those listed, and I would say that that qualifies as close to best practice in this case.

Option 4 This sounds unnecessary! :-)

Option 5 (NEW! The one I'd go for :-) Have a member variable of type FundDescriptor in the Fund model.

Whenever faced with this kind of design decisions I usually try to avoid thinking "what would be the most efficient solution" or "what would be the solution requiring least code", but instead think of what would look most logical to for a person that reads the code for the first time.

Think of the "extends" construct as an "is-a" relation, and read it out loud. If it makes sense and is logical, go for it. How does, "a Fund is a FundDescriptor" sound to you? If it sounds like "hmm.. that doesn't make sense", then I'd go for option 5, otherwise I would go for option 3.

Have a look at the accepted answer at

http://stackoverflow.com/questions/269496/inheritance-vs-aggregation

aioobe
Nice answer. Thanks. I like your option 5.
Chris Knight