• Post Reply Bookmark Topic Watch Topic
  • New Topic

Singleton Pattern query  RSS feed

 
Sucheta Shrivastava
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a code ManagerService  that prints the instances boss1 and boss2. Both have same hashcode.  How will i come to know which boss is it boss1 or boss2 .  i cannot makeout since the instances are same hence hashcode are same. how to know  which boss is it.





 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, that is an incorrect way of setting up a singleton.
It is possible to end up with more than one copy of ManagerService in a multi-threaded environment with that code in getInstance.

boss1 and boss2 are not "instances" they are references to the same instance.  Trying to figure out which reference you are using makes no sense in this respect.
 
Sucheta Shrivastava
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:First, that is an incorrect way of setting up a singleton.
It is possible to end up with more than one copy of ManagerService in a multi-threaded environment with that code in getInstance.




Then what is the correct way . please show.



boss1 and boss2 are not "instances" they are references to the same instance.  Trying to figure out which reference you are using makes no sense in this respect.


Still how do you figure them u. i am being asked to do that
 
Campbell Ritchie
Marshal
Posts: 56520
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best way is to write an enum with one member; you might call it INSTANCE. It is probably a good idea to make the object immutable. Give it a private constructor, which you can pass as many arguments as you like. Give it methods, again as many as you like. What you are doing is writing a class, then restricting it to a single instance.
If you look in books like Effective Java™ by Joshua Bloch, you will find out lots more about singletons.
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if (for some reason) you can't use an enum, then you get much the same effect:



As for boss1 and boss2, you can't.
They are the same instance.

If you;re being asked to write code to identify which reference has been used, then either you have misunderstood what is being asked, or the person asking it doesn't know what they are asking for...or they're testing you.
 
Grant Callant
Greenhorn
Posts: 11
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Singletons are a funny design pattern, sometimes labeled as an anti-pattern. The idea is to only allow one instance to be created of a type of object. Most of the time this isn't a good idea, and can work against you. Sometimes however this can be a good thing. Consider if you were wanting to use a logger- Java supports several loggers, sf4j, which is built in, and log4j are two I'm quite familiar with. The job of a logger is to get errors and exceptions which normally would be printed to the screen or console (not good in production programs), and write them to a file. You don't want a bunch of loggers all opening the same file up, and writing the same information over and over (besides that the OS would not allow this). You also don't want a bunch of loggers all printing errors to dozens of different files! This is a great place to use a singleton, as it prevents creating new loggers if one has already been instantiated. In fact, sf4j and log4j both use a singleton design pattern in their underlying code.

In your problem, you have a manager, and you only want to allow one (even though most companies I know of have several managers! ). Calling get instance is essentially just calling the same manager by a different name (not a perfect analogy). So when you call to your boss (boss1) by the first name (reference), she essentially answers back "Yes, I'm here". When you call to your boss (boss2) again by the second name (reference) she again answers "Yes, that is still me."

As Dave said, although your code does not appear to use multiple threads, your singleton example could actually result in multiple managers, with multiple threads (called a "race condition"). Typically the way to fix this was using the keyword synchronized, which puts up a flag (mutex) that said a thread was already using the code, so no other thread could get to it until it was done. Better ways are using Initialization on Demand Holder (IODH) (an example here: http://archive.oreilly.com/pub/post/singletons_and_lazy_loading.html).

Also, as Campbell mentioned, a better way to do this would be to use an Enum.
 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:And if (for some reason) you can't use an enum, then you get much the same effect:

I prefer this way as it is clearer what you are intending.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!