面试官问了一道单例模式设计相关的问题。问题大概是这样,先考察一下我对单例模式的了解情况,毕竟这是GoF经典书籍《设计模式》中的一个经典的模式。在确认了我基本清楚这个设计模式后,让我用Java代码写一个单例模式的类,于是我给出了如下代码段。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
class
SingletonEx
{
private
static
SingletonEx instance =
null
;
private
SingletonEx(){}
public
static
SingletonEx getInstance()
{
if
( instance ==
null
)
{
instance =
|