While designing a service class should it be singleton in java? Generally DAO is made singleton, so should the calling Service class be made singleton also?
3 Answers
IMHO yes, services should not hold state and should therefore be made singleton.
-
3@Jyotirup Using a singleton properly will not impact performance (if anything it will improve it) Dec 29, 2011 at 16:19
-
6@Jyotirup: That's wrong, you mix up threads and objects... Synchronization is a different topic.– homeDec 29, 2011 at 16:27
-
2@thinksteep, My feeling is that singletons should be practically stateless and there for thread safe. Dec 29, 2011 at 16:38
-
1@home Like Peter pointed out, I was meaning in a multi threaded application we will face the issue– JyotirupDec 29, 2011 at 16:39
-
1@Peter, I agree that singletons should be stateless, but when you are designing singleton for a service, can we really implement as stateless?– kosaDec 29, 2011 at 16:42
Singletons are bad, if you develop them. If you are using dependency injection, let the DI container handle the singleton nature of your Service object. If you are not using dependency injection, use a static method instead of a singleton.
Classic example of bad:
public class HootUtility // singleton because developer was a goofball.
{
...
public void blammy(...) { ... }
public HootUtility getInstance() { ... }
}
... somewhere in the code.
HootUtility.getInstance().blammy(...); // This is silly.
Better implementation of the above:
public class HootUtility // Not a singleton because I am not a ______. (fill in the blank as you see fit)
{
// You can limit instantiation but never prevent instantiation.
// google "java reflection" for details.
private HootUtility()
{
throw new UnsuppotedOperationException();
}
public static void blammy(...) { ... }
}
... somewhere in the code.
HootUtility.blammy(...);
If you have a service interface that has an concrete implementation, use a dependency injection framework to inject the implementation (DI frameworks include: spring and guice).
Edit: If I was using spring, I would choose singleton scope (the default).
-
Interesting approach about throwing exception in constructor. Personally I just left it blank. Do you actually do it in your production code? Dec 29, 2011 at 16:25
-
-
1In general I agree with you. Nevertheless, option #1 is a reasonable solution if the object (singleton) has to maintain internal state.– homeDec 29, 2011 at 16:31
-
@Jyotirup In Spring community it's common to make it Singleton since it's default scope. In Guice - no scope or "prototype". See my answer for some extra notes. Dec 29, 2011 at 16:32
-
@StasKurilin So in a multi threaded application making it singleton will hit on the performace?– JyotirupDec 29, 2011 at 16:38
No
Actually I believe you shouldn't care about it while design. Like @DwB mentioned DI framework should do this job. Furthermore I believe no scope ("prototype") should be default and I don't see anything bad if somebody will create it itself. Also that issue can be simplified by modularization and separating service interface and implementation like best practices told as to do.
-
-
@Jyotirup I don't think so. If they are state free you can change and you are using DI properly you can change it easily whenever you want. Dec 29, 2011 at 16:49
-
1I'd say it would be a bad approach to prototype. Think about if you're making thousands of requests on your web app, or even just hundreds. That's hundreds of instances of a class being initialized. Conceptually, if the creation of the class and its dependencies (eg. dao prototypes with db connections) is expensive enough, prototyping services could lead to thrashing faster, especially in the event of a DDoS. Singletonned services with a DI framework like Spring's @Autowired would cut down on some unnecessary resource allocation Dec 26, 2017 at 9:41