Hi Ramsey,
Guice is very flexible and powerful. You can use the @Named annotation to
distinguish between implementations:
class ComponentOne {
@Inject @Named("Advanced")
Service service;
}
... or create your own annotation for a more type-safe approach:
class ComponentTwo {
@Inject @Basic
Service service;
}
class AppModule {
void configure() {
bind(Service.class).annotatedWith(Names.named("Advanced")).to(AdvancedServiceImpl.class);
bind(Service.class).annotatedWith(Basic.class).to(BasicServiceImpl.class);
}
}
Guice also solves the "robot legs problem". Imagine there is only one
implementation of the Service interface named ServiceImpl. You want two
instances of ServiceImpl: one pointing to the service's URL and another
pointing to a backup URL. You can use PrivateModules to solve this problem as
described in the Guice's FAQ [1] (see question "How do I build two similar but
slightly different trees of objects?").
[1]http://code.google.com/p/google-guice/wiki/FrequentlyAskedQuestions
Cheers,
Henrique
On 10/04/2012, at 23:28, Ramsey Gurley wrote:
>
> On Apr 7, 2012, at 8:07 PM, Henrique Prange wrote:
>
>> Suppose you have a Service interface and a BasicService implementation for
>> it. This Service is required across your components. So you have something
>> like this:
>>
>> class ComponentOne {
>> Service service = new BasicService();
>> }
>>
>> class ComponentTwo {
>> Service service = new BasicService();
>> }
>>
>> ...
>>
>> Then, the project evolves, and you develop a new AdvancedService
>> implementation for the Service interface. You have to traverse the entire
>> code base updating the instantiation of Service.
>>
>> On the other hand, if you use Guice, you can delegate the creation of
>> Services to Guice, and inject the instances of Service when required. You
>> write code like this:
>>
>> class ComponentOne {
>> @Inject
>> Service service;
>> }
>>
>> class ComponentTwo {
>> @Inject
>> Service service;
>> }
>>
>> ...
>>
>> And you configure a Guice Module to specify that you want an instance of
>> BasicService (or an AdvancedService) when you need a Service.
>>
>> class AppModule extends AbstractModule {
>> void configure() {
>> bind(Service.class).to(BasicService.class);
>> }
>> }
>>
>> If you need to change the implementation class, you just have to redefine
>> your module:
>>
>> class AppModule extends AbstractModule {
>> void configure() {
>> bind(Service.class).to(AdvancedService.class);
>> }
>> }
>>
>
> What if I want an advanced service on component one and a basic service on
> component two?
>
> Ramsey
>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list ([email protected])
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/webobjects-dev/archive%40mail-archive.com
This email sent to [email protected]