Jeff, As a simple rule of thumb, "guice is the new new", and so you'll want to avoid use of "new" for non-trivial objects wherever possible.
-Fred On Fri, Sep 17, 2010 at 3:52 PM, Jeff <[email protected]> wrote: > Hi, > > I'm finally writing my first Guice module and I'm not sure how to > proceed on using "new" inside a module. For instance, if I have 2 > interfaces A and B that I want to make available from my module to non- > Guice modules: > > public interface A > { > B<String> getUp(); > B<String> getDown(); > } > > Is it a best-practice to implement as: > > class Aimpl > { > private final B<String> bUp = new BUp(); > private final B<String> bDown = new BDown(); > > public B<String> getUp() { return bUp; } > public B<String> getDown() { return bDown; } > } > > Where I am using "new" to create objects, but only within my own > module, or is it best to rely on Guice to give me these objects: > > class Aimpl > { > private final B<String> bUp; > private final B<String> bDown; > > @Inject > Aimp( @Named( "Up" ) final B<String> bUp, @Named( "Down" ) final > B<String>bDown ) > { this.bUp = bUp; this.bDown = bDown; } > > public B<String> getUp() { return bUp; } > public B<String> getDown() { return bDown; } > } > > > In reality my classes are larger with more "get" methods on A. For > instance, I have an "A" interface that gives access to a number of > Comparator<MyObject> strategies. Think of my "A" as similar to the > Predicates class in Guava but I want to expose "A" as an interface > instead of a static class so I can mock it, and each of the generic > types is specialized for my classes. However, my Aimpl constructor is > now accepting too many parameters. > > I suppose if this was a new project I would simply inject a strategy > directly into each class that requires the strategy and not have the > "A" interface or class. In my case I need to expose these strategies > to both other Guice modules and legacy non-Guice-ified code and > therefore I have created the "A" interface. > > I guess the bottom line is that I need advice on using "new" within a > module to directly instantiate other package-visible classes in the > package. > > Thanks for your time. > Jeff > > -- > 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]<google-guice%[email protected]> > . > For more options, visit this group at > http://groups.google.com/group/google-guice?hl=en. > > -- 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.
