Best way to implement factory with Guice

253 views
Skip to first unread message

Barak Yaish

unread,
Mar 15, 2015, 11:37:54 AM3/15/15
to google...@googlegroups.com
Hi,

Doing my first steps with Guice, I thought the fastest way to understand it would migrate parts of existing application to Guice style. The application is kind of web crawler, and I have factory creating crawlers based on the input url:

public static Crawler getCrawler( String url )
{
try
{
if( url.contains( "www.site1.com" ) )
return new Site1( AppConfig.getInstance() );
else if( url.contains( "www.site2.com" ) )
return new Site2( AppConfig.getInstance() );
}
catch ( Exception e )
{
logger.error( "failure", e );
}

return null;
}


Is the Guice version is only take the crawler instances from the injector and injecting the AppConfig?

public static Crawler getCrawler( String url )
{
try
{
if( url.contains( "www.site1.com" ) )
return GuiceInjector.getInstance( Site1.class );
else if( url.contains( "www.site2.com" ) )
return GuiceInjector.getInstance( Site2.class );
}
catch ( Exception e )
{
logger.error( "failure", e );
}

return null;
}

Is there a better and/or more elegant way? 

Thanks!

Laszlo Ferenczi

unread,
Mar 16, 2015, 9:57:56 AM3/16/15
to google...@googlegroups.com
Hi,

There are many ways to solve this problem, one easy enough to understand:

@Inject
Provider<CrawlerOne> crawlerOneProvider;

@Inject
Provider<CrawlerTwo> crawlerTwo;


public Crawler getCrawler(String url) {
    if (url...) {
        return crawlerOneProvider.get();
    }
    else {
        return crawlerTwoProvider.get();
    }
}


As an alternative you can inject the injector itself and get the instance from it.

Also a slightly more advanced, but much more elgant way is to use AssistedInject (it's exectly for this use case) See the guice docs for more info.

PS: no need to make everything static, the whole point of the DI framework that you don't need static anchors in your code.

--
L

--
L

--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice...@googlegroups.com.
To post to this group, send email to google...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/8911e587-58bd-4631-80a2-7a5d91007431%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Stephan Classen

unread,
Mar 16, 2015, 11:11:06 AM3/16/15
to google...@googlegroups.com
Don't inject the injector. Its almost always a code smell.

Barak Yaish

unread,
Mar 16, 2015, 11:42:27 AM3/16/15
to google...@googlegroups.com
Thanks. Is there a way to overcome the exhausting if-else if- else if structure, some magical way to support new crawlers in the system without "register" them in that factory?

Sam Berlin

unread,
Mar 16, 2015, 11:52:48 AM3/16/15
to google...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages