• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

need clear solution

 
nareshk kumar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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?
 
Robert Watkins
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • 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?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • 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.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course, you could be kinky and have your clone return this!
 
Edwin Keeton
Ranch Hand
Posts: 214
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Doubleton pattern emerges.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic