Hi Rory, How about if the implementation class, TwitterFetcher and JiwaiFetcher, dependencies on another type in their constructor? How can I pass the dependencies in their constructor?
thanks. On Aug 14, 11:39 am, Rory Ye <[email protected]> wrote: > Hi,zhaoyi > > u can use the annotation. > > public interface Fetcher { > > List<Status> fetch() throws HttpException, IOException; > > } > > public class TwitterFetcher implements Fetcher { > ........ > > } > > public class JiwaiFetcher implements Fetcher { > ..... > > } > > and use two annotation to bind each implemention. > > @Retention(RetentionPolicy.RUNTIME) > @Target({FIELD, METHOD, PARAMETER}) > @BindingAnnotation > public @interface Twitter { > > } > > @Retention(RetentionPolicy.RUNTIME) > @Target({FIELD, METHOD, PARAMETER}) > @BindingAnnotation > public @interface Jiwai { > > } > > bind(Fetcher.class).annotatedWith(Twitter.class).to(TwitterFetcher.class).in(Singleton.class); > > bind(Fetcher.class).annotatedWith(Jiwai.class).to(JiwaiFetcher.class).in(Singleton.class); > > then u can inject them. > > public class FetchService { > > @Inject > @Twitter > Fetcher twitterFetcher; > > @Inject > @Jiwai > Fetcher jiwaiFetcher; > > public void fetch() { > twitterFetcher.fetch(); > ... > } > > > > } > On Fri, Aug 14, 2009 at 9:33 AM, zhaoyi <[email protected]> wrote: > > > Can't I use provider to do this? If I use factory pattern, I only need > > one factory class. However, If I use guice, I have to create one > > factory class, one Module class and one provider class. I think guice > > will make our application more complicated. > > > On Aug 10, 3:00 pm, Brian Pontarelli <[email protected]> wrote: > > > This pattern was common at Orbitz, although more complex. The idea was > > > that you wanted to use a service for booking airline tickets. You > > > would ask the factory for an implementation of the AirBookingService > > > for UA or AA or NWA. Each implementation would talk to the specific > > > airline and talk their special on-the-wire language. > > > > We did this in a pretty cool way when we converted to Spring many > > > years back by using a proxy service that would lazy load the real > > > service after calling a lookup based on the request. Looked like this: > > > > public class Caller { > > > private AirBookingService service; // injected as a dynamic proxy > > > > public String doBooking() { > > > AirBookingRequest request = ...; > > > service.book(request); > > > } > > > > } > > > > public class AirBookingServiceHandler implements InvocationHandler { > > > private AirLookupService lookup; // injected > > > > public Object invoke(Object proxy, Object instance, Object.. > > > params) { > > > AirBookingRequest request = (AirBookingRequest) params[0]; > > > BookingService service = > > > lookup.lookupService(request.getAirlineCode()); > > > return service.book(request); > > > } > > > > } > > > > That's the general gist of it anyways. The invocation handler was > > > actually generic enough to handle all services and pass calls from the > > > proxy to the correct service implementation. This allowed everything > > > to be injected without having to inject factories everywhere. > > > > -bp > > > > On Aug 9, 2009, at 3:07 PM, [email protected] wrote: > > > > > Hey there, > > > > > Generally you wouldn't use a factory in this way - the idea is that > > > > the caller just expects any implementation of the interface returned > > > > therefore the caller specifying the implementation to use doesn't make > > > > sense. > > > > > Out of interest, how are you using this particular piece of code? > > > > > Cheers > > > > > Mark > > > > > On Aug 9, 3:04 pm, zhao yi <[email protected]> wrote: > > > >> This is my code based on Factory pattern. I want to use guice and how > > > >> can I convert it to use guice? > > > > >> interface Interface1{ > > > >> public void sayHello(); > > > > >> } > > > > >> class Imple1 implements Interface1{ > > > >> @Override > > > >> public void sayHello() { > > > >> System.out.println("imple 1"); > > > >> } > > > > >> } > > > > >> class Imple2 implements Interface1{ > > > >> @Override > > > >> public void sayHello() { > > > >> System.out.println("imple 2"); > > > >> } > > > > >> } > > > > >> class Factory{ > > > >> public Interface1 getInterface(int type){ > > > >> if(type == 1){ > > > >> return new Imple1(); > > > >> }else if(type ==2){ > > > >> return new Imple2(); > > > >> } > > > >> return null; > > > >> } > > > > >> } > > > > >> thanks. > > -- > My site:http://www.jdkcn.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "google-guice" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/google-guice?hl=en -~----------~----~----~----~------~----~------~--~---
