Java design pattern (1) -- Singleton pattern

Source: Internet
Author: User

(1) Singleton Mode
Key points: (1) Each class has only one instance. (2) It must create the unique instance on its own. (3) It must provide the instance to other objects on its own.
Nature: controls the number of instances.
Application scenarios: thread pool, cache, log object, dialog box, printer, graphics card driver object, etc.
(2) The Singleton mode can be divided into lazy singleton and ELE. Me Singleton.
1. Lazy (delayed Loading Method): Time for Space
Public class Singleton {
// 1. revoke the permission of the externally instantiated Singleton class
Private Singleton (){};

// 3. Declare a static object to save the instance
Private static Singleton single = null;

// 2. provide external methods to obtain the instance
Public static Singleton getInstance ()
{
// 4. Determine whether the instance has been instantiated
If (single = null)
{
// 5. instantiate if not instantiated
Single = new Singleton ();
}
Return single;
}
}
Advantage: The resource usage is high. If you do not execute getInstance, the instance will not be used.
Disadvantage: The thread is not secure, and multiple instances may appear in the concurrent environment.
Improvement Plan:
Public class Singleton {
// 1. revoke the permission of the externally instantiated Singleton class
Private Singleton (){};

// 3. Declare a static object to save the instance
Private static Singleton single = null;

// 2. Actively provide external methods to obtain the instance (with the synchronized lock to achieve thread security)
Public static synchronized Singleton getInstance ()
{
// 4. Determine whether the instance has been instantiated
If (single = null)
{
// 5. instantiate if not instantiated
Single = new Singleton ();
}
Return single;
}
}
Disadvantage: the use of synchronized increases unnecessary resource overhead.
2. Hungry Chinese style (pre-loading method): Change the time with Space
Public class Singleton {
// 1. revoke the permission of the externally instantiated Singleton class
Private Singleton (){};

// 3. Run to initialize the instance
Private static Singleton single = new Singleton ();

// 2. provide external methods to obtain the instance
Public static Singleton getInstance ()
{
Return single;
}
}
Advantages: thread security; Initialization is implemented when running, so instance calling is fast.
Disadvantage: the resource utilization is not high, and there may be instances that will never be used.
Test:
Public class main {
Public static void main (String [] args)
{
Singleton single1 = Singleton. getInstance (); // get instance 1

Singleton single2 = Singleton. getInstance (); // get instance 2

If (single1 = single2) // determine whether two instances are the same
{
System. out. print ("same instance ");
}
Else
{
System. out. print ("different instances ");
}
}
}
Running result: same instance

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.