Most programmers probably don’t use design patterns. The ideas behind design patterns are abstract, and there are techniques that can be used or not used to become a programmer. But people who use design patterns well will be better and more advanced programmers. Of the design patterns, I think the simplest and most understandable is the singleton pattern. One of the design patterns that I interview people for is the singleton pattern, because I think every programmer has to deal with this pattern. When I apply for jobs, MANY interviewers ask me about this model.

If an interviewer asks about the singleton feature, I think this is a simple answer. The singleton pattern is of the create type, and there can only be one instance globally, as the name implies. The constructor modifier static ensures that there is only one instance.

If we go a little further, we’ll talk about singleton slacker versus singleton hungrier. Hanky-hank mode means that the singleton is initialized immediately while the program is running. Lazy mode means that a singleton is initialized only the first time it is used.

Of course, if interviewers dig a little deeper, they can talk about improved singleton patterns, lifecycles, and thread safety, but I don’t think it’s a good idea to answer too many questions. Because this kind of thing is different, different people, only in the code constantly tempered out is the best, but it is obvious that most of us do not have this foundation. As long as you can clarify the concept, know how to use and where to come from, generally ok.

When I was just starting out, GetInstance was fun to see, and I often called it casually. Later, I realized that it was a singleton mode after GETTING familiar with it. Such as this

Language::GetInstance()->
Copy the code

The benefits of singletons are obvious, as some resource managers and simulator engine setup features are often set to singletons. Prevent other objects from instantiating their own copy in the global, after all, there is only a single instance of the global, you can flexibly change the instantiation properties, making the code more concise and concise.

Rabbit cloud program

Dedicated to promoting programming tutorials and serving programmers

13 original content

The public,