Sign up ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

They both seem to take about the same amount of effort to use:

Singleton: needs a .h and .m file, has to be imported into any class you want to call it from and then has to be instantiated

How is that any different than a class?

share|improve this question

closed as too broad by MichaelT, Snowman, durron597, GlenH7, Ixrec Sep 2 at 18:29

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

I think you might be misunderstanding what a singleton is.. In OO terms, A singleton is a class. –  David Cowden Aug 10 '12 at 17:50
@DavidCowden A singleton is really an object, not a class. Or rather, it is the set of all possible objects of a class, where the size of the set is 1. –  Matthew Flynn Aug 10 '12 at 21:42
@MatthewFlynn Yes, and no.. -_- The logic that defines a class such that only one instance is ever created and references to that instance are returned when a new instance is requested reside in the scope of a class. It's usually a class's responsibility to determine whether it should behave as a singleton or not, not the compiler's nor instantiator's.. –  David Cowden Aug 11 '12 at 1:07
@DavidCowden - Granted, in a class-based language, objects' behavior is generally defined by their class. That said, you can enforce the singleton notion with configuration rather than class definition using a framework such as Spring. It's not exactly the same thing, but it's close enough to actually be more useful. –  Matthew Flynn Aug 11 '12 at 2:40
in typical use in Objective-C, a singleton class will provide a class method returning the singleton object, and it's up to the caller to use that method and not try to create a second instance. Creating a second instance would be a blatant programming error, and the author of the singleton class takes no responsibility for blatant programming errors. –  gnasher729 Jul 4 at 12:48

2 Answers 2

A singleton isn't really the same as a class, it is more commonly known as a design pattern. Usually a singleton is a class such that only one instance of it can exist at a time.

Practically speaking, you could write a class with nothing special about it, create one instance at the beginning, and throughout your code, just use the same instance, and you could still call it a singleton. Normally, people add a little overhead code, so that an object is instantiated as per normal, but when someone tries to instantiate a second object from it, it returns the first object instead. This allows global data (some argue this is bad), it allows the object to be instantiated by what ever code needs it first by allowing all parts of code to reference it as if they were creating an object, and if you implement the destructors appropriately, you can even destroy and recreate the object if you need to do that for some reason.

You can check out for more information and sample code to enforce how the singleton will be used.

Design patterns are a topic addressed in the book called, "Design Patterns", by a group known as the "Gang of Four", which is a bit of a classic in the programming world. It looks at several different design patterns, and goes in to much more detail not only the implementation, but also why you would or should use one pattern for what kind of scenario. You can also look at for a briefer overview.

share|improve this answer
In Math, yes, a singleton is an abstraction used to describe a set with one one member and that needs no enforcing. In Computer Science/Engineering, a singleton is a design pattern in which a class ensures that only one instance of it is ever present in memory. It is the responsibility of the class and not the instantiator (programmer) or compiler to enforce this behavior. Therefor, being a singleton is a property of a class, which, could be restructured as a trait a class inherits, therefore making a class that behaves as a singleton exactly that, a singleton. –  David Cowden Aug 11 '12 at 1:17

A singleton is a class and behaves exactly like any other class, the only exception being that any instances of a singleton reference the same object data. This means that any instance of a singleton class are actually all the same instance.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.