设计模式之单例模式实现(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中使用RNCryptor对资源文件加密(先加密后拖进项目中)
概述:IPA 在发布时,业务相关的敏感资源文件以明文的形式存储,由于没有加密保护,这些文件在应用发布后 可能被其他人获取,并结合其他漏洞和手段产生真实攻击.所以我们要 1.在设计.开发阶段,集合业务确 ...
- LeetCode 455. Assign Cookies (C++)
题目: Assume you are an awesome parent and want to give your children some cookies. But, you should gi ...
- MathExam6378
我的第一个程序 一.预估与实际 PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时(分钟) Planning 计划 15 10 • Estima ...
- iOS学习资源搜集
swift 2.0 新的开始 iOS7初学者入门 斯坦福大学公开课:iOS 8开发 苹果官方开发 中文 iOS/Mac 开发博客列表 git
- Java文件写入时是否覆盖
这个是和服务器读数据结合着来的,是向服务器文件写数据,这就碰到了是否覆盖以前写的数据的问题,看FileWriter();的参数后面的参数名叫append,用词典查是附加的意思,灵机一动,改成false ...
- DFS--障碍在指定时间会消失
哈利被困在了一个魔法花园里.魔法花园是一个 N*M 的矩形,在其中有着许多植物, 这些植物会在时刻 K 的倍数消失. 哈利每单位时间都会选择上.下.左.右四 个方向的其中一个进行移动. #includ ...
- 在数组中找出两数之和为10的所有组合(JAVA)
/*利用冒泡排序实现*/ import java.util.Scanner;public class Paixun { public static void main(String[] args) { ...
- 使用JProfiler做性能分析过程
供自己记录一下,也分享给大家使用JProfiler的过程(感谢教我使用这个工具的大佬),整个博客比较粗糙,希望对大家有帮助 1.首先安装好JProfiler,打开eclipse,右键你所要分析的项目, ...
- 项目报错“JavaServer Faces 2.2 can not be installed : One or more constraints”等一系列问题
在做springmvc+maven项目时,经常遇到如下错误: 解决办法(这里以jdk1.8,web3.0为例): 一:保证build path的jre版本 remove掉旧版本的,add新版本 二:保 ...
- 软工网络15团队作业8——Beta阶段敏捷冲刺(Day3)
提供当天站立式会议照片一张 每个人的工作 1.讨论项目每个成员的昨天进展 赵铭: 还是在学习知晓云数据库怎么用 吴慧婷:这两天进一步进行界面设计,暂时完成了背单词界面的初步设计. 陈敏: 完成了背单词 ...