LintCode 204: Singleton
LintCode 204: Singleton
题目描述
单例是最为最常见的设计模式之一。对于任何时刻,如果某个类只存在且最多存在一个具体的实例,那么我们称这种设计模式为单例。例如,对于class Mouse(不是动物的mouse哦),我们应将其设计为singleton模式。
你的任务是设计一个getInstance方法,对于给定的类,每次调用getInstance时,都可得到同一个实例。
样例
在Java中:
A a = A.getInstance();
A b = A.getInstance();
a应等于b.
Thu Mar 2 2017
思路
这题如果用C++写的话,需要注意的就是静态成员在使用前需要初始化,否则就会报错。
代码
// 单例
#include <iostream>
using namespace std;
class Solution {
public:
/**
* @return: The same instance of this class every time
*/
static Solution* _this;
Solution()
{
if (_this == NULL) _this = this;
}
static Solution* getInstance()
{
return _this;
}
};
Solution* Solution::_this = NULL;
int main(int argc, char** argv)
{
Solution a;
Solution b;
cout << a.getInstance() << endl;
cout << b.getInstance() << endl;
return 0;
}
LintCode 204: Singleton的更多相关文章
- lintcode:Singleton 单例
题目: 单例 单例是最为最常见的设计模式之一.对于任何时刻,如果某个类只存在且最多存在一个具体的实例,那么我们称这种设计模式为单例.例如,对于 class Mouse (不是动物的mouse哦),我们 ...
- 204. Singleton
Description Singleton is a most widely used design pattern. If a class has and only has one instance ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- LintCode Singleton
Singleton 3 大要素: 1.有private static的句柄(成员变量即field) 2. constructor 必须为private 3.有public static的getInst ...
- Lintcode: Singleton && Summary: Synchronization and OOD
Singleton is a most widely used design pattern. If a class has and only has one instance at every mo ...
- lintcode 中等题:Singleton number II 落单的数 II
题目 落单的数 II 给出3*n + 1 个的数字,除其中一个数字之外其他每个数字均出现三次,找到这个数字. 样例 给出 [1,1,2,3,3,3,2,2,4,1] ,返回 4 挑战 一次遍历,常数级 ...
- 23种设计模式--单例模式-Singleton
一.单例模式的介绍 单例模式简单说就是掌握系统的至高点,在程序中只实例化一次,这样就是单例模式,在系统比如说你是该系统的登录的第多少人,还有数据库的连接池等地方会使用,单例模式是最简单,最常用的模式之 ...
- 设计模式之单例模式(Singleton)
设计模式之单例模式(Singleton) 设计模式是前辈的一些经验总结之后的精髓,学习设计模式可以针对不同的问题给出更加优雅的解答 单例模式可分为俩种:懒汉模式和饿汉模式.俩种模式分别有不同的优势和缺 ...
- PHP设计模式(四)单例模式(Singleton For PHP)
今天讲单例设计模式,这种设计模式和工厂模式一样,用的非常非常多,同时单例模式比较容易的一种设计模式. 一.什么是单例设计模式 单例模式,也叫单子模式,是一种常用的软件设计模式.在应用这个模式时,单例对 ...
随机推荐
- 初期测评 A 排序
https://vjudge.net/contest/240302#problem/A 输入一行数字,如果我们把这行数字中的‘5’都看成空格,那么就得到一行用空格分割的若干非负整数(可能有些整数以‘0 ...
- jquery获取父元素或父节点的方法
jquery获取父元素方法比较多,比如parent(),parents(),closest()这些都能帮你实现查找父元素或节点,下面我们来一一讲解: 先举个例子: <ul class=" ...
- [转帖]紧急预警:Globelmposter3.0变种来袭,多行业中招
紧急预警:Globelmposter3.0变种来袭,多行业中招 https://www.csdn.net/article/a/2018-09-04/15959658 CSDN 转载深信服... ...
- [转帖] dd 命令图解
dd命令-->dd是disk dump的缩写,指定大小的块拷贝一个文件,同时进行指定的转换,起到一个初始化磁盘的作用 https://blog.csdn.net/jerry_1126/arti ...
- IE实现userData 永久存储
注意:只支持IE5,及其以上的浏览器 //需要使用 if 条件注释 <!DOCTYPE html> <html> <head> <meta charset ...
- 如何杀掉Monkey测试
1.adb shell 2.ps | grep monkey 3.kill pid 然后可以看到手机进程中的monkey进程被杀死了,再执行ps | grep monkey,就会发现没有monkey进 ...
- HDU4791_Alice's Print Service
全场最水题. 保留打印a[i]份分别需要的钱,从后往前扫一遍,保证取得最优解. 查找的时候,二分同时判断最小值即可. 注意初值的设定应该设定为long long 的无穷大. #include < ...
- SPOJ Triple Sums(FFT+容斥原理)
# include <cstdio> # include <cstring> # include <cstdlib> # include <iostream& ...
- 每日一问(常用的集合接口和类有哪些【二】)—ArrayList类和数组之间的转换
ArrayList的实质是数组,但是在类的实例中所存储的数组是无法访问的,因此实际上是无法直接作为数组使用,那么如何将这两者进行转化呢? Collection接口定义了toArray的方法,可将实现该 ...
- BZOJ5305 HAOI2018苹果树(概率期望+动态规划)
每种父亲编号小于儿子编号的有标号二叉树的出现概率是相同的,问题相当于求所有n个点的此种树的所有结点两两距离之和. 设f[n]为答案,g[n]为所有此种树所有结点的深度之和,h[n]为此种树的个数. 枚 ...