Design Pattern-Singleton pattern (1) lazy and hungry

Source: Internet
Author: User

This document is the second article on the design mode study notes. It mainly analyzes the singleton mode. Including lazy, hungry, registered, and improved lazy,
There is also an instance for reading the propertoes configuration file. It is expected to be divided into three sections. This is the first section. First, we will analyze the simplest lazy and ELE. Me types.

Singleton mode is a simple design mode. Suitable for scenarios where a class has only one instance, such as window manager, print buffer pool, and file system,
They are all prototype examples. Typically, objects of different types are accessed by different objects in a software system. Therefore, a global access is required.
Pointer, which is a well-known application of Singleton mode. Of course, this is only when you are sure that you no longer need more than one instance.
The purpose of the singleton model is what you are concerned about in the previous section. In Singleton mode, you can:


1. Ensure that only one instance of a class is created
2. provides a global access pointer to the object
3. Allow multiple instances in the future without affecting the client of the singleton class

There are three classic Singleton models: lazy, hungry, and registered.

Lazy mode is characterized by delayed loading. For example, the configuration file adopts the lazy mode. As the name suggests, lazy mode is very lazy. the instance of the configuration file is still used.
......
The hungry Chinese style is loaded at the beginning. If the lazy style is "Time for space", then the hungry Chinese style is "space for Time" because an instance was created at the beginning, therefore, you can directly return the result after each usage.

 

Let's take a look at the Code:

 

Lazy style:

[Java]
// Lazy Singleton Mode
Public class MySingleton {
 
// Set static variables
Private static MySingleton mySingleton = null;
 
Private MySingleton (){
// Private constructor
System. out. println ("--> start calling constructor In the lazy Singleton mode ");
}

// Open a public method to check whether an instance exists and returns. If no public method is created
Public static MySingleton getInstance (){
System. out. println ("--> the public method is called in the lazy Singleton mode to return to the instance ");
If (mySingleton = null ){
System. out. println ("--> the lazy constructor instance is not currently created ");
MySingleton = new MySingleton ();
} Else {
System. out. println ("--> the lazy constructor instance has been created ");
}
System. out. println ("--> method call ends, returns Singleton ");
Return mySingleton;
}
}
Check the test code of the client:
[Java]
Public class Client {

/**
* Lazy Singleton Mode
* MySingleton
*/
Public static void myprint (){
System. out. println ("----------------- lazy Singleton mode ----------------");
System. out. println ("First Instance acquisition (lazy )");
MySingleton s1 = MySingleton. getInstance ();
System. out. println ("second instance acquisition (lazy )");
MySingleton s2 = MySingleton. getInstance ();
If (s1 = s2 ){
System. out. println (">>>> s1, s2 is the same instance (lazy) <");
}
System. out. println ();
}
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
// Lazy
Myprint ();
// Hungry Chinese
// Myprint2 ();
// Lazy Improvement
// Myprint2a ();
// Registration type
// Myprint3 ();
 
}
 
}

Output result:
----------------- Lazy Singleton mode ----------------
First instance acquisition (lazy)
--> In the lazy Singleton mode, the public method is called to return to the instance.
--> The lazy constructor instance is not currently created
--> In the lazy Singleton mode, the constructor is called.
--> Method call ends. A singleton is returned.
Second Instance acquisition (lazy)
--> In the lazy Singleton mode, the public method is called to return to the instance.
--> The lazy constructor instance has been created.
--> Method call ends. A singleton is returned.
>>>>> S1 and s2 are the same instance (lazy) <

We can see that there is no instance when calling the public method for the first time, so we created an instance and then accessed it again, because there is already a created instance, therefore, it is returned directly.

 

Hungry Chinese style:

[Java]
// Hunger Singleton Mode
Public class MySingleton2 {
 
// Set static variables to directly create an instance
Private static MySingleton2 mySingleton = new MySingleton2 ();
 
Private MySingleton2 (){
// Private constructor
System. out. println ("--> starting to call the constructor In the hungry Chinese Singleton mode ");
}

// Open a public method to check whether an instance exists and returns. If no public method is created
Public static MySingleton2 getInstance (){
System. out. println ("--> the hungry Chinese Singleton mode starts to call the public method to return to the instance ");
Return mySingleton;
}
}

Check the test code of the client:
[Java]
/**
* Hungry Chinese Singleton Mode
* MySingleton2
*/
Public static void myprint2 (){
System. out. println ("----------------- hunger type Singleton mode ----------------");
System. out. println ("First Instance acquisition (Hunger )");
MySingleton2 s1 = MySingleton2.getInstance ();
System. out. println ("second instance acquisition (Hunger )");
MySingleton2 s2 = MySingleton2.getInstance ();
If (s1 = s2 ){
System. out. println (">>>> s1, s2 is the same instance (hungry) <");
}
System. out. println ();
}
[Java]
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
// Lazy
// Myprint ();
// Hungry Chinese
Myprint2 ();
// Lazy Improvement
// Myprint2a ();
// Registration type
// Myprint3 ();
 
}


Output result:

----------------- Hunger-type Singleton mode ----------------
Obtain the instance for the first time (hungry Chinese Style)
--> The hungry Chinese Singleton mode starts to call constructors.
--> In the hungry Chinese Singleton mode, the public method is called to return to the instance.
Second Instance acquisition (hunger style)
--> In the hungry Chinese Singleton mode, the public method is called to return to the instance.
>>>>> S1 and s2 are the same instance (hungry Chinese) <


To sum up, the constructor and public method of the two schemes are both static, and the instance and public method are both private ). However, you do not need to create an instance for each call in the hungry Chinese style. The created instance is directly returned. In this way, although time is saved, space is occupied, and the instance itself is static and will always be carried in the memory. In the lazy way, it is judged that loading only when used will affect the speed of the program. The most important thing is that, in the case of concurrency, the lazy style is not safe. If two threads are called thread 1 and thread 2, The getInstance () method is called at the same time. if thread 1 first enters the if block, then thread 2 is controlled, two instances will be created.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.