I've got a value that many objects need. For example, a financial application with different investments as objects, and most of them need the current interest rate.I was hoping to encapsulate my financial environment as an object, with the interest rate as a property. But, sibling objects that need that value can't get to it.So how do I share values among many objects without over-coupling my design? Obviously I'm thinking about this wrong.
What if globals make sense?
object oriented
I've got a value that many objects need.This is a design smell. It's uncommon that many objects need to know about something. That said, the current interest rate is a fairly good example of exceptional circumstances. One thing to worry about is that there's rarely the interest rate. Different financial instruments use different rates. At the very least, different locales use different 'standard' rates. Further, to aid in testing and reporting, you'll usually want to pass in a rate since you don't want to use the current rate there. You want to use the 'what if' or 'as of reporting date' rate.So how do I share values among many objects without over-coupling my design?By sharing them, not having them all refer to a single instance. Passing the same thing around is still coupling to a degree, but not over coupling since something like the current interest rate is needed as input to a variety of calculations.