Download presentation
Presentation is loading. Please wait.
Published byHugh Ryan Modified over 9 years ago
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* Singleton::Instance () { 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.