Singleton - PowerPoint PPT Presentation

About This Presentation
Title:

Singleton

Description:

The Singleton pattern ensures that a class is only instantiated once and ... Declare a static instance of each subclass. Benefits of the Singleton Pattern ... – PowerPoint PPT presentation

Number of Views:299
Avg rating:3.0/5.0
Slides: 15
Provided by: csr8
Learn more at: http://www.cs.rpi.edu
Category:

less

Transcript and Presenter's Notes

Title: Singleton


1
Singleton
  • Christopher Chiaverini
  • Software Design Documentation
  • September 18, 2003

2
What is the Singleton Pattern?
  • The Singleton pattern ensures that a class is
    only instantiated once and provides a global
    access point for this instance

3
Creational Pattern
  • Abstracts the instantiation process
  • Object Creational Pattern
  • Delegates the instantiation to another object

4
Why use the Singleton Pattern?
  • It is important for some classes to only have one
    instance
  • It is also important that this single instance is
    easily accessible
  • Why not use a global object?
  • Global variables make objects accessible, but
    they don't prevent the instantiation of multiple
    objects.

5
Solution
  • Make the class responsible for keeping track of
    its only instance
  • The class can ensure that no other instances are
    created
  • This provides a useful way to access the instance
  • This is the Singleton pattern

6
Examples of Singleton Patterns
  • Print Spooler
  • File Systems
  • Window Managers
  • Digital Filters
  • Accounting Systems

7
Sample Class
Class Singleton public static Singleton
Instance() protected Singleton() private
static Singleton _instance
8
Sample Implementation
Singleton Singleton_instance 0 Singleton
SingletonInstance () if (_instance 0)
_instance new Singleton
return _instance
9
Things to Notice
  • Instance() function ensures that only one
    instance is created and that it is initialized
    before use
  • Singleton constructor is declared as protected
  • Direct instantiation causes errors at compile
    time
  • Since _instance is a pointer, Instance() can
    return a pointer to a subclass of Singleton

10
Caution
  • In C a Singleton cannot be defined as a global
    or static object that relies on automatic
    initialization
  • This does not guarentee that only one instance of
    the static object will be created
  • There might not be enough information to create
    every Singleton at static initialization time
  • C doesn't define the order in which
    constructors for global objects are called
    dependencies between Singletons will cause errors

11
Derived Classes
  • Code should be able to use a subclass of a
    Singleton without being modified
  • What is the best way to do this?
  • Specify the Singleton in the Instance() method
  • Implement Instance() in the subclasses
  • Neither approach is very flexible

12
Registry of Singletons
  • Singleton classes register their instance with
    the registry
  • Registry maps between string names and Singletons
  • Singleton name can be specifed at startup by the
    user or an environment variable
  • Problem How to register classes?
  • Declare a static instance of each subclass

13
Benefits of the Singleton Pattern
  • Controlled access to sole instance
  • Reduced name space
  • Allows refinement of operations and
    representation
  • Allows a variable number of instances
  • More flexible than class operations

14
Use the Singleton Pattern when...
  • There must be exactly one instance of a class
  • This instance must be accessible from a
    well-known access point
  • The instance should be extensible by subclassing
  • Clients should be able to use a derived class
    without modifying their code
Write a Comment
User Comments (0)
About PowerShow.com