| Why use a singleton class? |
This design pattern and methodology ensures that only one instance of the C++ class is instantiated. It assures that only one object is created and no more. It is often used for a logging class so only one object has access to log files, or when there is a single resource, where there should only be a single object in charge of accessing the single resource. The singleton pattern discussed here gives the class itself, the responsibility of enforcement of the guarantee that only one instance of the class will be allowed to be generated.
| Example of a C++ Singleton class: |
File: logger.hpp
-
Note:
- That the instance function returns a pointer to a static variable and thus is declared static.
- Only the class function Instance can call the constructor. Public access to the constructor is denied.
- The constructor, copy constructor and assignment operator are all private to ensure that the programmer using the singleton class can only create a single instance of the class using only the Instance() function.
- The life of the singleton instantiation is for the duration of the application.
File: logger.cpp
Usage:
| C++ Singleton class using inheritance: |
In this example the base class enforces a singleton pattern.
File: base.hpp
File: base.cpp
File: derived.hpp
File: derived.cpp
File: main.cpp
Compile: g++ main.cpp derived.cpp base.cpp
Results: a.out
-
5 7
| The Template Singleton class: |
One can inherit a singleton class from a base class or use a template for each type of instantiation. The Base class and Derived class relationship offers flexibility as there are no design limits in the derived class.
The Template singleton is also flexible and is used in a manner where any class can be turned into a singleton by using this template.
File: singleton.hpp
Usage:
| The static stack variable Singleton class: |
-
Note:
- This version of a singleton class initializes when the Instance() function is called.
- The Instance() function returns an instance instead of a pointer. The pointer is not exposed and thus a delete can not be inappropriately applied.
- [Potential Pitfall]: This form of the singleton can present a problem because of the life expectancy of the object. If one singleton is instantiated within another, one must be keenly aware of the destructor call sequence.
Usage:
Also see An Exception Correct C++ Singleton Template Base Class with Controlled Destruction Order
| Other variations and tips: |
Optimization option:
- This should slightly reduce the number of computed operations and thus is slightly more optimized.
The examples given above use a C++ global static variable. Other variations of a singleton can use:
- an environment variable.
- a file (A lock file is often used to insure a single instance of a process).
- a static STL list or STL map of values or paired values can be used by a singleton class as a singleton registry for other classes to verify singleton compliance.
本文详细介绍了C++中单例模式的实现方法及其应用场景。包括如何确保类仅有一个实例,以及如何通过不同方式实现单例模式,如使用静态成员函数、模板方法及静态局部变量等。
1530

被折叠的 条评论
为什么被折叠?



