David Vallner
10/28/2006 4:10:00 PM
lcor1979@gmail.com wrote:
> What I'll like, is that Child1 and Child2 have their own values of
> @@test, but @@test should be initialized and used in Parent.
> Also, @@test cannot be an instance variable because it is heavy to
> initialize and will be common to all instances of Child1 or Child2.
>
Class variables and class instance variables sound way too much like
global state coming into the system through a backdoor, I'd avoid both.
Personally, I'd replace lookup through a class variable or class
instance variable with injecting the shared object into instances. The
less shared state, the more flexibility you gain in being able to rewire
your objects.
Disclaimer: I am biased in favour of IoC and DI based "objects floating
in vacuum" designs, especially for scenarios that involve reusing an
expensive-to-create component like this one. If nothing else,
declarative dependency resolution lets you plug in testing components
easier / clearer than imperative (even if Ruby makes the latter easier
in that you're allowed to molest any object in arbitrary ways.)
David Vallner