The singleton pattern is intended to ensure that only one instance of a class or structure is created, and that subsequent requests for an instance of that class or structure returns the first instance created.
The singleton is typically accomplished through a global static variable that is managed through a function or method. On the first call, the function determines if the global variable has been set and it not, creates an instance of the appropriate class or structure and stores it in the global variable. The function then returns the contents of the global variable.
As an example, a singleton function looks like this in C++:
A smart pointer is used so the object is deleted when the program exits.
As can be seen in the above example, the singleton pattern is somewhat dependent on the features of the programming language being used and is primarily for object-oriented code. However, the pattern is so simple that creating an entire example to demonstrate it seems pointless.
The above example demonstrates everything needed to implement the basic factory pattern. For a more detailed example of the singleton pattern, see the following links into the Facade Pattern examples for several languages.
C
C++
C#
Python