问题描述
我已阅读有关 S.O. 的问题.关于辛格尔顿,刚刚看了一个小时的谷歌技术谈话.据我所知,面向对象领域的共识似乎是单例更像是一种反模式,而不是一种有用的设计模式.
I've read the questions on S.O. regarding Singleton and just watched an hour long google tech talk. As far as I can tell, the consensus in the OO world seems to be that singletons are more of an anti-pattern rather than a useful design pattern.
也就是说,这几天我在面试,问题经常出现——什么是单身人士,你会如何使用它?
That said, I am interviewing these days and the question comes up a lot--what is a singleton, and how would you use it?
回答这个问题的最佳方式是什么?我是否应该简单地描述设计模式,然后说我听说过的唯一可接受的用途是用于日志记录,并且它经常被误用于全局状态?
What is the best way to answer this question? Should I simply describe the design pattern and then say the only acceptable use I've heard of is for logging, and that it is often mis-used for global state?
推荐答案
棘手.有很多人认为单例本质上是一种反模式,就像你说的(包括我在内),但也有很多人认为这只是一种 OOP 可接受的全局方式.
Tricky. There are plenty of people who feel that singletons are essentially an anti-pattern like you said (me included), but there are also a lot who feel that it is simply an OOP-acceptable way to do globals.
如果面试官在那个阵营,那么是的,我认为他错了,但这可能不是面试中最好的说法.;)
If the interviewer is in that camp then yes, I think he's wrong, but that might not be the best thing to say during the interview. ;)
所以我可能会尽量保持中立并坚持事实.你不知道你的面试官属于哪个阵营,所以坚持不争的事实.单例有什么作用?为了演示用法,请坚持大多数人都同意单例是可以接受的答案的少数情况.或者解释一下你的经历(因为人们也不能不同意这一点).
So I'd probably try to be neutral and stick to the facts. You don't know which camp your interviewer falls in, so stick to the indisputable facts. What does a singleton do? And to demonstrate usage, stick to the few cases where most people can agree that a singleton is an acceptable answer. Or explain your experience (since people can't disagree with that either).
但无论你是支持"还是反对"单身人士,面试可能都不是为这个事业奋斗的合适时机.;)
But whether you're "for" or "against" singletons, an interview is probably not the right time to crusade for that cause. ;)
这篇关于如何回答面试问题:什么是单身人士,你会如何使用单身人士?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!