Re: Pattern for configuring child objects.
[email protected] (Nick Perez)
| Newsgroups | perl.moose |
|---|---|
| Message-ID | <[email protected]> |
On Thu, 23 Feb 2012 11:27:18 +0000 Pedro Melo <[email protected]> wrote: > Hi, >=20 > On Thu, Feb 23, 2012 at 5:57 AM, Bill Moseley <[email protected]> > 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. =C2=A0 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. =C2=A0Just wondering if there's other > > ideas. >=20 > I've come across the same problem often times, and I never got a > answer I was fully happy with. >=20 > I've used two approaches in the past: >=20 > 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. >=20 > 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. >=20 > Bye, If you end up doing DI, take a look at using Bread::Board since it makes such things pretty trivial. --=20 Nicholas Perez XMPP/Email: [email protected] http://search.cpan.org/~nperez/ http://github.com/nperez