0

I'm going to start by saying that I understand that programming in mostly class functions and variables can be harmful to object-orientation, and that most of the time an instance is preferred. I'll be using Java as my language for this question, but it should apply to any language which makes the distinction between class and instance scopes.

But there are still times when only a single instance is wanted, which calls for the Singleton pattern:

public class Singleton1
{
    private static Singleton1 instance;

    private Object myObject;

    private Singleton1()
    {
        myObject = new Object();
        // other initializations here
    }

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

    public Object getMyObject()
    {
        return myObject;
    }

    // and so on with the non-static public and private methods
}

This will be used as such:

Singleton1 singleton1 = Singleton1.instance();
singleton1.getMyObject();
// any other method called like singleton1.method(args);

This pattern forces programmers using the Singleton1 class to get instances via Singleton1.getInstance() instead of the usual new Singleton1(), since the only constructor is private. The main benefit of this is guaranteeing that there is only one instance of Singleton1 being used, and so any one modification rings true everywhere in the program.

My question is, why go through the rigamarole of keeping the constructor and instance private, when the static modifier takes care of all this already? See my example counterpoint below:

public class Singleton2
{
    private static Object myObject;

    static
    {
        myObject = new Object();
        // other initializations here
    }

    private Singleton2(){/*nothing*/}

    public static Object getMyObject()
    {
        return myObject;
    }

    // and so on with the static public and private methods
}

This will be used as such:

Singleton2.getMyObject();
// any other method called like Singleton2.method(args);

To me, this seems like less code and easier to keep up with.

Ky -
  • 525
  • 5
  • 17
  • I looked at http://programmers.stackexchange.com/q/34485/104052 and it seems to be specific to OP's case – Ky - Feb 17 '15 at 02:29
  • @Deduplicator looking at the question (which only consists of the sentence `Some hold that the Singleton Pattern is always an anti-pattern. What do you think?`), I am not sure if it addresses the same concerns as mine. – Ky - Feb 17 '15 at 02:40
  • 3
    @Supuhstar did you read the answers? In particular [this one](http://programmers.stackexchange.com/a/247340/40980)? –  Feb 17 '15 at 03:01
  • @MichaelT yes, and they all talk about singletons and why to/not to use them. I'm asking why use the singleton pattern instead of a static class, ignoring the benefits/drawbacks of a single-instance object. – Ky - Feb 17 '15 at 03:06
  • 1
    There is also http://programmers.stackexchange.com/questions/40373/so-singletons-are-bad-then-what and http://programmers.stackexchange.com/questions/235527/when-to-use-a-singleton-and-when-to-use-a-static-class and quite possibly http://programmers.stackexchange.com/questions/34485/what-is-the-difference-between-all-static-methods-and-applying-a-singleton-patte (probably the best) - but I can only dup to one question at a time. –  Feb 17 '15 at 03:08
  • @MichaelT 40373 is another that just talks about singletons and alternatives. 235527 gets much closer to the type of answer I want, so maybe that is the best candidate? And I actually address 34485 in my first comment. – Ky - Feb 17 '15 at 03:24

1 Answers1

-2

First and foremost, calling Singleton2's getMyObject() method will crash the program because it does not create a new instance when it is null, and will just return null.

Second, there is no getMyObject in a singleton class. There is just getInstance().

Lawrence Aiello
  • 1,398
  • 11
  • 12
  • Sorry, I forgot to make explicit that `Singleton2` is not instantiatable. – Ky - Feb 17 '15 at 02:34
  • Is it assumed the user won't use it in this way? Because that is a bad assumption. – Lawrence Aiello Feb 17 '15 at 02:35
  • See my latest edit to my question; it cannot be instantiated. – Ky - Feb 17 '15 at 02:36
  • But now they are both the same class..... – Lawrence Aiello Feb 17 '15 at 02:43
  • I think you need to look again. **First**, it's true that without any other fanciness, `Singleton2#getMyObject()` will return `null`, but I hoped to imply that within `static{}` and the unwritten static methods that `myObject` can be initialized and mutated. **Second**, singleton objects are not just there to be objects, otherwise you might as well use an `enum`. Singleton objects should have methods you can call just like any other object. **Finally**, they are not the same class, but they do the same thing, which is the heart of my question. – Ky - Feb 17 '15 at 02:47
  • Also remember that a method returning `null` will not crash the program. Trying to call a method on a null object will. – Ky - Feb 17 '15 at 02:54