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.

6

1 Answer 1

-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().

6
  • Sorry, I forgot to make explicit that Singleton2 is not instantiatable.
    – Ky -
    CommentedFeb 17, 2015 at 2:34
  • Is it assumed the user won't use it in this way? Because that is a bad assumption.CommentedFeb 17, 2015 at 2:35
  • See my latest edit to my question; it cannot be instantiated.
    – Ky -
    CommentedFeb 17, 2015 at 2:36
  • But now they are both the same class.....CommentedFeb 17, 2015 at 2: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 -
    CommentedFeb 17, 2015 at 2:47

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.