Hi,

On Thu, Feb 23, 2012 at 5:57 AM, Bill Moseley <mose...@hank.org> wrote:
> I often have classes that abstract out the work of fetching data from
> multiple sources -- so I have attributes that hold instances of the objects
> that do the actual work.   Each of these objects need their own config.
>
> I've used three approaches to pass config to the contained objects, but
> each seems a bit cumbersome.  Just wondering if there's other ideas.

I've come across the same problem often times, and I never got a
answer I was fully happy with.

I've used two approaches in the past:

 1. pass inner worker object fully created to the top level class: you
create the worker object with whatever configuration you want, and
then pass it in the top level constructor call;
 2. use dependency injection or some variation of the concept: either
have a singleton configuration manager and worker objects call into it
for configuration information, or pass a CodeRef configuration
callback to the top level object to be passed along to the worker
objects, to be used in the same way.

On the whole I rather like dependency injection because on my use
cases I force the callers of the configuration manager to register
themselves with it, and allow me to reconfigure live whenever the app
receives a external event "there is new configuration available,
please adjust yourselves". When this happens, the configuration
manager can obtain the new configuration parameters, and signal all
the registered clients that they should reconfigure at their
convenience.

Bye,
-- 
Pedro Melo
@pedromelo
http://www.simplicidade.org/
http://about.me/melo
xmpp:m...@simplicidade.org
mailto:m...@simplicidade.org

Reply via email to