C++ Singleton design pattern

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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值