Chapter 19 challenge


This challenge is the first that has thrown me, and it’s just the organization more than anything. Should the individual stocks have an instance variable pointing to a portfolio instance? Or, should portfolio instances simply identify stock instances and bring them into the portfolio instance? I just can’t get my head wrapped around it.

I really have enjoyed the book, guys. Thanks,

Brad H.


Keeping in mind that I’m miles away from my book and I don’t remember this challenge…

Typically you’d see a portfolio containing stocks, and thus the object model should reflect that. That way if you need to summarize the portfolio’s value, you can iterate over its stocks without looking through all stocks for their ownership.

It’s possible to point in both directions, but you have to protect against disagreements by being careful about who makes changes.