Binding with multiple configurations ( Robots leg ? )

23 views
Skip to first unread message

James

unread,
Dec 13, 2011, 11:19:07 PM12/13/11
to google...@googlegroups.com
I'm trying to use Guice to bind various processor classes where each processor takes a config object ( country specific ) and can operator on that county. 

interface Config
    {
        String getName();
    }

    interface Processor
    {
        String getName();
    }

     class Process
    {
        private final Map<String, Processor> processorMap;

        @Inject
        public Process(final Map<String, Processor> processorMap)
        {
            this.processorMap = processorMap;
        }

        String process(final String country)
        {
            return processorMap.get(country).getName();
        }
    }

class ConfigA implements Config {
}

class ConfigB implements Config {
}

     class ProcessorA implements Processor
    {

        public ProcessorA(final SomeADeps deps, final Config config) {}


     class ProcessorB implements Processor
    {

        public ProcessorB(final Config config, final SomeBDeps deps) {}

  class Process
    {
        private final Map<String, Processor> processorMap;

        @Inject
        public Process(final Map<String, Processor> processorMap)
        {
            this.processorMap = processorMap;
        }

        String process(final String country)
        {
            return processorMap.get(country).getName();
        }
    }


In the binding section
public class TestModule extends AbstractModule {
 protected void configure()
    {
        //All the config object are created by loading from various property files
        Map<String, Config> countryToConfigMap;  // This map is populated

        //I also have a property file, that says
        // country US > use ProcessorA ( with ConfigA instance 1 )
        // country UK > use ProcessorA  ( with ConfigA instance 2 )
        // country DE > use ProcessorB  ( with ConfigB instance 1 ) 
        //etc 

       //TODO: How to bind Process class ?
    } 
}

In the main class, I'm trying to do something like this:

Injector injector = Guice.createInjector(new TestModule());
Process process = injector.getInstance(Process.class);
System.out.println("Name:" + process.process("US"));


Could someone help me understand and figure out on how to go about solving the above design using guice ?

Thanks!

Fred Faber

unread,
Dec 14, 2011, 7:56:42 AM12/14/11
to google...@googlegroups.com
Have a look here:

--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-guice/-/gS4dCrnJ_kkJ.
To post to this group, send email to google...@googlegroups.com.
To unsubscribe from this group, send email to google-guice...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.

Reply all
Reply to author
Forward
0 new messages