• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why are business services classes designed as singleton  RSS feed

 
Naresh Shanmugam
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any reason why business service classes(stateless) are designed as singletons(just once instance in the application). If the business service is stateless it could be designed as a static class as well. Can someone share some information on why it has been designed as a singleton please? What advantage does a singleton class has over the static class?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 36659
475
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Naresh,
In Java, static classes have to be inner classes. But those "look weird" when calling them. It is more API friendly to use a top level class. Hence the singleton.

Sometimes singletons are implemented with inner classes though . They just don't get referenced that way from the outside.
 
Naresh Shanmugam
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your response Jeanne. I meant class with full of static methods. Is there any difference between a class with full of static methods and a singleton class where only one instance exists per JVM. In my project service classes and DAO classes are singletons, why cant they be static methods in a class?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 36659
475
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Naresh,
In general, APIs with instance methods make it easier to mock out and add interfaces. And also make it easier to substitute a different implementation as needed.

A class with static methods makes the code more tight coupled so is often viewed as an inferior design.
 
Campbell Ritchie
Sheriff
Posts: 54067
130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A class with all static members is usually called a utility class. The idea of utility classes is to contain functions or constants for the use of other code. A utility class wouldn't therefore be much use for a DAO because all the data would be predetermined in the class.
 
Stephan van Hulst
Saloon Keeper
Posts: 7207
118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Except if the programmer does this:

A class with only static members, and useful as a DAO, but incredibly bad design.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!