c++中实现单例模式singleton class
本文首发于个人博客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
- [c-singleton-vs-global-static-object](- https://stackoverflow.com/questions/1463707/c-singleton-vs-global-static-object)
- c-singleton-design-pattern
-cpp-singleton-memory-retrieve
History
- 20180122: created.
Copyright
- Post author: kezunlin
- Post link: https://kezunlin.me/post/8932eaec/
- Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.
c++中实现单例模式singleton class的更多相关文章
- Qt中实现单例模式(SingleTon),大约有3种办法
Qt中实现单例模式(SingleTon) 单例模式分为“饥汉”和“饿汉”两种版本,也正是线程安全问题使得原本简单的单例模式变得复杂.由于单例模式很常用,Boost库中有强大的泛型单例实现,我也利用Qt ...
- Java中的单例模式(Singleton Pattern in Java)
Introduction 对于系统中的某个类来说,只有一个实例是很重要的,比如只有一个timer和ID Producer.又比如在服务器程序中,配置信息保留在一个文件中,这些配置信息由一个单例对象统一 ...
- 设计模式之单例模式——Singleton
设计模式之单例模式--Singleton 设计意图: 保证类仅有一个实例,并且可以供应用程序全局使用.为了保证这一点,就需要这个类自己创建自己的对象,并且对外有 ...
- 【白话设计模式四】单例模式(Singleton)
转自:https://my.oschina.net/xianggao/blog/616385 0 系列目录 白话设计模式 工厂模式 单例模式 [白话设计模式一]简单工厂模式(Simple Factor ...
- Objective-C中的单例模式
单例模式算是设计模式中比较简单的一种吧,设计模式不是只针对某种编程语言,在C++, Java, PHP等其他OOP语言也有设计模式,笔者初接触设计模式是通过<漫谈设计模式>了解 ...
- C# 中实现单例模式
文章目录 简介 不安全线程的单例模式 简单安全线程带锁 双重检查 - 带锁 安全初始化 安全并且懒汉式静态初始化 带泛型的懒汉式单例 异常 提高效率 总结 简介 单例模式是软件工程中广为人知的设计模式 ...
- 转:C++中的单例模式
C++中的单例模式 单例模式也称为单件模式.单子模式,可能是使用最广泛的设计模式.其意图是保证一个类仅有一个实例,并提供一个访问它的全局访问点,该实例被所有程序模块共享.有很多地方需要这样的功能模块, ...
- ooad单例模式-Singleton
单例模式Singleton 主要作用是保证在Java应用程序中,一个类Class只有一个实例存在. 比如建立目录 ...
- 浅谈iOS中的单例模式
iOS中的单例模式 就我本身理解而言,我认为的单例:单例在整个工程中,就相当于一个全局变量,就是不论在哪里需要用到这个类的实例变量,都可以通过单例方法来取得,而且一旦你创建了一个单例类,不论你 ...
随机推荐
- 基础安全术语科普(三)——RAT
什么是RAT? RAT 即 Remote Access Tools (远程管理工具或远程访问工具)的缩写.通俗点说就是木马病毒. RAT 分为两部分——客户端 与 服务端. RAT的工作原理? 服务端 ...
- vue 请求图片方法
node的每一个文件,都是一个域,那么里面所有的变量都不允许被外界引用,除非导出.要使用外界的变量,也必须使用导入的方式来导入.import 文件路径. css可以直接使用import +文件路径导入 ...
- spring-boot-plus是易于使用,快速,高效,功能丰富,开源的spring boot 脚手架.
Everyone can develop projects independently, quickly and efficiently! spring-boot-plus是一套集成spring bo ...
- SQLite AUTOINCREMENT
只有integer字段可以设置autoincrement.int不行.integer是无符号整型.
- Ios 开发 mac cocoaPods的环境搭建
CocoaPods不多介绍,一个大家几乎都会使用的第三方库的管理框架! 本文主要介绍如何安装和使用CocoaPods,本人亲测可行. 1.Ruby环境搭建 查看下当前ruby版本: ruby -v 更 ...
- Okhttp 请求流程梳理
最近在看 Okhttp 的源码.不得不说源码设计的很巧妙,从中能学到很多.其实网上关于 Okhttp 的文章已经很多了,自己也看了很多.但是俗话说得好,好记性不如烂笔头,当你动手的时候,你会发现你在看 ...
- IE8下Extjs报缺少':'符号错误
先介绍下这个问题的由来: 上午其他项目组人员在rtx上问,求帮忙解决ie8兼容性问题. 然后快到饭点,知道这个bug肯定不是那么好解决,肯定不能耽误吃饭时间. 果断说,下午来弄. 下午3点开始去看这个 ...
- 通过 Django Pagination 实现简单分页
作者:HelloGitHub-追梦人物 文中所涉及的示例代码,已同步更新到 HelloGitHub-Team 仓库 当博客上发布的文章越来越多时,通常需要进行分页显示,以免所有的文章都堆积在一个页面, ...
- 【XSY2495】余数
Input Output Input 3 4 Output 4 HINT 原式 =n*m-n除以i向下取整 用数论分块做就可以了 #include<bits/stdc++.h> #defi ...
- Kettle6.1连接MongoDB报错
配置好mongodb连接之后,点击预览报下面的错: 报错: java.lang.NoClassDefFoundError: javax/crypto/spec/PBEKeySpec a ...