• 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

To Make a HashMap as a Singleton

 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Happy Holidays!

I have a value object that is cached on the application for frequent access. This value object ( a Java class) happens to be a HashMap. Therefore, I have something like:

In order to manage the access to this HashMap, I would like to create a singleton. Normally, I create a singleton this way:


I am confused about putting the two code snippets together - how do I make a HashMap a Singleton for accessing?
 
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds fine to me, although if this is a J2EE environment, singletons aren't actually unique (despite the name) so you'd want to be careful of that. Also, you'd want to make get/put methods for accessing/creating the singleton synchronized unless its read only. Keep in mind, a singleton is a design pattern, not a specific implementation, so there are multiple ways to create one, some that are bad and others that are good.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Scott Selikoff:
Sounds fine to me, although if this is a J2EE environment, singletons aren't actually unique (despite the name) so you'd want to be careful of that. Also, you'd want to make get/put methods for accessing/creating the singleton synchronized unless its read only. Keep in mind, a singleton is a design pattern, not a specific implementation, so there are multiple ways to create one, some that are bad and others that are good.



Why is a singleton "not unique in a J2EE environment"? If that is the case, then a singleton is not unique ever. Why do we say "the class loader is the universal scope, unless you're in a J2EE environment"?

The truth of the matter is that a singleton cannot exist without a formal proof that the universe is finite. By restricting the universal scope to "class loader", you are merely defining "a singleton within the scope of a class loader", not a "singleton". I could quite easily shift the scope around, for example, I might say:
"a singleton within a method" - we call that a local declaration
"a singleton within an object" - we call that a field
"a singleton within a thread" - we call that a thread local
"a singleton within a JVM" - I imagine this would require some kind of class loader registry (I've never attempted it)
"a singleton within several JVMs" - getting pretty complex now
"a singleton within all JVMs" - is the universe finite?

As you can see a "singleton" has no definition, and it doesn't take much analysis to prove its superficial existence. This is entirely beside the point that a "singleton" as the de facto definition stands (class loader scoped) is a euphemism for a requirement defect, failure to abstract appopriately, global (within the defined scope) variable. It is only ever (ab)used to update state in such a way that can be globally (class loader scoped) accessed, since many applications run under one class loader (a "nice" place to define a global scope).

Granted, talking in euphemisms is demonstrated to be a sound financial investment, and I've just bought a new car and motorbike, so I'm feeling the heat
 
reply
    Bookmark Topic Watch Topic
  • New Topic