由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 关于singleton 的面试题
相关主题
singleton pattern problemOne C++ question
请问一道special singleton class的题Java 面试题
gg面试题问一个关于c++的很傻的问题,多谢
弱问:singleton要不要destructor啊?C++ Q47: protected constructor (C39)
Google电面被拒,郁闷中问个c++的问题
Can a 10-year-Java guy answer these 2 questions promptly?singleton哪种写法好?
问一个thread safe singleton的问题C++ Singleton Template - 编译通不过
C++ Singleton的实现Code for Singleton design pattern
相关话题的讨论汇总
话题: singleton话题: static话题: instance话题: private
进入JobHunting版参与讨论
1 (共1页)
w*****e
发帖数: 158
1
看大牛的面试经历中写道 “面试中问道了设计模式, 但是只谈了singleton,
singleton谈了很多,
如何实现, 有什么问题”
我对于 singleton (classA) 实现的理解:
1. private constructor
2. define static member pointer variable (pointing to an instance of the
classA)
3. define static member function getInstance() returning the pointer or
reference
4. Inside the getInstance():
* when first time class, create an instance and save the address into
static member pointer;
* return the the static member variable
还有什么需要注意的吗? 可能会遇到什么问题?
先多谢了。
a****n
发帖数: 1887
2
1. private copy constructor
2. 多线程的情况, double checked
w*****e
发帖数: 158
3
thank you asuran.
能具体说说 “多线程的情况, double checked" 吗? 多谢。
p********7
发帖数: 549
4
private assignment operator
private destructor
a****n
发帖数: 1887
5
double check 主要是为了减少多线程情况下进入critical section的次数
static Singleton* getInstance()
{
if (instance == null)
{
lock()
if (instance == null)
instance = new Singleton();
unlock()
}
return instance;
}
K******g
发帖数: 1870
6
这个从来没有见过,能举个例子吗?

