• Post Reply Bookmark Topic Watch Topic
  • New Topic

Example for java doubleton pattern.  RSS feed

 
vighnesh udupa
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please provide me an example for java doubleton pattern.
Somebody please help provide me an example for java doubleton pattern ASAP or please provide me the link of some websites where I can get a good example for the same.

Thanks in advance.
Vighnesh
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
EaseUp

Take a singleton and modify it to create only two instance.

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and Welcome to JavaRanch
 
vighnesh udupa
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Seetharaman !

Can you please provide me the complete class example without using enum?

I need it very urgently.

Thanks again

Following is the example I am using for singleton. Is it possible to make this class doubleton by editing or adding some code?

 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vighnesh udupa wrote:Can you please provide me the complete class example without using enum?

We are NotACodeMill. you should DoYourOwnHomework.

I need it very urgently.

You were already told to EaseUp. And I'd also prefer if you KeepItDown. All caps is bad, large fonts is just as bad. I've reduced the size of your first sentence.

To have a doubleton you will need two instances. For scalability reasons I would suggest an array or List. In your get instance you then return the next one. That requires an index of the last returned / next-to-return element. The latter is probably easier so you can initialize it at 0. Ignoring object initialization, the return code would be something like this:
This distribution algorithm is sometimes called round robin. There may be better algorithms which actually check which instance is better. You're then going into the realm of instance pools, where an instance is returned when done (usually by calling a close() or dispose() method).
 
vighnesh udupa
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you. but didn't get your reply.
 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vighnesh udupa wrote:thank you. but didn't get your reply.

You are suggested to create n instances (e.g. 2).
By calling the getInstance() method, it would return k th instance where 0<= k < n.
By using the Round Robin approach, the chance of getting each instance is fair.

 
Maan Suraj
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to know is it ok to implement doubleton pattern like below


is above logic ok?i dont want to use cloning
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maan Suraj wrote:I want to know is it ok to implement doubleton pattern like below

There are a few things that look a bit strange about that implementation to me:

- Why do you have firstInstance/secInstance and an array? You only need one or the other (and, as Rob said above, the array is more flexible).

- Why does your getInstance() method return two instances?

- The first time you call it, you'll return an array containing one instance, and one null value.

- Oh, and finally: if it's not a singleton, don't call it a singleton! That'll just confuse people .
 
Maan Suraj
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah i should have rahter named it Doubleton ...my mistakte...


anyway thanks
 
Maan Suraj
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
- The first time you call it, you'll return an array containing one instance, and one null value.


So how do i prevent above? I thought i will check for null values in array and then may be decide how many objects i have.

Also, i have gone through Rob Spoor post above

1. // instances is the instance array
2. Doubleton instance = instances[index];
3. index = (index + 1) % instances.length; // wraps around. With 2 instances, it alternates between 0 and 1.
4. return instance;


Am not getting it completely. I understand its round robin and all, but where will the above code exactly fit in?
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the getInstance() method, which should only return one single instance.

Your code is very flawed, because you return the shared array. That means that anyone can call getInstance(), get the array, and change its element values.
 
Bhanu Prakash k
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vighnesh,

Below is the snapshot of the double ton pattern. getInstance() method will return the instance as a round robin fashion.


Any inputs to the above code is appreciated.

-Bhanu
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bhanu Prakash k wrote:Any inputs to the above code is appreciated.

There's a flaw in your synchronization. If two threads call it at the same time before it's initialised, both of them could perform the null-check before either of them hits the sync block. In that case four objects would be created.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And the access to "index" is not synchronized, allowing two threads to get the same Doubleton instance.
 
Bhanu Prakash k
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew/Rob

Thanks for the inputs. Below is the updated code and synchronized doubleTon to get Doubleton instance in round robin.

-Bhanu.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!