Java, Singletons and Uniformity

Technical discussion about the NMDC and <a href="http://dcpp.net/ADC.html">ADC</A> protocol. The NMDC protocol is documented in the <a href="http://dcpp.net/wiki/">Wiki</a>, so feel free to refer to it.

Moderator: Moderators

Locked
TheParanoidOne
Forum Moderator
Posts: 1420
Joined: 2003-04-22 14:37

Java, Singletons and Uniformity

Post by TheParanoidOne » 2004-03-21 15:36

I have a Java question that I'm hoping someone can give me some advice on. I thought I would ask somewhere close to home before venturing out into the big wide world. ;) Here goes ...

I am in the middle of a programme that has a number of Singleton classes and they are therefore structured similar to the following:

Code: Select all

class Singleton {
  private static Singleton instance = null;

  private Singleton(){
    // constructor code
  }

  public static Singleton getInstance(){
    if (instance == null){
      instance = new Singleton();
    }
    return instance;
  }

  // other code
}
As there are a number of classes that behave in this way, I would like to enforce some form of uniformity to ensure that they all implement getInstance(). It would also allow me to take advantage of polymorphism and treat all these classes in a similar way. Normally such uniformity would be enforced using Interfaces and/or Abstract classes. Due to the static nature of the method though, these two directions are not possible.

Does anyone have any clue as to what I could do? My Google searches led to many people asking similar questions. While being very interesting and informative, the information didn't really answer my question.

It's not the end of the world if I can't get it done, but the uniformity would be nice, as well as being an extra piece of knowledge which is always a good thing. :)

Thoughts?
The world is coming to an end. Please log off.

DC++ Guide | Words

Qbert
Posts: 73
Joined: 2003-06-07 03:12

Post by Qbert » 2004-03-21 18:40

My Visual Studio .NET 2003 is licensed under my name, and the same for my operating system... What about you?
I surf on an OC3 without limitations, two to be exact, and I'm not joking.

Qbert
Posts: 73
Joined: 2003-06-07 03:12

Post by Qbert » 2004-03-21 18:40

And I forgot to mention, even if you don't take the advice from that webpage, you should make your getInstance() synchronized.
My Visual Studio .NET 2003 is licensed under my name, and the same for my operating system... What about you?
I surf on an OC3 without limitations, two to be exact, and I'm not joking.

TheParanoidOne
Forum Moderator
Posts: 1420
Joined: 2003-04-22 14:37

Post by TheParanoidOne » 2004-03-21 19:00

Yep, seen that page. Good point about synchronised.

See, that's what I mean about uniformity. I know that I'll have synchronised in one class but forget it in another, and so on. I'd prefer that the compiler force (or at least gently nudge) me into typing the correct code.
The world is coming to an end. Please log off.

DC++ Guide | Words

FarCry
Programmer
Posts: 34
Joined: 2003-05-01 10:49

Post by FarCry » 2004-03-22 14:27

It won't help you with your problem, but I'd like to note that declaring the getInstance() method synchronized is very inefficient as it will force synchronization for each function call to a Singleton. It's better to move the Singleton construction to a second function (like the web page example does), declare that one synchronized and add a second check for null there - the getInstance() function can stay un-synchronized then.

[NL]Pur
Programmer
Posts: 66
Joined: 2004-07-21 14:32

Post by [NL]Pur » 2004-08-11 07:59

Code: Select all

class Singleton implements Single {
  private static Singleton instance = null; 

  private Singleton() { 
    // constructor code 
  } 

  public Object getInstance() { 
    if (instance == null) { 
      instance = new Singleton(); 
    } 
    return instance; 
  } 

  // other code 
}

interface Single {
	public Object getInstance();
}

TheParanoidOne
Forum Moderator
Posts: 1420
Joined: 2003-04-22 14:37

Post by TheParanoidOne » 2004-08-11 14:53

Hmm. Resurrecting a five month old thread. Interesting.

That code will not work. public Object getInstance() is an instance method. You need to call the method on an object of type Singleton.

eg. s.getInstance(); where s is a Singleton object.

But making the constructor private means that you can never externally create a new Singleton object using new and therefore you can't ever call getInstance().

Nice try, but I think you need to go over the existing thread again, especially my initial post.
The world is coming to an end. Please log off.

DC++ Guide | Words

GargoyleMT
DC++ Contributor
Posts: 3212
Joined: 2003-01-07 21:46
Location: .pa.us

Post by GargoyleMT » 2004-08-11 23:05