【在 a****n 的大作中提到】
: double check 主要是为了减少多线程情况下进入critical section的次数
: static Singleton* getInstance()
: {
: if (instance == null)
: {
: lock()
: if (instance == null)
: instance = new Singleton();
: unlock()
: }

b**********n
发帖数: 399
7
如果两个线程同时call getInstance() 不用lock的话就会一人创建一个instance了

【在 K******g 的大作中提到】
: 这个从来没有见过,能举个例子吗?
K******g
发帖数: 1870
8
那也不用套两层啊。在if前面lock不就得了?

【在 b**********n 的大作中提到】
: 如果两个线程同时call getInstance() 不用lock的话就会一人创建一个instance了
s**x
发帖数: 7506
9
you will always lock if you put lock before if.
just a simple optimization.

【在 K******g 的大作中提到】
: 那也不用套两层啊。在if前面lock不就得了?
w****m
发帖数: 146
10
it is not effciennt

【在 K******g 的大作中提到】
: 那也不用套两层啊。在if前面lock不就得了?
相关主题
Can a 10-year-Java guy answer these 2 questions promptly?One C++ question
问一个thread safe singleton的问题Java 面试题
C++ Singleton的实现问一个关于c++的很傻的问题,多谢
进入JobHunting版参与讨论
s*****n
发帖数: 5488
11
performance.你甚至可以接着大讲这个double check实现实际上是有bug的。
放狗搜吧。

【在 K******g 的大作中提到】
: 那也不用套两层啊。在if前面lock不就得了?
s**x
发帖数: 7506
12
can u do somthing like this?
static Singleton* getInstance()
{
static Singleton s;
return &s;
}
I think i got asked this once, kind of confused. looks like should be okay.
s*****n
发帖数: 5488
13
可以写个程序验证一下,不过我认为每次都会调用default constructor construct一
个新的。

【在 s**x 的大作中提到】
: can u do somthing like this?
: static Singleton* getInstance()
: {
: static Singleton s;
: return &s;
: }
: I think i got asked this once, kind of confused. looks like should be okay.

s*****n
发帖数: 5488
14
程序证明只construct了一次。所以这个实现是对的。xs

【在 s*****n 的大作中提到】
: 可以写个程序验证一下,不过我认为每次都会调用default constructor construct一
: 个新的。

M******q
发帖数: 94
15
I think there could still be issues in multi-threaded scenarios, depending o
n how compilers implement it in assembly code. I got asked once and the int
erviewer felt very good about himself when he pointed out the issue, haha.
just google it, double check lock pattern is still not perfect.

【在 s**x 的大作中提到】
: can u do somthing like this?
: static Singleton* getInstance()
: {
: static Singleton s;
: return &s;
: }
: I think i got asked this once, kind of confused. looks like should be okay.

c********t
发帖数: 4527
16
that issue only exists in Java?
I have one interview which I argue all the interview time with the
interviewee about this issue
Guess what, I still get the job.
LOL

o
int


【在 M******q 的大作中提到】
: I think there could still be issues in multi-threaded scenarios, depending o
: n how compilers implement it in assembly code. I got asked once and the int
: erviewer felt very good about himself when he pointed out the issue, haha.
: just google it, double check lock pattern is still not perfect.

e****e
发帖数: 418
17
public class Singleton {
// Private constructor prevents instantiation from other classes
private Singleton() {}
/**
* SingletonHolder is loaded on the first execution of Singleton.
getInstance()
* or the first access to SingletonHolder.INSTANCE, not before.
*/
private static class SingletonHolder {
private static final Singleton INSTANCE = new Singleton();
}
public static Singleton getInstance() {
return SingletonHolder.INSTANCE;
}
}
Since Java dynamic loading, wh
M******q
发帖数: 94
18
I guess interviewer think the fact you are arguing with him on this issue, y
ou are good enough in understanding singleton.
Look at the following links, the issue exists in C/C++.
I thought Java's threading package is good enough to avoid that. Anyway, I h
aven't touched Java for awhile.
http://www.drdobbs.com/184405726
http://www.drdobbs.com/184405772

【在 c********t 的大作中提到】
: that issue only exists in Java?
: I have one interview which I argue all the interview time with the
: interviewee about this issue
: Guess what, I still get the job.
: LOL
:
: o
: int
:

f******n
发帖数: 90
19
I don't see why it needs a private destructor.
If so, how is it supposed to be destructed?

【在 p********7 的大作中提到】
: private assignment operator
: private destructor

M******q
发帖数: 94
20
another public function, like destroy() that delete "this". it has to be cal
led explicitly though.

【在 f******n 的大作中提到】
: I don't see why it needs a private destructor.
: If so, how is it supposed to be destructed?

相关主题
C++ Q47: protected constructor (C39)C++ Singleton Template - 编译通不过
问个c++的问题Code for Singleton design pattern
singleton哪种写法好?Palantir 2nd coding interview [pass, set for on-site]
进入JobHunting版参与讨论
p********7
发帖数: 549
21
the constructor is private. For this pattern, you can't use constructor
and destructor. Because they can't control the number of instance. You can
only use friend class cleanup to delete the instance.

【在 f******n 的大作中提到】
: I don't see why it needs a private destructor.
: If so, how is it supposed to be destructed?

f******n
发帖数: 90
22
yes. But why do you have to destroy it explicitly? Why not a public dtor?

cal

【在 M******q 的大作中提到】
: another public function, like destroy() that delete "this". it has to be cal
: led explicitly though.

M******q
发帖数: 94
23
in some variants of singleton, like the instance pointer is instantiated thr
ough "new", public dtro doesn't make a difference. you have to delete it exp
licitly anyway, if you really want to call the dtor to do some cleanup

【在 f******n 的大作中提到】
: yes. But why do you have to destroy it explicitly? Why not a public dtor?
:
: cal

P*******b
发帖数: 1001
24
定义一个static的destroy成员函数不行吗?

【在 p********7 的大作中提到】
: the constructor is private. For this pattern, you can't use constructor
: and destructor. Because they can't control the number of instance. You can
: only use friend class cleanup to delete the instance.

p********7
发帖数: 549
25
Sorry, I don't know.

【在 P*******b 的大作中提到】
: 定义一个static的destroy成员函数不行吗?
P*******b
发帖数: 1001
26
讨论讨论

【在 p********7 的大作中提到】
: Sorry, I don't know.
w**a
发帖数: 4743
27
如果我面试你,就问如何传递constructor参数
f******n
发帖数: 90
28
Then when does a private dctor make a difference?
If it's private, we would do sth like: pObj->destory().
If it's public, we would need to do: delete pObj;
help! I still don't get why dctor need to be private for singleton pattern.

thr
exp

【在 M******q 的大作中提到】
: in some variants of singleton, like the instance pointer is instantiated thr
: ough "new", public dtro doesn't make a difference. you have to delete it exp
: licitly anyway, if you really want to call the dtor to do some cleanup

M******q
发帖数: 94
29
"doesn't make a difference" means "it doesn't matter".
singleton doesn't require dctor to be private. singleton's essential idea is
, there is only one instance of a certain class. My understanding is, other
aspects could vary on different variants of singleton.
BTW, destroy() should be static function, not associated with any instance.

【在 f******n 的大作中提到】
: Then when does a private dctor make a difference?
: If it's private, we would do sth like: pObj->destory().
: If it's public, we would need to do: delete pObj;
: help! I still don't get why dctor need to be private for singleton pattern.
:
: thr
: exp

P*******b
发帖数: 1001
30
我也不理解为啥要用friend class去delete,直接static destroy不是一样的吗?

【在 f******n 的大作中提到】
: Then when does a private dctor make a difference?
: If it's private, we would do sth like: pObj->destory().
: If it's public, we would need to do: delete pObj;
: help! I still don't get why dctor need to be private for singleton pattern.
:
: thr
: exp

相关主题
被鄙视了, c++基础知识请问一道special singleton class的题
刚才有个讨论singleton的帖子,找不到了gg面试题
singleton pattern problem弱问:singleton要不要destructor啊?
进入JobHunting版参与讨论
f******n
发帖数: 90
31
I don't see why destroy() has to be a static, instead of a regular method.

is
other
.

【在 M******q 的大作中提到】
: "doesn't make a difference" means "it doesn't matter".
: singleton doesn't require dctor to be private. singleton's essential idea is
: , there is only one instance of a certain class. My understanding is, other
: aspects could vary on different variants of singleton.
: BTW, destroy() should be static function, not associated with any instance.

M******q
发帖数: 94
32
you are right. it doesn't have to

【在 f******n 的大作中提到】
: I don't see why destroy() has to be a static, instead of a regular method.
:
: is
: other
: .

1 (共1页)
进入JobHunting版参与讨论
相关主题
Code for Singleton design patternGoogle电面被拒,郁闷中
Palantir 2nd coding interview [pass, set for on-site]Can a 10-year-Java guy answer these 2 questions promptly?
被鄙视了, c++基础知识问一个thread safe singleton的问题
刚才有个讨论singleton的帖子,找不到了C++ Singleton的实现
singleton pattern problemOne C++ question
请问一道special singleton class的题Java 面试题
gg面试题问一个关于c++的很傻的问题,多谢
弱问:singleton要不要destructor啊?C++ Q47: protected constructor (C39)
相关话题的讨论汇总
话题: singleton话题: static话题: instance话题: private