The singleton design pattern

One of the many design patterns mentioned in the book Design Patterns, the singleton is a means of controlling the access to, or the instantiation of, an object for which the number of instances must be strictly controlled at runtime.

Using a singleton, static methods within the code can decide whether an object has been, or should now be instantiated. This is very useful when one type of object may or may not exist and is needed, for example, a file manager...

START FileManager.h

  class FileManager {
  public:
    static FileManager *Instance();
    void ShredFile(char *FileName);

    // destructor - anyone may delete us, the only requirement is that this is called
    //    at least once after instance is called to prevent memory leaks.
    ~FileManager();

    // other file manager member functions    
  private:
    // Instance pointer - the project wide instance of file manager.
    static sm_FileManager *_instance = NULL;

    // constructor - prevents other classes from making new FileManagers
    FileManager();

    // other file manager member functions
  }

END

START FileManager.cpp

  FileManager *FileManager::Instance() {
    if(_instance != NULL) {
      return(_instance);
    } else {
      _instance = new FileManager();
    }
  }

  // other operations...

END

START main.cpp

  void main() {
    char *szFileToShred = "Credit Card Numbers.txt";
    FileManager *fm;

    fm = FileManager::Instance();
    fm->ShredFile(szFileToShred);
  }

END

Of course this example is simplistic, but it illustrates the principles. In most modern medium to large scale software projects, the number of objects can grow quite large, and tracking the existence or non-existence of objects to control resources can be greatly simplified by a singleton object.

A singleton is a good substitute for an object that would be global in a throw away or utility project. It has the added advantage of being able to logically decide if it should return an existing object, a new object or no object at all based on how the Instanciate() function is written.

Here is the above code which was (submitted in C++) in Java.

FileManager.java

public class FileManager {
 
   private static FileManager instance = null;

   public static FileManager getInstance() {

        if ( instance == null ) { 

            instance = new FileManager();

        }

        return instance;
    }

}

Main.java

public class Main {


    public static void main( String args ) {

        FileManager instance = FileManager.getInstance();

    }

}

In set theory, a singleton is a set with exactly one element. The simplest example of a singleton is the set containing the empty set:  { {} }

Singleton -- 1. A single woman in her early 30s who at times
is either an empowered, confident, content, feminist
professional; or, 2. an insecure, self-image-loathing,
job-hating, fool-acting (where men are concerned) whiner
who will "end up all alone, half-eaten by an Alsatian."

The above is a definition of the term "singleton" invented by Helen Fielding, author of the hit novel (and co-writer of the hit film) Bridget Jones's Diary. The definition comes from the CNN website.

There has been a backlash against the labelling and stereotyping of "singletons" as depicted in the novel. Some females claim that single women in their 30s have been portrayed as desperate wine-guzzling lunatics who can't carry out a conversation with a male. Others believe that Bridget is a true modern woman; she worries about her weight, tries to quit smoking on a regular basis, and can't get her love life organised. The former are embarrassed to be linked to such a woman, the latter happy to know that they aren't the only ones making a mess of their lives.

Sin"gle*ton (?), n.

In certain games at cards, as whist, a single card of any suit held at the deal by a player; as, to lead a singleton.

 

© Webster 1913.

Log in or register to write something here or to contact authors.