• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Implementing Singleton

 
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The usual approach in implementing singleton I see is to create a class with:
1. private constructor - to prevent construction outside class
2. static getInstance() - to return the single instance
3. instance methods for whatever biz logic the class provides

But, in step 3, why not just simply use static methods/variables all the way? In this case, we still achieve 1 'instance' effect by having only the static version.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To get the flexibility of polymorphism. A Singleton can very simply be adapted to use different implementations, for example based on some configuration file or something. That's harder to do with a all-static utility class.

Both patterns share the disadvantage that it couples all clients to a global access point. That's why I don't use it very often.
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, you can never get an instance of java.lang.Math (which does what you describe). So, if you wanted to use a Math object in a JSP, for instance, you couldn't. If Math were a singleton, then you could ask for the singleton instance and bind it to the servlet request.

You actually can get an instance of java.lang.Math, but it's frowned upon.
 
Bartender
Posts: 1205
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why would you want an instance of a Math object in any class? What makes a JSP different from any other class?

Ryan
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Most JSP frameworks are bean-based, so you can't call static methods. So, having an instance of Math would be useful if you wanted to perform some advanced calculations in your JSP. Of course, most people would write a helper bean which does the calculations and just use that in the JSP. I just used Math as an example because that's what came to mind off the top of my head. If Math were a true singleton, you could just place it on the request and the JSP could have access to it easily without having to resort to scriptlet code.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic