Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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
2  
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
add comment

2 Answers

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 https://en.wikipedia.org/wiki/Singleton_pattern 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 https://en.wikipedia.org/wiki/Software_design_pattern 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
add comment

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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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