The most significant benefit of this pattern is one which is much more 
obvious outside the scope of guice, and one which is used all the time. For 
instance, have you ever tried to design a class that runs itself (a run 
method) within a thread? It's nice when you create one of these objects to 
have the constructor simply start the thread and return. The problem is 
that you have to pass "this" to the thread start routine as static data, 
and it's a real issue if the OS happens to put your constructor to sleep 
after it's scheduled itself to be run, but before it returns, then the OS 
schedules your new thread to wake up and do something with your partially 
constructed object. Several IDEs (netbeans included) display a warning 
whenever 'this' is handed to an object in the constructor - it flags these 
as 'suspicious constructor arguments'. 

The usual way around this problem is to have a static factory method in 
your class that calls a private constructor to build your object and then 
initializes it with a call to a private init function that hands 'this' to 
the consumer object (e.g., thread). Now, the only way you can create your 
objects is by calling it's static factory method. So... the OP's question 
still stands, and for very good reason - is there a way to annotate your 
class so that guice will create it by calling a static factory instead of a 
constructor?

Thanks!
John

On Tuesday, March 13, 2007 at 11:00:10 AM UTC-6, Hani Suleiman wrote:
>
>
> On Mar 13, 2007, at 12:55 PM, Ben wrote:
>
> >
> > Is there a way to tell Guice that I want to use
> > "MyClass.newInstance(...)" to instantiate the class?
> >
> What's the benefit of doing this instead of a constructor?
>
> > And more, can I direct Guice to use "Factorys.createMyInteraface()" to
> > instantiate an instance of MyInterface?
> >
>
> Why not just write a Provider, which is analogous to your factory?
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-guice.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-guice/8bb1b5e8-9dba-4078-a2a2-9572d11c5f00%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to