I've seen it used, but I'm not sure the usage were good usecase examples. Do you have examples of idiomatic usages of Guice Mapbinder? (Cases where Mapbinder开发者_StackOverflow社区 is really the correct tool to solve a problem)
Offhand, it looks like a reasonable way to create a registry of runtime-named implementations of a common interface. Consider selecting one of many plugins/modes/whatever from a command line or configuration file: the desired injection can't be known at compile time. A MapBinder provides an easy runtime lookup without resorting to type-switching.
I extensively use it in Guts-GUI.
You can take a look, in particular, at the ResourceModule
, where it is used to map the right ResourceConverter<T>
for a given type T
:
Map<TypeLiteral<?>>, ResourceConverter<?>>
The MapBinder
is directly created in the Resources
helper class.
This way, any module can add its own resource converters for its own types, e.g. MessageModule
adds its own converters.
I also used it as Map<Integer, WindowProcessor>>
in WindowsModule
to define an ordered list of WindowProcessor
s to be applied, one after another, to a newly created window..
Once again, this allows various modules to insert their own processor to the list applied to every window: ResourceModule
uses it to add the ability of automatic injection of i18n resources to windows.
精彩评论