设计模式之单例模式实现(C++)
#ifndef SINGLETON_H
#define SINGLETON_H #include <cassert>
#include <memory>
#include <mutex> #define DECLARE_SINGLETON_CLASS(T) friend Singleton<T> template <typename T>
class Singleton
{
public:
using PT = std::shared_ptr<T>; Singleton() = delete;
~Singleton() = delete; public:
template <typename... Args>
static PT getInstance(Args&&... args)
{
// std::call_once(m_flag, create, std::forward<Args>(args)...);
// error: no matching function for call call_once... <unresolved overloaded function type>
// couldn't deduce template parameter '_Callable'
// why ?
std::call_once(m_flag, [&]() {
create(std::forward<Args>(args)...);
});
assert(m_instance);
return m_instance;
} private:
template <typename... Args>
static void create(Args&&... args)
{
m_instance = std::shared_ptr<T>{new T(std::forward<Args>(args)...), destroy};
} static void destroy(T* t)
{
delete t;
} private:
static std::once_flag m_flag;
static PT m_instance;
}; template <typename T>
std::once_flag Singleton<T>::m_flag; template <typename T>
typename Singleton<T>::PT Singleton<T>::m_instance{}; #endif // SINGLETON_H
#include "Singleton.h"
#include <iostream>
#include <string> using namespace std; #define print() cout << "[" << __func__ << ":" << __LINE__ << "]"
#define print_position() print() << endl
#define print_class() print() << " " << typeid(*this).name() << " " #if 1 class Bundle
{
public:
Bundle()
{
print_class() << "construct" << endl;
} ~Bundle()
{
print_class() << "destruct" << endl;
} Bundle(const Bundle& )
{
print_class() << "copy construct" << endl;
} Bundle(Bundle&&)
{
print_class() << "move construct" << endl;
} Bundle& operator=(Bundle&)
{
print_class() << "copy operator assign" << endl;
return *this;
} Bundle& operator=(Bundle&&)
{
print_class() << "move operator assign" << endl;
return *this;
}
}; class SingleInstanceKlass
{
DECLARE_SINGLETON_CLASS(SingleInstanceKlass); private:
SingleInstanceKlass()
{
print_class() << "default construct" << endl;
}
SingleInstanceKlass(int)
{
print_class() << "int construct" << endl;
}
SingleInstanceKlass(const string&)
{
print_class() << "string construct" << endl;
}
SingleInstanceKlass(const Bundle&)
{
print_class() << "Bundle construct" << endl;
}
~SingleInstanceKlass()
{
print_class() << "destruct" << endl;
} public:
void run()
{
print_position();
}
}; template <typename... Args>
void unused(Args...)
{
} void onExit()
{
print_position();
} int main(int argc, char *argv[])
{
unused(argc, argv); atexit(onExit); print_position();
{
Singleton<SingleInstanceKlass>::getInstance(Bundle{});
Singleton<SingleInstanceKlass>::getInstance(.);
Singleton<SingleInstanceKlass>::getInstance();
Singleton<SingleInstanceKlass>::getInstance("")->run();
}
print_position(); return ;
} #endif
设计模式之单例模式实现(C++)的更多相关文章
- 设计模式之单例模式(Singleton)
设计模式之单例模式(Singleton) 设计模式是前辈的一些经验总结之后的精髓,学习设计模式可以针对不同的问题给出更加优雅的解答 单例模式可分为俩种:懒汉模式和饿汉模式.俩种模式分别有不同的优势和缺 ...
- GJM : C#设计模式(1)——单例模式
感谢您的阅读.喜欢的.有用的就请大哥大嫂们高抬贵手"推荐一下"吧!你的精神支持是博主强大的写作动力以及转载收藏动力.欢迎转载! 版权声明:本文原创发表于 [请点击连接前往] ,未经 ...
- java设计模式之单例模式(几种写法及比较)
概念: Java中单例模式是一种常见的设计模式,单例模式的写法有好几种,这里主要介绍三种:懒汉式单例.饿汉式单例.登记式单例. 单例模式有以下特点: 1.单例类只能有一个实例. 2.单例类必须自己创建 ...
- 每天一个设计模式-4 单例模式(Singleton)
每天一个设计模式-4 单例模式(Singleton) 1.实际生活的例子 有一天,你的自行车的某个螺丝钉松了,修车铺离你家比较远,而附近的五金店有卖扳手:因此,你决定去五金店买一个扳手,自己把螺丝钉固 ...
- 设计模式之单例模式的简单demo
/* * 设计模式之单例模式的简单demo */ class Single { /* * 创建一个本类对象. * 和get/set方法思想一样,类不能直接调用对象 * 所以用private限制权限 * ...
- 设计模式之单例模式——Singleton
设计模式之单例模式--Singleton 设计意图: 保证类仅有一个实例,并且可以供应用程序全局使用.为了保证这一点,就需要这个类自己创建自己的对象,并且对外有 ...
- 10月27日PHP加载类、设计模式(单例模式和工厂模式)、面向对象的六大原则
加载类可以使用include.require.require_once三种中的任意一种,每个关键字都有两种方法,但是这种方法的缺点是需要加载多少个php文件,就要写多少个加载类的方法.一般也就需要加载 ...
- java 23 - 2 设计模式之单例模式
单例模式:保证类在内存中只有一个对象. 如何保证类在内存中只有一个对象呢? A:把构造方法私有 B:在成员位置自己创建一个对象 C:通过一个公共的方法提供访问 单例模式之饿汉式: (一进来就造对 ...
- [转]JAVA设计模式之单例模式
原文地址:http://blog.csdn.net/jason0539/article/details/23297037 概念: java中单例模式是一种常见的设计模式,单例模式的写法有好几种,这里主 ...
- python_way,day8 面向对象【多态、成员--字段 方法 属性、成员修饰符、特殊成员、异常处理、设计模式之单例模式、模块:isinstance、issubclass】
python_way day8 一.面向对象三大特性: 多态 二.面向对象中的成员 字段.方法属性 三.成员修饰符 四.特殊成员 __init__.__doc__.__call__.__setitem ...
随机推荐
- iOS 开发中,关于xxx.xcodeproj 文件冲突的解决方案 (以后谁不会了,直接将连接给他)
iOS 开发中,关于xxx.xcodeproj 文件冲突的解决方案 (一有冲突要手把手教一遍,太麻烦了,现在总结下,以后谁不会了,连接直接发他). 关于xxx.xcodeproj 文件冲突的话,是比较 ...
- jQuery全屏滚动插件fullPage使用
1. 引入jquery.js和jquery.fullPage.min.js <script src="jquery.min.js"></script> &l ...
- IIs8 svc
IIS8中添加WCF支持几种方法小结[图文] 方法一 最近在做Silverlight,Windows Phone应用移植到Windows 8平台,在IIS8中测试一些传统WCF服务应用,发现IIS8不 ...
- Java 面试-- 1
JAVA面试精选[Java基础第一部分] 这个系列面试题主要目的是帮助你拿轻松到offer,同时还能开个好价钱.只要能够搞明白这个系列的绝大多数题目,在面试过程中,你就能轻轻松松的把面试官给忽悠了 ...
- ns-3 可视化模拟 (一) PyViz
PyViz 个人觉得这个的使用简单. (1)首先安装 这是ubuntu下的 sudo apt-get install python-dev python-pygraphviz python-kiwi ...
- elasticsearch文档-字段的mapping
mapping == Mapping是指定义如何将document映射到搜索引擎的过程,比如一个字段是否可以查询以及如何分词等,一个索引可以存储含有不同"mapping types" ...
- PAT 1068 万绿丛中一点红
https://pintia.cn/problem-sets/994805260223102976/problems/994805265579229184 对于计算机而言,颜色不过是像素点对应的一个 ...
- VMWare之——宿主机与虚拟机互相ping通,宿主机ping通另一台机器的虚拟机
版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请注明出处:http://blog.csdn.NET/l1028386804/article/details/52267554 今天给大家带来 ...
- python基础(四)文件操作和集合
一.文件操作 对文件的操作分三步: 1.打开文件获取文件的句柄,句柄就理解为这个文件 2.通过文件句柄操作文件 3.关闭文件. 1.文件基本操作: f = open('file.txt','r') # ...
- mysql 函数示例(转)
MySQL函数大全及用法示例 1.字符串函数ascii(str) 返回字符串str的第一个字符的ascii值(str是空串时返回0) mysql> select ascii('2'); ...