@Sam:
Yes, the Impl can be changed throughout the lifetime of the project,
so I will need some sort of Factory. I was simply hoping there was a
better way of having Guice provide it. I assume with your 2nd
suggestion, you're talking about something like this?
class ServiceFactory {
@Inject ServiceImplA serviceImplA;
@Inject ServiceImplB serviceImplB;
public Service get( int type ) {
if ( type == 1 ) {
return serviceImplA;
} else if ( type == 2 ) {
return serviceImplB;
}
return null;
}
}
That works, although I don't know if I'm fond of requiring that all
implementations be instantiated each time the ServiceFactory is
instantiated.
@Gary:
The "type" is external to the application. I've simplified the use
case obviously, but the client is an object that gets spun up during
an HTTP request. The underlying dependencies (impls) that it needs
will depend on what's in the request. Think Command pattern.
Your builder pattern would work great if I could pass the type to the
get() method, rather than to the provider constructor. But Guice
doesn't support that as a valid Provider<T> implementation.
@Brian:
Can you give me an example of using dynamic proxies, as you explained?
This seems like a fairly common use case, so I'm surprised there isn't
a better pattern to handle it. Thanks everyone for your prompt
responses so far.
Ryan
On Nov 18, 8:38 am, Gary Pampara <
gpamp...@gmail.com> wrote:
> Agreed the state could be a problem. You could probably get around it
> by directly passing in the value to avoid managing the state.
>
> Would naturally be great to find a solution that "is always best".
>
>
>
> On Wed, Nov 18, 2009 at 5:31 PM, Brian Pontarelli <
br...@pontarelli.com> wrote:
> > Just need to be careful of state and provider use if you go with a builder pattern.
>
> > Another way I've handled this in the past is using dynamic proxies that use the method parameters to perform a lookup. In the end, all the solutions I've used are the same. There is at least one level of indirection and someone has to perform a lookup or build a new instance (i.e. a provider). In the lookup case the Injector can be used to perform the lookup. In the provider case I've used post-inject after instantiation or pre-injection of the provider.
>
> > -bp
>
> > On Nov 18, 2009, at 8:23 AM, Gary Pampara wrote:
>
> >> Sorry I see now what you mean. Where do you get the information
> >> regarding the 'type' so that the factory can create the needed
> >> instance?
>
> >> I solved this a while ago in a project using the builder pattern. This
> >> is a trivial example, but it seems to work. If anyone has a better way
> >> of doing this, I'd appreciate the advice.
>
> >>
http://pastie.org/704247
>
> >> For more options, visit this group athttp://
groups.google.com/group/google-guice?hl=.