Replacing Factory with Guice

124 views
Skip to first unread message

Karthik Krishnan

unread,
Aug 1, 2008, 3:29:28 AM8/1/08
to google-guice
I have an Strategy implementation

public interface ContactRecipientStrategy {
void deliverToRecipient(Recipient recipient) throws Exception
}


Two implementations

public class EmailRecipientStrategy implements
ContactRecipientStrategy {

}

public class TelephoneRecipientStrategy implements
ContactRecipientStategy {
}

In my factory implementation, I have a switch condition which returns

.....

public ContactRecipientStategy getStrategy(String emailAddress) {

bool flag = ifValid(emailAddress);

if (flag) {
return new EmailRecipientStrategy();
}
return new TelephoneRecipientStrategy();
}


Is there a way to implement this? I have to call this factory method
from another method body.


To bind my implementation, I did the following

bind(ContactRecipientStrategy).annotatedWith(FlagTrue.class).to(EmailRecipientStrategy.class);
bind(ContactRecipientStrategy).annotatedWith(FlagFalse.class).to(TelephoneRecipientStrategy.class);

I don't want to create two annotation classes for a switch condition.
Is there a better way? I tried AssistedInject, but I can not use it as
it is not Mavenized in Guice 1.0 unless I reinstall it everytime I
build. Please help me here.

Thanks,

Krishnan

jordi

unread,
Aug 1, 2008, 3:35:57 AM8/1/08
to google...@googlegroups.com
check out the chapter "Introducing the custom provider" from these slides http://google-guice.googlecode.com/files/JavaOnGuice20070426.pdf

i guess that's what you're looking for
Reply all
Reply to author
Forward
0 new messages