views:

131

answers:

3

What are the best examples of Singleton Pattern in Java API? Is Runtime class a singleton?

+4  A: 

Only two examples comes to mind:

See also:


Update: to answer PeterMmm's (currently deleted?) comment (which asked how I knew that it was a singleton), check the javadoc and source code:

public class Runtime {
    private static Runtime currentRuntime = new Runtime();

    /**
     * Returns the runtime object associated with the current Java application.
     * Most of the methods of class <code>Runtime</code> are instance 
     * methods and must be invoked with respect to the current runtime object. 
     * 
     * @return  the <code>Runtime</code> object associated with the current
     *          Java application.
     */
    public static Runtime getRuntime() { 
        return currentRuntime;
    }

    /** Don't let anyone else instantiate this class */
    private Runtime() {}

It returns the same instance everytime and it has a private constructor.

BalusC
There a few at the Swing/AWT API, too. Singletons should be used with care. That's why there aren't so many in the whole Java API
Hardcoded
You can have multiple `Desktop` objects within the same VM. On the Sun/Oracle JRE applets from different sites will have different `Desktop`s. `Runtime` doesn't carry any state, so at the very least isn't usefully a singleton (and you will notice that its use is pointlessly verbose).
Tom Hawtin - tackline
@Tom: the `Desktop` is indeed more a multiton. About the `Runtime`, I was wondering the same... They must have *something* in mind when designing it during the dark JDK 1.0 ages.
BalusC
@BalusC My best guess is that they were intending to return a different implementation depending on platform, like `Toolkit`. That could have been done more humanely by providing static methods that forward. Of course, I think everything should have been done in a "Parameterise from Above" style.
Tom Hawtin - tackline
A: 

Note singletons should be used with care and reflection. Consider the arguments against singletons and your situation before you implement one. Overuse of singletons is an anti-pattern - similar to global variables.

Singleton Wiki Article

Java Dev on Singletons

Why Singletons are Evil

I've used them in the past and see some benefit to them. I've also been highly annoyed when trying to do test driven development with them around that's one area where they are evil. Also inheriting from them results in some hard to understand behaviour - at least in Python - I don't know for sure in Java. Generally you just don't do it because of that. So like threading these seem like a great idea at first and then you run into the pitfalls and realize well maybe this isn't so good after all.

Khorkrak
A: 

This is for Swing : SingleFrameApplication. Check out this presentation it wonderfully describes how does it work.

Xorty
This isn't a singleton and also not part of standard Java API.
BalusC
Why do you think it isn't a singleton ?
Xorty
It's an abstract class and there's no `getInstance()` method or similar which returns *self*. The javadoc also doesn't tell any word/hint about it. That whole class more look like a static factory with some extra's.
BalusC
Btw: use `@nickname` to notify others about contra-comments in other's answers. I wasn't notified about your comment until I peeked this topic once again.
BalusC
@BalusC : well it actually extends Application https://appframework.dev.java.net/nonav/javadoc/AppFramework-1.03/index.html - look at getInstance method. I am quite sure that this is singleton :)
Xorty
If the class is extendable, then it means that it doesn't have a `private` constructor, then it's per definition not a singleton. It's a factory class (which "by coincidence" returns the same instance everytime).
BalusC
@BalusC : Can you explaing this then? [from javadoc] : getInstance() The Application singleton, or a placeholder if launch hasn't been called yet.
Xorty
I'd say, the word "singleton" is poorly used here. You see, it can even return a placeholder. I think they chose the word "singleton" to make clear that a static instance is been used.
BalusC
@BalusC : I think in SingleFrameApplication whole name "SingleFrame" is poorly used, since it can have more frames, it looks odd. But there is another class - ApplicationContext in link I posted here in comment. Also presentation I posted in answer describes that quite well, but it's little more difficult singleton. Or, is it? You made me totally unsure :)
Xorty