FarCry had some comments on the other hub, you might ask him (I don't want to post his text without asking him.)

TheParanoidOne
Forum Moderator
Posts: 1420
Joined: 2003-04-22 14:37

Post by TheParanoidOne » 2004-08-12 00:29

Well, at this stage it would be nothing more than an intellectual exercise, so meh.
The world is coming to an end. Please log off.

DC++ Guide | Words

[NL]Pur
Programmer
Posts: 66
Joined: 2004-07-21 14:32

Post by [NL]Pur » 2004-08-12 05:17

Hmm. Resurrecting a five month old thread. Interesting.
That code will not work. public Object getInstance() is an instance method. You need to call the method on an object of type Singleton.
eg. s.getInstance(); where s is a Singleton object.
if you call s.getInstance() you can typecast it to a Singleton object by:
(SingleTon)s.getInstance()

remember that every object in java is a child of the class Object ;)
But making the constructor private means that you can never externally create a new Singleton object using new and therefore you can't ever call getInstance().
This is just side effect ;) Make it protected if you want..
Nice try, but I think you need to go over the existing thread again, especially my initial post.
Is this your normal way of saying tx, for the suggestion ?

TheParanoidOne
Forum Moderator
Posts: 1420
Joined: 2003-04-22 14:37

Post by TheParanoidOne » 2004-08-12 06:49

[NL]Pur wrote:
Hmm. Resurrecting a five month old thread. Interesting.
That code will not work. public Object getInstance() is an instance method. You need to call the method on an object of type Singleton.
eg. s.getInstance(); where s is a Singleton object.
if you call s.getInstance() you can typecast it to a Singleton object by:
(SingleTon)s.getInstance()

remember that every object in java is a child of the class Object ;)
I'm well aware of the fact that all classes in Java inherit from the Object class. However you cannot randomly cast an object of one type into an object of type Singleton. You will cause a ClassCastException.
[NL]Pur wrote:
But making the constructor private means that you can never externally create a new Singleton object using new and therefore you can't ever call getInstance().
This is just side effect ;) Make it protected if you want..
That will make no difference. The protected keyword gives access to subclasses only. There is still no way to create a Singleton object.

Rather than just taking my word for it, I suggest you try out the code extracts you have provided.
The world is coming to an end. Please log off.

DC++ Guide | Words

[NL]Pur
Programmer
Posts: 66
Joined: 2004-07-21 14:32

Post by [NL]Pur » 2004-08-12 07:54

You must type cast right way, then it won't cause an Exception.

Protected methods are available for all classes in the same package. Has nothing todo with subclasses.

And you word for it ? I already tried before i posted to create the object and that worked fine. And it's a must do to use Objects in Interface otherwise the interface useless and can only be implemented with 1 certain class.

TheParanoidOne
Forum Moderator
Posts: 1420
Joined: 2003-04-22 14:37

Post by TheParanoidOne » 2004-08-12 08:50

[NL]Pur wrote:You must type cast right way, then it won't cause an Exception.
The time of casting is irrelevant. If I say

Code: Select all

String s = "hello";
Singleton st = (Singleton) s;
st.someSingletonMethod();
the compile will be fine, but at runtime you will get a ClassCast Exception because you cannot cast an object of one type to a completely unrelated one.
[NL]Pur wrote:Protected methods are available for all classes in the same package. Has nothing todo with subclasses.
You are correct about the package statement. My mistake. The protected access modifier *is* related to the concept of subclassing though.
[NL]Pur wrote:And it's a must do to use Objects in Interface otherwise the interface useless and can only be implemented with 1 certain class.
I'm not sure why you're still talking about interfaces as a solution to the problem when I've already stated that they're not. :?
The world is coming to an end. Please log off.

DC++ Guide | Words

[NL]Pur
Programmer
Posts: 66
Joined: 2004-07-21 14:32

Post by [NL]Pur » 2004-08-12 10:37

String s = "hello";
Singleton st = (Singleton) s;
st.someSingletonMethod();
This is a crappy example, First of All you didn't even compile my or the code you thought up because:, ClassCastException are always given by the compiler, that is a buildin securtiy thing of the VM.

offcourse you can't typecast a String into a SingleTon Class,

But the getInstance (in my example)is returning an Object. Since every Object inhertence from this class you can always typecast from it. That is one of the powers of Java :o)

So my example works i'm positive about that.
I'm not sure why you're still talking about interfaces as a solution to the problem when I've already stated that they're not.
everyone can make mistakes ;)

Locked