SingletonMonostateSingletons

From Epowiki

Jump to: navigation, search

Contents

Singleton's have the following disadvantages

  1. Their deletion is undefined. You have to do something special at the end of the program to make sure they get destroyed.
  2. Derivatives of Singletons are not Singletons; the Singleton code must be explicitly added to them.
  3. Singletons must always be accessed through a pointer or reference. Access to their variables and functions is always indirect.
  4. Obtaining the pointer or reference to the Singleton always involves a small amount of overhead.

Singleton's have the following advantages:

  1. Singleton's can have base classes that are not singletons. That is, if you have an existing class, you can create a derivative that is a Singleton.
  2. Singleton's are allocated once and only once.
  3. Interesting policies can be added to the method that provides access to the singleton pointer or reference. These policies may check that the system is in a state that is proper for the creation of the singleton, or that the requestor has the necessary priviledges to access the singleton,etc.
  4. Singleton's can have non-trivial constructors.


Monostate classes (classes with nothing but static data members) have the following disadvantages:

  1. All classes in the Monostate hierarchy must be monostate. Existing classes cannot be made monostate through derivation.
  2. No instantiation policy can exist for Monostate classes, The space is allocated for the variables at before main is called.
  3. Monostate classes cannot have significant constructors, unless those constructors behave like singletons, ensuring that the variables are instantiated only once. (This gets hairy when the Monostate hierarchy isdeep)
  4. Monostate instances may be allocated and deallocated many times. (Not their variables, but the instances...)

Monostate classes have the following advantages:

  1. Derivatives of Monostate classes can also be monostate.
  2. Monostate classes can have virtual functions that are polymorphically dispatched. Many derivatives of the same monostate can exist in the same program and can institute different policies for dealing with the monostate variables.
  3. Monostate objects are destroyed when the program ends. There are no deletion ambiguities.
  4. Access to monostate objects does not have to be through pointers or references. Variable and function access can be direct.
  5. Users of monostate classes do not need to know that they are Monostate. They can be created with new, destroyed with delete, or created on the stack. There can be any number of instances in existance at any given time, but they will always refer to the same variables.
Personal tools