Use the design pattern to become the CEO and marry Bai Fumi (3)---explain the singleton pattern for the boss to press

Interview lineup

On the second day, Chen Ermao came to the company for an interview as scheduled, but he saw the company name: Walter Goode. What do you mean, Er Mao looked dumbfounded.

The sisters at the front desk are still quite beautiful, white and clean, making Ermao feel good.

Sitting reluctantly in the interview room arranged by the girl at the front desk, Ermao looked at the company, but saw: the windows are bright and the lights are bright, ah.

After a while, several people came in, one was an old cat, sitting on one side, a capable woman sitting on one side, and the other pretty middle-aged, sitting in the middle. There is no suspense, the old cat is the boss of the software department who needs to come over, the middle one must be the boss, and the next one should be human resources.

Single-handedly

At first, the old cat asked a few basic computer knowledge, such as C language parameters, Http request methods, and projects that I have done. Since I prepared it last night, Ermao's answer was OK.

Then suddenly the boss began to ask: Are you familiar with design patterns?

Sure enough! Er Mao directly replied stiffly: That is quite familiar.

The boss asked again about the application scenario of the singleton mode. Ermao was so excited. I was a bit confused when I read the book last night, but the first singleton mode I saw was still very impressive.

Singleton mode application scenarios

Ermao began to answer. Take our company as an example:

The singleton model can ensure that the company has only one finance department that can contribute money.

The singleton mode can ensure that only the warehouse can store the equipment to be shipped.

The singleton mode can ensure that the company has only one main power switch to control the entire company's equipment.

The business license is also a single case, which is equivalent to a single case registered by the company in the national enterprise information database.

The wife who is legally compliant is also a singleton, which means that an important purpose of the marriage law is to realize the singleton of the spouse so as to realize the reasonable allocation of spouse resources.

Haha..., the last case was Ermao's magical pen, and the three people including the boss laughed.

Among them, Yushu Lao Mao laughed the most slutty. Er Mao wanted to give him two punches to calm him down.

Singleton code

The boss said, let's take a section of the code of the singleton mode. OK, let's take only one warehouse as an example.

/**
 * 单例模式--仓库
 */
public class Warehouse {
    
    
	private static Warehouse warehouse = new Warehouse();

	private Warehouse() {
    
    

	}

	public static Warehouse getInstance() {
    
    
		return warehouse;
	}
}

The boss said, let's talk about it, why this can guarantee a singleton. Er Mao then began to explain:

  1. By staticspecifying warehouse as a static variable, it already exists when the class is loaded. Since it is a static variable, there is only one.
  2. By privatethe constructor is defined as private, so you can not use new Warehouse()to generate Warehouse instance, so Wasehouse instance only one, is that static variable points.
  3. The last static method getInstanceis to get static variables.

Sweet dreams come

After hearing this, the boss was silent, and after a while, he said: I didn’t expect that our small place could produce two talents like you and Lao Mao at the same time! Come to work tomorrow.

Ah, Ermao is about to jump up with excitement, and finally he can work in a serious manner. He is still a legendary programmer. This kind of feel is too cool!

"But", suddenly the boss said another sentence, go to the workshop director, I think you are a managerial talent. . .

Ermao: I'm down

Old cat: I'm down~~

Boss: Hey, talent!

In the evening Ermao had a dream. In the dream, Ermao was driving a sports car with a beautiful woman in her arms. The beautiful woman’s long hair was blown by the wind, and a few strands of hair drifted over Ermao’s ears...

Winner in life, but so~~

The most beautiful dream is not to come true, but to look forward to.

UML graphics

The singleton mode is just one class, which is relatively simple and will not be detailed
Insert picture description here

Guess you like

Origin blog.csdn.net/woshisangsang/article/details/108212720