剑指Offer面试题:1.实现单例模式
一 题目:实现单例模式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.实现单例模式的更多相关文章
- 剑指Offer:面试题15——链表中倒数第k个结点(java实现)
问题描述 输入一个链表,输出该链表中倒数第k个结点.(尾结点是倒数第一个) 结点定义如下: public class ListNode { int val; ListNode next = null; ...
- 剑指offer面试题3 二维数组中的查找(c)
剑指offer面试题三:
- 剑指Offer——笔试题+知识点总结
剑指Offer--笔试题+知识点总结 情景回顾 时间:2016.9.23 12:00-14:00 19:00-21:00 地点:山东省网络环境智能计算技术重点实验室 事件:笔试 注意事项:要有大局观, ...
- C++版 - 剑指offer之面试题37:两个链表的第一个公共结点[LeetCode 160] 解题报告
剑指offer之面试题37 两个链表的第一个公共结点 提交网址: http://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?t ...
- C++版 - 剑指offer 面试题23:从上往下打印二叉树(二叉树的层次遍历BFS) 题解
剑指offer 面试题23:从上往下打印二叉树 参与人数:4853 时间限制:1秒 空间限制:32768K 提交网址: http://www.nowcoder.com/practice/7fe2 ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- Leetcode - 剑指offer 面试题29:数组中出现次数超过一半的数字及其变形(腾讯2015秋招 编程题4)
剑指offer 面试题29:数组中出现次数超过一半的数字 提交网址: http://www.nowcoder.com/practice/e8a1b01a2df14cb2b228b30ee6a92163 ...
- C++版 - 剑指Offer 面试题39:二叉树的深度(高度)(二叉树深度优先遍历dfs的应用) 题解
剑指Offer 面试题39:二叉树的深度(高度) 题目:输入一棵二叉树的根结点,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度.例如:输入二叉树 ...
- C++版 - 剑指offer 面试题24:二叉搜索树BST的后序遍历序列(的判断) 题解
剑指offer 面试题24:二叉搜索树的后序遍历序列(的判断) 题目:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则返回true.否则返回false.假设输入的数组的任意两个 ...
- C++版 - 剑指Offer 面试题45:圆圈中最后剩下的数字(约瑟夫环问题,ZOJ 1088:System Overload类似)题解
剑指Offer 面试题45:圆圈中最后剩下的数字(约瑟夫环问题) 原书题目:0, 1, - , n-1 这n个数字排成一个圈圈,从数字0开始每次从圆圏里删除第m个数字.求出这个圈圈里剩下的最后一个数字 ...
随机推荐
- 做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)?
import random import string def GenKey(length): chars = string.ascii_letters + string.digits return ...
- setState详解
我们都知道,React通过this.state来访问state,通过this.setState()方法来更新state.当this.setState()方法被调用的时候,React会重新调用rende ...
- Logback Pattern 日志格式配置
Logback日志配置示例 <appender name="SYSLOG" class="ch.qos.logback.classic.net.SyslogAppe ...
- mysql——主键自动增长&唯一索引
首先说一下主键和唯一索引的区别 主键:一个数据库的一张表有且仅有一个主键,而且主键不能重复 唯一索引:一个数据库的一张表上唯一索引可以有多个,只是所在唯一索引上的值不能重复,这一点和主键一样 下面我们 ...
- AOP切面用于系统日志
import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.*; import org.springframework. ...
- Eclipse2013_文件编码设置
1. Eclipse --> Window --> Preferences --> General --> Workspace 其中的 "Text file enco ...
- LeetCode第[69]题(Java):Sqrt(x)
题目:求平方根 难度:Easy 题目内容: Compute and return the square root of x, where x is guaranteed to be a non-neg ...
- 使用mybatis报错constructor found in com.atguigu.mybatis.bean.Department matching [java.lang.Integer, java.lang.String]
报错constructor found in com.atguigu.mybatis.bean.Department matching [java.lang.Integer, java.lang.St ...
- Dib to Bitmap doesn't work in WPF
一.Dib to Bitmap doesn't work in WPF 代码如下: protected byte[] BitmapFromDIB(IntPtr pDIB, IntPtr pPix) { ...
- Confluence 6 配置系统属性
在这个页面中描述 Confluence 启动时如何设置 Java 属性和其他选项. 请查看 How to fix out of memory errors by increasing availabl ...