• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Service Locator

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everyone,

We implement a service locator as a singleton with a cache.
cache = Collections.synchronizedMap(new HashMap());
How does it become a singleton?Is it by using a synchronizedMap?
Does the synchronizedMap make it a sigleton?
What about the multiple classloaders in the J2EE container which make it impossible to be a true singleton?
Could anyone offer some help?
best regards,
Roul
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Roul Ravashimka:
We implement a service locator as a singleton with a cache.
cache = Collections.synchronizedMap(new HashMap());
How does it become a singleton?Is it by using a synchronizedMap?
Does the synchronizedMap make it a sigleton?


No, not at all.
Why do you want to make it a singleton in the first place?
 
roul ravashimka
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm wrong about:
cache = Collections.synchronizedMap(new HashMap());
would make it a singleton.
In the book J2EE Core Patterns, the say that it is implemented as a singleton. But how do you achieve this?
Is it because you cant say: ServiceLocator sl = new ServiceLocator();
You can only say: ServiceLocator sl = ServiceLocator.getInstance();
Althought they also say that the J2EE container has multiple class loaders, thereby it can't be a singleton.
Why i think it should be a singleton? If it's a singleton, there would be only one instance, so only one cache to be maintained.
If it isn't a singleton, there can be more than one instance, so also more than one cache which needs to be filled up with EJB Home objects,...
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can make a class singleton by overloading its default constructor to be protected and having a factory class inherit from it. The factory class will have a method that will give you the singleton instance anytime you ask it!
Hope this helps.
sanjay.
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My coffee hasn't sunk in yet, but I think:

would do for starters.
Amend or criticize at will -- still groggy.
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or just

Anyway, the singleton pattern doesn't strictly say "exactly one instance" but a "known, limited number of instances"...
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Never use that "if null let's create one" code in a multi-threaded or multi-processor environment, even if you learned it from Sun sample code. It's flat broken. Here are a few links if you need convincing: http://www.javaworld.com/javaworld/javaqa/2002-04/01-qa-0412-doublelock.html
This kind of cache doesn't really have to have exactly one instance in the world. One per JVM in a distributed setup is fine, and one or two extras due to class loader quirks wouldn't cause any loss of life. But the cache adds value by sharing cached items with many client objects, and singleton or utility (all static methods & variables) classes are handy ways to do that.
Give some thought as to how caches get loaded. That's frought with nearly as much danger as the singleton pattern itself.
[ April 10, 2004: Message edited by: Stan James ]
 
And inside of my fortune cookie was this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic