singleton christopher chiaverini software design & documentation september 18, 2003

14
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003

Upload: hugh-ryan

Post on 26-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003

Singleton

Christopher ChiaveriniSoftware Design & Documentation

September 18, 2003

Page 2: Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003

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

Page 3: Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003

Creational Pattern

● Abstracts the instantiation process● Object Creational Pattern

– Delegates the instantiation to another object

Page 4: Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003

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.

Page 5: Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003

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

Page 6: Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003

Examples of Singleton Patterns

● Print Spooler● File Systems● Window Managers● Digital Filters● Accounting Systems

Page 7: Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003

Sample Class

Class Singleton {public: static Singleton* Instance();protected: Singleton();private: static Singleton* _instance;};

Page 8: Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003

Sample Implementation

Singleton* Singleton::_instance = 0;

Singleton* Singleton::Instance () { if (_instance == 0) { _instance = new Singleton; } return _instance;}

Page 9: Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003

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

Page 10: Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003

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

Page 11: Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003

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

Page 12: Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003

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

Page 13: Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003

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

Page 14: Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003

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