• 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

need clear solution

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai,

is there any possibilities to apply clone methode on singleton class oblect?

if possible , how it will be satisfied the singleton design pattern?

if not possible, why ? and what happens by the jvm?
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by nareshk kumar:
hai,

is there any possibilities to apply clone methode on singleton class oblect?

if possible , how it will be satisfied the singleton design pattern?

if not possible, why ? and what happens by the jvm?



Of course it is possible. If your Singleton class implememnts Cloneable, and makes the clone method public, then you will be able to clone the Singleton. This may even be desirable, depending on exactly [i]what[\i] the nature of the singleton is.

What happens is what you'd expect: you get a new object with the same data inside, exactly like any other clone.

Does it satisfy the Singleton pattern? Who cares - it's not like you get points for how closely your solution conforms to a pattern. The real question is: does it solve whatever it is you are trying to do?

Strictly speaking, of course, it does _not_ count as a Singleton any more, because you've created a way to make multiple instances. However, like I said - who cares?
 
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


if not possible, why?


Why would you want to implement a Clonable Singleton? The purpose of the Singleton pattern is to ensure there is only one instance of an object in a JVM. If you want more than one, you don't want to use a Singleton.
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its ok to 'implement' Cloneable, but don't make the clone method public. If its a singleton, there is no need to clone it. Perhaps its a multiton?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm pretty sure the original poster just sees the "clone()" method in Object and is worried that someone might call it on his Singleton object.

Although every class does inherit clone(), the clone() method actually doesn't work unless (1) The class implements the Cloneable interface, and (2) The class overrides clone() to make it public. It's a very strange design!

In any case, you don't need to worry: if a class doesn't want to be cloned, it can't be. clone() only works on classes that take the specific steps above.
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course, you could be kinky and have your clone return this!
 
Ranch Hand
Posts: 214
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Doubleton pattern emerges.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I haven't figure out how to work this scheme with final methods yet, but I'm working on it.

No way around it. Constructing methods must call super first.
[ September 22, 2005: Message edited by: Mr. C Lamont Gilbert ]
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mr. C Lamont Gilbert:
.. a terrible idea... something wrong with clone()...



So why is overriding it to make it public a terrible idea (given that that's what Gosling says to do in "the Java programming language)? And given that something is terribly wrong with clone()[*], what's wrong with spreading the word about that?

---
[*] This part is just my opinion -- that Object.clone() is a crazy design that should never have been used.
 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by nareshk kumar:
hai,

is there any possibilities to apply clone methode on singleton class oblect?

if possible , how it will be satisfied the singleton design pattern?

if not possible, why ? and what happens by the jvm?



In other words, can you have more than one objects and still have one object? No.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic