一 题目:实现单例模式Singleton

题目:设计一个类,我们只能生产该类的一个实例。

  只能生成一个实例的类是实现了Singleton(单例)模式的类型。由于设计模式在面向对象程序设计中起着举足轻重的作用,在面试过程中很多公司都喜欢问一些与设计模式相关的问题。在常用的模式中,Singleton是唯一一个能够用短短几十行代码完整实现的模式。因此,写一个Singleton的类型是一个很常见的面试题。

二:单例模式的几种实现方法

(1)单线程单例模式

// 单例模式
class Singleton
{
private:
Singleton(){}
~Singleton(){delete m_pSingleton;}; public:
static Singleton* Instance(); private:
static Singleton*m_pSingleton;
}; Singleton* Singleton::m_pSingleton = NULL;
Singleton* Singleton::Instance()
{
if (!m_pSingleton)
{
m_pSingleton = new Singleton();
} return m_pSingleton;
}

  这是一个很棒的实现,简单易懂。但这是一个完美的实现吗?不!该方法是线程不安全的,考虑两个线程同时首次调用instance方法且同时检测到p是NULL值,则两个线程会同时构造一个实例给p,这是严重的错误!同时,这也不是单例的唯一实现!

(2)懒汉单例模式

  a. 使用互斥对象实现懒汉模式

// 单例模式
class Singleton
{
private:
Singleton(){};
~Singleton(){
delete m_pSingleton;
DeleteCriticalSection(&cs);} public:
static Singleton * Instance(); private:
static Singleton *m_pSingleton;
static CRITICAL_SECTION cs;
static bool bInitCriticalSection;
}; CRITICAL_SECTION Singleton::cs = {};
bool Singleton::bInitCriticalSection = false;
Singleton * Singleton::m_pSingleton = NULL; Singleton * Singleton::Instance()
{
if (!bInitCriticalSection)
{
InitializeCriticalSection(&cs);
bInitCriticalSection = true;
}
if (!m_pSingleton)
{
EnterCriticalSection(&cs);
m_pSingleton = new Singleton();
LeaveCriticalSection(&cs);
} return m_pSingleton;
}

  b. 使用内部静态变量实现懒汉模式

  此方法也很容易实现,在instance函数里定义一个静态的实例,也可以保证拥有唯一实例,在返回时只需要返回其指针就可以了。推荐这种实现方法,真得非常简单

// 单例模式
class Singleton
{
private:
Singleton(){}
~Singleton(){DeleteCriticalSection(&cs);} public:
static Singleton * Instance(); private:
static CRITICAL_SECTION cs;
static bool bInitCriticalSection;
}; CRITICAL_SECTION Singleton::cs = {};
bool Singleton::bInitCriticalSection = false;
Singleton * Singleton::Instance()
{
if (!bInitCriticalSection)
{
InitializeCriticalSection(&cs);
bInitCriticalSection = false;
}
EnterCriticalSection(&cs);
static Singleton m_Singleton;
LeaveCriticalSection(&cs); return &m_Singleton;
}

(3)饿汉单例模式

  饿汉实现本来就是线程安全的,不用加锁。为啥?自己想!

// 单例模式
class Singleton
{
private:
Singleton(){};
~Singleton(){} public:
static Singleton * Instance(); private:
static Singleton *m_pSingleton;
}; Singleton * Singleton::m_pSingleton = new Singleton();
Singleton * Singleton::Instance()
{
return m_pSingleton;
}

剑指Offer面试题:1.实现单例模式的更多相关文章

  1. 剑指Offer:面试题15——链表中倒数第k个结点(java实现)

    问题描述 输入一个链表,输出该链表中倒数第k个结点.(尾结点是倒数第一个) 结点定义如下: public class ListNode { int val; ListNode next = null; ...

  2. 剑指offer面试题3 二维数组中的查找(c)

    剑指offer面试题三:

  3. 剑指Offer——笔试题+知识点总结

    剑指Offer--笔试题+知识点总结 情景回顾 时间:2016.9.23 12:00-14:00 19:00-21:00 地点:山东省网络环境智能计算技术重点实验室 事件:笔试 注意事项:要有大局观, ...

  4. C++版 - 剑指offer之面试题37:两个链表的第一个公共结点[LeetCode 160] 解题报告

    剑指offer之面试题37 两个链表的第一个公共结点 提交网址: http://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?t ...

  5. C++版 - 剑指offer 面试题23:从上往下打印二叉树(二叉树的层次遍历BFS) 题解

    剑指offer  面试题23:从上往下打印二叉树 参与人数:4853  时间限制:1秒  空间限制:32768K 提交网址: http://www.nowcoder.com/practice/7fe2 ...

  6. C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解

    剑指offer 面试题39:判断平衡二叉树 提交网址:  http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...

  7. Leetcode - 剑指offer 面试题29:数组中出现次数超过一半的数字及其变形(腾讯2015秋招 编程题4)

    剑指offer 面试题29:数组中出现次数超过一半的数字 提交网址: http://www.nowcoder.com/practice/e8a1b01a2df14cb2b228b30ee6a92163 ...

  8. C++版 - 剑指Offer 面试题39:二叉树的深度(高度)(二叉树深度优先遍历dfs的应用) 题解

    剑指Offer 面试题39:二叉树的深度(高度) 题目:输入一棵二叉树的根结点,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度.例如:输入二叉树 ...

  9. C++版 - 剑指offer 面试题24:二叉搜索树BST的后序遍历序列(的判断) 题解

    剑指offer 面试题24:二叉搜索树的后序遍历序列(的判断) 题目:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则返回true.否则返回false.假设输入的数组的任意两个 ...

  10. C++版 - 剑指Offer 面试题45:圆圈中最后剩下的数字(约瑟夫环问题,ZOJ 1088:System Overload类似)题解

    剑指Offer 面试题45:圆圈中最后剩下的数字(约瑟夫环问题) 原书题目:0, 1, - , n-1 这n个数字排成一个圈圈,从数字0开始每次从圆圏里删除第m个数字.求出这个圈圈里剩下的最后一个数字 ...

随机推荐

  1. iOS 自定义滑动切换TabbarItem 觉得设计丑也要做出来的UI效果。。。

    UI丑却要继续做的感言: 对UI不满意的时候,就会觉得丑爆了,时间长了,却丑习惯了. 论前一阵子Tabbar 多丑,丑得最后不要tabbar了...但是自定义tabbar 和遇到的问题解决的过程可以记 ...

  2. centos7 firewall开放查看关闭端口

    查看所有打开的端口: firewall-cmd --zone=public --list-ports 添加 firewall-cmd --zone=public --add-port=80/tcp - ...

  3. hadoop17---RPC和Socket的区别

    RPC是在Socket的基础上实现的,它比socket需要更多的网络和系统资源.RPC(Remote Procedure Call,远程过程调用)是建立在Socket之上的,出于一种类比的愿望,在一台 ...

  4. dirname和shell常用命令

    $ cd `dirname $0` 和PWD%}  显示当前目录名称${#var}             替换为变量字符个数特殊变量$ 当前SHELL的PID? 前一个命令的退出状态! 后台执行的上 ...

  5. 字符数组(char)和字符串(string)的转换

    #include<iostream>#include<string>using namespace std;void main(){ string LyuS = "W ...

  6. 基于SSM的单点登陆05

    springmvc.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=" ...

  7. github Git-fork-别人的项目后更新代码的方法

     用github还处于菜的阶段,遇到问题简单记录.   举个例子,需要 fork 这个项目 https://github.com/tarobjtu/WebFundamentals.git 点击 for ...

  8. Win32 API编程:网络编程在设置WSAAsyncSelect模型后connect的返回值问题

    通过WSAAsyncSelect()可以设置非阻塞异步套接字 ::WSAAsyncSelect(s, hDlg, WM_SOCKET, FD_CONNECT | FD_CLOSE | FD_WRITE ...

  9. SpringBoot 事务隔离性和传播性

    propergation 传播性 Spring中七种Propagation类的事务属性详解:  REQUIRED:支持当前事务,如果当前没有事务,就新建一个事务.这是最常见的选择.  SUPPORTS ...

  10. AI理论学习笔记(一):深度学习的前世今生

    AI理论学习笔记(一):深度学习的前世今生 大家还记得以深度学习技术为基础的电脑程序AlphaGo吗?这是人类历史中在某种意义的第一次机器打败人类的例子,其最大的魅力就是深度学习(Deep Learn ...