Charles Roth
2018-01-30 16:45:23 UTC
A bit of a philosophical question, here. Any thoughts welcome.
So I've adapted a project to use Guice to create, say, object Alpha, which
gets @Inject-ed into other objects. Everything works fine.
In theory, no code in the project (other than unit-tests) should ever use
'new' to create an Alpha. Suppose I want to *enforce *that? What can I do?
1. I could @Deprecate the constructor (and explain it in a comment) so
that anyone who attempts to try to use 'new' on it will at least get warned.
2. I could make the constructor default or protected, and put Alpha in
the same package as the Guice module class.
3. I could add some horrible static to Alpha, that keeps track of how
(or how many) times it gets constructed, and throw an exception. (YUCK!!)
4. I could examine the stack trace in the constructor, and throw an
exception if Guice is not in the chain. (Ditto ditto testing class.)
(Also yuck.)
5. I could let go of my need to micro-manage things, accept the
philosophy behind IOC, and simply accept that Guice will construct Alpha,
and the rest of my team won't do really dumb things. (OK, maybe I could
add a comment to the constructor, too :-) ) See for example, Uncle Bob at
http://blog.cleancoder.com/uncle-bob/2015/07/01/TheLittleSingleton.html
6. I could argue with the developers of Java 10 (11, 12...?) and beg
them to *please *put something like "friend" in Java, so I could (in
this case) make the Guice module a "friend" of Alpha (or is it vice-versa?).
Did I miss anything? How would you argue this in your shop?
Cheers!
So I've adapted a project to use Guice to create, say, object Alpha, which
gets @Inject-ed into other objects. Everything works fine.
In theory, no code in the project (other than unit-tests) should ever use
'new' to create an Alpha. Suppose I want to *enforce *that? What can I do?
1. I could @Deprecate the constructor (and explain it in a comment) so
that anyone who attempts to try to use 'new' on it will at least get warned.
2. I could make the constructor default or protected, and put Alpha in
the same package as the Guice module class.
3. I could add some horrible static to Alpha, that keeps track of how
(or how many) times it gets constructed, and throw an exception. (YUCK!!)
4. I could examine the stack trace in the constructor, and throw an
exception if Guice is not in the chain. (Ditto ditto testing class.)
(Also yuck.)
5. I could let go of my need to micro-manage things, accept the
philosophy behind IOC, and simply accept that Guice will construct Alpha,
and the rest of my team won't do really dumb things. (OK, maybe I could
add a comment to the constructor, too :-) ) See for example, Uncle Bob at
http://blog.cleancoder.com/uncle-bob/2015/07/01/TheLittleSingleton.html
6. I could argue with the developers of Java 10 (11, 12...?) and beg
them to *please *put something like "friend" in Java, so I could (in
this case) make the Guice module a "friend" of Alpha (or is it vice-versa?).
Did I miss anything? How would you argue this in your shop?
Cheers!
--
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 google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/7365593c-2d6d-4651-80da-cd3cede48855%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
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 google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/7365593c-2d6d-4651-80da-cd3cede48855%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.