题目:单例
2015-08-19 19:03
363 查看
单例 是最为最常见的设计模式之一。对于任何时刻,如果某个类只存在且最多存在一个具体的实例,那么我们称这种设计模式为单例。例如,对于 class Mouse (不是动物的mouse哦),我们应将其设计为 singleton 模式。
你的任务是设计一个
您在真实的面试中是否遇到过这个题?
Yes
哪家公司问你的这个题?
Airbnb
Alibaba
Amazon Apple
Baidu Bloomberg
Cisco Dropbox
Ebay Facebook
Google Hulu
Intel Linkedin
Microsoft NetEase
Nvidia Oracle
Pinterest Snapchat
Tencent Twitter
Uber Xiaomi
Yahoo Yelp
Zenefits
感谢您的反馈
样例
在 Java 中:
a 应等于 b.
挑战
如果并发的调用 getInstance,你的程序也可以正确的执行么?
标签 Expand
LintCode 版权所有
面向对象设计
相关题目 Expand
2
(oo-design)
中等 赋值运算符重载 20 %
饿汉式AC代码:
class Solution {
/**
* @return: The same instance of this class every time
*/
private static Solution solution ;
public static Solution getInstance() {
// write your code here
if(solution==null){
solution = new Solution();
}
return solution;
}
};
懒汉式AC代码
class Solution {
/**
* @return: The same instance of this class every time
*/
private static Solution solution = new Solution();
public static Solution getInstance() {
// write your code here
return solution;
}
};
另外参考别人代码所写的,多线程 方式
class Solution {
/**
* @return: The same instance of this class every time
*/
private static Solution solution ;
public static Solution getInstance() {
// write your code here
if(null==solution){
synchronized(Solution.class){
if(null==solution){
Solution tmp = solution;
if(null==tmp){
synchronized (Solution.class) {
tmp = new Solution();
}
}
solution = tmp;
}
}
}
return solution;
}
};
你的任务是设计一个
getInstance方法,对于给定的类,每次调用
getInstance时,都可得到同一个实例。
您在真实的面试中是否遇到过这个题?
Yes
哪家公司问你的这个题?
Airbnb
Alibaba
Amazon Apple
Baidu Bloomberg
Cisco Dropbox
Ebay Facebook
Google Hulu
Intel Linkedin
Microsoft NetEase
Nvidia Oracle
Pinterest Snapchat
Tencent Twitter
Uber Xiaomi
Yahoo Yelp
Zenefits
感谢您的反馈
样例
在 Java 中:
A a = A.getInstance(); A b = A.getInstance();
a 应等于 b.
挑战
如果并发的调用 getInstance,你的程序也可以正确的执行么?
标签 Expand
LintCode 版权所有
面向对象设计
相关题目 Expand
2
(oo-design)
中等 赋值运算符重载 20 %
饿汉式AC代码:
class Solution {
/**
* @return: The same instance of this class every time
*/
private static Solution solution ;
public static Solution getInstance() {
// write your code here
if(solution==null){
solution = new Solution();
}
return solution;
}
};
懒汉式AC代码
class Solution {
/**
* @return: The same instance of this class every time
*/
private static Solution solution = new Solution();
public static Solution getInstance() {
// write your code here
return solution;
}
};
另外参考别人代码所写的,多线程 方式
class Solution {
/**
* @return: The same instance of this class every time
*/
private static Solution solution ;
public static Solution getInstance() {
// write your code here
if(null==solution){
synchronized(Solution.class){
if(null==solution){
Solution tmp = solution;
if(null==tmp){
synchronized (Solution.class) {
tmp = new Solution();
}
}
solution = tmp;
}
}
}
return solution;
}
};
相关文章推荐
- git入门指南
- Canvas——JavaScript高级程序设计笔记(12)
- EF里的默认映射以及如何使用Data Annotations和Fluent API配置数据库的映射
- 题目:加一
- 静态函数调用非静态函数的小例子
- ExtJS学习之MessageBox
- test
- javascript对闭包的深入理解
- javascript对闭包的深入理解
- Obtain Play's injected object
- Linux 开发闲杂知识点速查
- Javascript闭包
- 解决虚拟机linux端mysql数据库无法远程访问
- mac下安装nginx+uwsgi+webpy出错解决
- 题目:判断数独是否合法
- Unique Paths
- Qt窗口的标题栏自绘
- [LeetCode] Ugly Number
- ios之 微信扫码登录网页实现原理
- udp tcp fax发送