The interview was asked about design patterns? Don't be afraid to look here: Singleton pattern

Design patterns are an old-fashioned problem. Some people have worked for many years but don't know anything about design patterns, but more people know a little bit, but don't ask for a deep understanding. In fact, this is not good, let alone the application at work, even in the interview, it is very embarrassing to be confused when the interviewer asks about the design pattern. This article is not nonsense, and does not talk about large-scale theoretical teaching. It is only aimed at interviews, giving the key points of design patterns, and letting everyone know and understand design patterns from the perspective of test taking.

First of all, make it clear that design patterns are not advanced technologies, not fancy skills. The design pattern is just a design idea. According to different business scenarios, the code structure is designed in different ways. The most essential purpose is to decouple , and if it is extended, it is also for scalability and robustness, but this They are all based on decoupling.

We all know the 21 design patterns of the famous GoF, and there should be many people who have read this book by head first. For these 21 design patterns, the questions asked by the interviewer may be ever-changing, which makes people inexplicably sad (dan) hurt (teng), but don't be afraid, as long as you figure out the key points and essence of each design pattern, you can By analogy, it was easily solved.

Today we will take a look at the simplest singleton mode. I won't introduce the theory, if you haven't heard of it, you can check it out. Even the simplest singleton has key points. To give an inappropriate example, students who have read novels such as Xiuxian Xiudao know that general formation masters have one or more "array eyes". Similarly, each of our design patterns also has "array eyes". , so where is the "eye" of the singleton? Don't panic, let's take a look at the code first. According to the theory of singleton mode: to ensure that there is only one instance in the system, so I wrote the following code

Well, if I am the interviewer and you are the candidate, I want you to give me a single instance, and you ask me if I am qualified by the above code, I am sure I am not qualified. Why? It's really not because I want to make a big news, but this singleton code can still barely run under normal circumstances, but it will be a big pill when it encounters multi-threaded concurrency conditions. Because this code is not thread- safe, it is possible to create multiple singleton instances for new, and if there are too many, it is still a fart "singleton".

好,废话不多说,继续撸代码,你可能会说:”不是说线程不安全吗?小爷我加上线程安全判断呗,度娘在手天下我有,来了您呐~~~“

好了,这回该消停了吧,锁也加了,线程也安全了。But,你还是太连清。。。这样依然有问题。问题在哪里?就在关键点2,检测单例是否被构造。虽然这里判断了一次,但是由于某些情况下,可能有延迟加载或者缓存的原因,只有关键点2这一次判断,仍然不能保证系统是否只创建了一个单例,也可能出现多个实例的情况,那么怎么办呢?

所以,在判断单例实例是否被构造时,需要检测两次,在线程锁之前判断一次,在线程锁之后判断一次,再去构造实例,这样就万无一失了。是不是很简(Tu)单(Xue)呢?

 

最后,我们来归纳一下。下次面试别人再问你单例模式,你可以这样说:

单例是为了保证系统中只有一个实例,其关键点有5

一.私有构造函数

二.声明静态单例对象

三.构造单例对象之前要加锁(lock一个静态的object对象)

四.需要两次检测单例实例是否已经被构造,分别在锁之前和锁之后

如果要你撸代码,你就撸最后这一段,完美~~~面试官要是个女的准想和你生猴子。。。

哦,别高兴太早了,面试官要是个男的,也可能会问你下列问题

0.为何要检测两次?

如上面所述,有可能延迟加载或者缓存原因,造成构造多个实例,违反了单例的初衷。

1.构造函数能否公有化?

不行,单例类的构造函数必须私有化,单例类不能被实例化,单例实例只能静态调用

2.lock住的对象为什么要是object对象,可以是int吗?

不行,锁住的必须是个引用类型。如果锁值类型,每个不同的线程在声明的时候值类型变量的地址都不一样,那么上个线程锁住的东西下个线程进来会认为根本没锁,相当于每次都锁了不同的门,没有任何卵用。而引用类型的变量地址是相同的,每个线程进来判断锁多想是否被锁的时候都是判断同一个地址,相当于是锁在通一扇门,起到了锁的作用。

 

好了,这下估计男的都想跟你生猴子了。。。

Guess you like

Origin blog.csdn.net/zs520ct/article/details/79870121