• 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

cloning Singleton object

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Can we clone Singleton object, if not why ?
Actually I am trying to clone Singleton object it is showing error, even after implementing the Cloneable interface I am getting same error.

Can anybody explain me the proper reason.

Thanks in Advance
Shashanka
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why would you clone a singleton? It should be possible (it would, of course, no longer be a singleton) but why do it?
[ May 07, 2008: Message edited by: Paul Sturrock ]
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, the main feature of a Singleton is that it exists exactly once. So usually cloning does not make much sense. But as "once" is still relative, one can imagine scenarios where cloning of a Singleton is necessary.

But next: without the error message and the code for the Singleton class it is nearly impossible to determine the reason for the fail of clone().
 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Post the error message / stacktrace
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anti pattern for sure.
But generally cloning an object is simple and painless, unless it does not override the clone() method and is throwing an exception inside it.
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is that exception? Can you post your cloe() method?
 
Manuel Leiria
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Syam Sathyan:

But generally cloning an object is simple and painless,



It's not that simple! There's a lot of ins and a lot of outs that must be taken into account when cloning an object (for instance if the object contains fields that refer to mutable objects)
[ May 07, 2008: Message edited by: Manuel Leiria ]
 
Syam Sathyan George
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand deep cloning is another topic by itself.
Need to see the stack trace, to comment
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course, the Singleton design pattern and the Cloneable Java interface are at odds with one another.

Depending on what you mean by a cloneable singleton, and how you implemented the Singleton pattern in Java, it may be possible (if still undesirable).

(Should clone() return the singleton instance?)
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
2 ways of implementing clone.

1.


2.


HTH
V
[ May 07, 2008: Message edited by: Vishal Matere ]
 
Vishal Matere
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As you can see here , we never create duplicate objects of singleton class.
Only one object exists as per definition of Singleton pattern.

V
 
Listen. That's my theme music. That's how I know I'm a super hero. That, and this tiny ad told me:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic