[picocontainer-dev] PicoContainer API enhancement
"Nick Sieger" <[email protected]>
| Newsgroups | gmane.comp.java.picocontainer.devel |
|---|---|
| Message-ID | <[email protected]> |
I thought I'd propose a small API enhancement that I found myself coding up
during my usage of Pico. I tend to prefer standalone pico with a
builder-style pattern that has a single class responsible for populating the
container with its components and then fetching a main object instantiated
by the container.
As soon as you have more than a handful of components in your container,
code to do this becomes rather ugly -- you basically have a huge block of
container.registerComponent/container.registerComponentImplementation
statements. I wanted something more declarative.
Basically, this amounts to applying the parameter object pattern to the
various PicoContainer.register* methods. Instead of:
MutablePicoContainer pico = new DefaultPicoContainer();
pico.registerComponentImplementation(Boy.class);
pico.registerComponentImplementation(Girl.class);
You get something like:
MutablePicoContainer pico = new DefaultPicoContainer();
ComponentMetadata[] components = new ComponentMetadata[] {
new ComponentMetadata(Boy.class),
new ComponentMetadata(Girl.class)
};
pico.register(components);
You can cover all the various register* methods in PicoContainer simply by
adding constructors to ComponentMetadata.
This probably does not seem like much gain (in fact for the example it
increases LOC), but it lets you move the component metadata declaration
somewhere else, e.g., a static field declaration. For me, separating that
data from the code that constructs the container is a fairly big maintenance
and readability win.
It also takes the concept that a component is not just a class, but possibly
a class plus some wiring parameter hints, or perhaps a custom CA, and gives
that thing a name. It lets you aggregate that information and pass it
around. It seems like a useful abstraction to have in the container.
If you're hankering for a larger, more concrete example, let me know and
I'll put one together.
Cheers,
/Nick