Hello. I have this question on DI and factories. I have read a few articles about DI and Guice in particular, saw a few videos (like the one with dhanji and limpbizkit, and also one with crazyboblee. As far as I understand, we should get rid of factories and static methods in them. For example, I should not create the static holder class and lazy instance in a singleton, it should be a normal public class, with public constructor* and I should rely on Guice to instantiate it and use it as singleton? The same with factories? They should have constructors and no static getFactory() methods - let Guice worry about this? Is this correct? But in this way I cannot prevent users of my classes to instantiate them freely as with factory methods. How should I go about this?
I have a factory that can return me 10 implementations of a given interface, based on an enum parameter in the create() method. I made it a class with no static methods, and a public constructor so that Guice can instantiate and inject it. However, the create method still has new XXX() statements. Should I use new inside the method? Or what is the Guice way of doing that? Another factory is like this - I created a kind of a repository of objects that uses the filesystem. Whenever the user wants to get an instance of the repository, he calls the static get() method with a file to serve as a root to that repository. The code inside canonicalizes the root, checks in a static map if such a repo is already created, and if so it returns it, if not, it creates a new one, inserts into the map and returns the new instance. All is synchronized of course. How to program this the Guice way? Thank you, jambalaya * I am using public class and constructors as the requirement is that there is no Guice dependency in the code in the implementation classes, so I use a different project for Guice module that uses @Provides methods (different package, so package-private constructors can't be used as well) when I need to inject something, and if an object has no dependencies and I want to bind it, I just rely on Guice and the default constructor. Maybe there is a better way? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
