• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why are business services classes designed as singleton  RSS feed

 
Naresh Shanmugam
Ranch Hand
Posts: 90
  • 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
Sheriff
Posts: 37256
519
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: 90
  • 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
Sheriff
Posts: 37256
519
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
Marshal
Posts: 55793
164
  • 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: 7821
142
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!