GeeCON Prague 2014*
The moose likes Java in General and the fly likes need clear solution Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "need clear solution" Watch "need clear solution" New topic
Author

need clear solution

nareshk kumar
Greenhorn

Joined: Sep 22, 2005
Posts: 4
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

Joined: Sep 20, 2005
Posts: 25
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?


Software is too expensive to build cheaply...
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


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.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Of course, you could be kinky and have your clone return this!


There is no emoticon for what I am feeling!
Edwin Keeton
Ranch Hand

Joined: Jul 10, 2002
Posts: 214

The Doubleton pattern emerges.


SCJP, SCWCD
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170



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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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

Joined: Sep 19, 2004
Posts: 531
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.
 
GeeCON Prague 2014
 
subject: need clear solution