本文首发于个人博客https://kezunlin.me/post/8932eaec/,欢迎阅读!

singleton class and usage in c++.

Guide

what singleton solve?

Singletons solve one (and only one) problem.

Resource Contention.

If you have some resource that

(1) can only have a single instance, and

(2) you need to manage that single instance,

you need a singleton.

There aren't many examples. A log file is the big one. You don't want to just abandon a single log file. You want to flush, sync and close it properly. This is an example of a single shared resource that has to be managed.

It's rare that you need a singleton. The reason they're bad is that they feel like a global and they're a fully paid up member of the GoF Design Patterns book.

When you think you need a global, you're probably making a terrible design mistake.

local static object

Actually, in C++ preferred way is local static object.

singleton pure

class Singleton
{
private:
Singleton(); public:
Singleton(Singleton const&) = delete;
Singleton& operator=(Singleton const&) = delete; static Singleton& instance()
{
static Singleton INSTANCE;
return INSTANCE;
}
};

singleton with shared_ptr

class Singleton
{
private:
Singleton(); public:
Singleton(Singleton const&) = delete;
Singleton& operator=(Singleton const&) = delete; static std::shared_ptr<Singleton> instance()
{
static std::shared_ptr<Singleton> s{new Singleton};
return s;
}
};

singleton usage

#define DISALLOW_COPY(TypeName) \
TypeName(const TypeName&) #define DISALLOW_ASSIGN(TypeName) \
TypeName& operator=(const TypeName&) #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
TypeName(const TypeName&); \
TypeName& operator=(const TypeName&) class CSingleton
{
public:
static CSingleton &GetInstance()
{
static CSingleton instance;
return instance;
}
void DoSomething()
{
printf("void CSingleton::DoSomething() called.\n");
} private:
CSingleton() {};
DISALLOW_COPY_AND_ASSIGN(CSingleton);
}; // usage
CSingleton::GetInstance().DoSomething(); // OK CSingleton& singleton = CSingleton::GetInstance(); // OK with reference
singleton.DoSomething(); CSingleton singleton = CSingleton::GetInstance(); // ERROR (copy constructor)

Example

config.h

#pragma once

class Config
{
public:
static Config& GetInstance(std::string filename="./config.ini");
~Config(); private:
Config(std::string filename);
Config(const Config& ref) {}
Config& operator =(const Config& ref) { return *this; }
};

config.cpp

#include "Config.h"

/*
static config instance will only be created once by calling Config::Config,
when program exit,static variable will be destoryed by calling Config::~Config.
*/ Config& Config::GetInstance(std::string filename)
{
static Config instance(filename);
return instance;
} Config::Config(std::string filename)
{
std::cout << "[LOG] Config::Config count= "<<count << std::endl;
// load config from filename
// ...
} Config::~Config()
{
std::cout << "[LOG] Config::~Config count= " << count << std::endl;
}

mysqldb.cpp

void MysqlDb::load_config(std::string filename)
{
this->mysql_connection = Config::GetInstance(filename).MYSQL_CONNECTION;
this->mysql_username = Config::GetInstance(filename).MYSQL_USERNAME;
this->mysql_password = Config::GetInstance(filename).MYSQL_PASSWORD;
this->mysql_database = Config::GetInstance(filename).MYSQL_DATABASE;
this->max_connection_pool_size = Config::GetInstance(filename).MAX_CONNECTION_POOL_SIZE;
}

Reference

History

  • 20180122: created.

Copyright

最新文章

  1. jquery理财贷款计算器
  2. 2 column数据构成主键的表转化为1 column为主键的表
  3. springMVC之web.xml配置
  4. 转 如何理解 重要性采样(importance sampling)
  5. Enjoy Android
  6. JAVA入门第一季(mooc-笔记)
  7. 带着萌新看springboot源码02
  8. 使用ghost装完系统后出现“引用了一个不可用的位置”
  9. 数据统计 任务的一点感想 , sql 使用中的坑。
  10. Python&#160;Elasticsearch批量操作客户端
  11. [Codeforces741D]Arpa&#39;s letter-marked tree and Mehrdad&#39;s Dokhtar-kosh paths——dsu on tree
  12. Nginx性能调优
  13. 基础选择器,长度与颜色,标签display,嵌套关系,盒模型,盒模型布局
  14. np.clip截取函数
  15. KTAG K-TAG ECU Programming Tool
  16. 安卓程序代写 网上程序代写[原]BluetoothClass详解
  17. PAT乙级1091-1095
  18. Java并发编程(六)原子性与易变性
  19. 学习Java的必要知识点记录
  20. 分享一个settings.xml

热门文章

  1. python函数与异常处理
  2. 去除img下方的空白(vertical-align:middle)——原理
  3. JUC - ReentrantLock 的基本用法 以及 lock()、tryLock()、lockInterruptibly()的区别
  4. 文本查重算法SimHash
  5. sublime设置 reindent 快捷键
  6. Redux的核心概念,实现代码与应用示例
  7. LeetCode刷题笔记(2)HashMap相关应用
  8. ansible之roles
  9. 第二十九章 System V共享内存
  10. Springboot中使用自定义参数注解获取 token 中用户数据