Introduction to the design mode: Singleton Mode

Source: Internet
Author: User
Introduction to design mode Singleton mode Release Date: 11-05-07 Article Source: Internet

[Share] Java Singleton mode provides two implementation methods: lazy and ELE. Me.
Implementation of Singleton Mode
|-Singleton /**
* One of the singleton mode implementations
*
* Create and instantiate a singleton object from the beginning
*
*/
Class Singleton
{
// Create a signleton instance object to store the created instance
Private Static Singleton = new Singleton ();
// Privatize the constructor
Private Singleton ()
{
// Code to be executed
}
// Obtain the sington Instance Object
Public static Singleton getinstance ()
{
// Return the Instance Object
Return Singleton;
}
} Copy the code |-test class public class singletondemo
{
Public static void main (string [] ARGs)
{
// Create a singleton Instance Object
Singleton singleton01 = singleton. getinstance ();
Singleton singleton02 = singleton. getinstance ();
// Determine whether two instance objects are the same
If (singleton01 = singleton02)
{
// Print the "the same object! "
System. Out. println ("the same object! ");
}
Else
{
// Print "The differentobject! "
System. Out. println ("the different object! ");
}
}
} Copy the code |-test result the same object! From the test results, we can see that the singleton object is the same.
|-Diagram of the sequence of calls of the hungry Chinese program

Implementation of Singleton Mode
|-Singleton /**
* Implementation of Singleton Mode
*
* When an instance object is required, an object is generated for instantiation.
*
*/
Class Singleton
{
// Declare a signleton variable to store the created instance
Private Static Singleton = NULL;
// Privatize the constructor
Private Singleton ()
{
// Code to be executed
}
// Obtain the sington Instance Object
Public static Singleton getinstance ()
{
// Determine whether Singleton is null. If yes, It is instantiated.
If (null = Singleton)
{
Singleton = new Singleton ();
}
// Return the Instance Object
Return Singleton;
}
} Copy the code |-test class and test result and aboveImplementation of Singleton ModeTo generate the same Singleton
Object.
|-Lazy program call sequence diagram

The above two methods are single-threaded, but in the case of multiple threads,LazyMore
A singleton object. Example
Implementation of Singleton mode (multithreading)
|-Singleton /**
* Implementation of Singleton Mode
*
* When an instance object is required, an object is generated for instantiation.
*
* When multiple threads are used, the generated object may not be an object.
*
*/
Class Singleton
{
// Declare a signleton variable to store the created instance
Private Static Singleton = NULL;
// Privatize the constructor
Private Singleton ()
{
// Code to be executed
}
// Obtain the sington Instance Object
Public static Singleton getinstance ()
{
// Determine whether Singleton is null. If yes, It is instantiated.
If (null = Singleton)
{
// Catch exceptions
Try
{
// Sleep the thread. The sleep time is randomly generated.
Thread. Sleep (long) (math. Random () * 1000 ));
Singleton = new Singleton ();
}
Catch (interruptedexception E)
{
E. printstacktrace ();
}
}
// Return the Instance Object
Return Singleton;
}
} Added the thread. Sleep () method to copy the code to sleep the thread for a period of time. |-Thread class // The Thread class that implements the runnable interface
Class singletonthread implements runnable
{
// Override the run () method in the runnable interface
Public void run ()
{
// Print the obtained singleton object address
System. Out. println (singleton. getinstance ());
}
} Copy the code to get the singleton object. |-Test class public class singletondemo
{
Public static void main (string [] ARGs)
{
// Create two anonymous thread objects and start
New thread (New singletonthread (). Start ();
New thread (New singletonthread (). Start ();
}
} Copy the code |-Test Result com. shengsiyuan. javalinux. singleton03.singleton @ 69b332
Com. shengsiyuan. javalinux. singleton03.singleton @ 173a10f the copied Code results show that the memory address of the generated singleton object is different, that is, not the same object. Description of Multithreading
The Order example mode cannot be used to generate the same object.
Implementation of the singleton mode in a lazy way (multithreading)-using the synchronized Modifier
Improves the singleton class and locks getinstance () synchronously, that is, the synchronized keyword is used.
Modify:Singleton /**
* Implementation of Singleton Mode
*
* When an instance object is required, an object is generated for instantiation.
*
* When multiple threads are used, the getinstance () method of the generated instance is locked.
*
*/
Class Singleton
{
// Declare a signleton variable to store the created instance
Private Static Singleton = NULL;
// Privatize the constructor
Private Singleton ()
{
// Code to be executed
}
// Lock this method to implement synchronization to obtain the sington Instance Object
Public static synchronized Singleton getinstance ()
{
// Determine whether Singleton is null. If yes, It is instantiated.
If (null = Singleton)
{
// Catch exceptions
Try
{
// Sleep the thread. The sleep time is randomly generated.
Thread. Sleep (long) (math. Random () * 1000 ));
Singleton = new Singleton ();
}
Catch (interruptedexception E)
{
E. printstacktrace ();
}
}
// Return the Instance Object
Return Singleton;
}
} Copy the code |-Test Result com. shengsiyuan. javalinux. singleton04.singleton @ 1b67f74
Com. shengsiyuan. javalinux. singleton04.singleton @ 1b67f74 copy the code and print the same result. The generated singleton object is the same object.
Implementation of the singleton mode in a lazy way (multithreading)-using the synchronized code block
Use the synchronous code block to lock the getinstance () method. /**
* One of the singleton mode implementations
*
* When an instance object is required, an object is generated for instantiation.
*
* When multiple threads are used, the getinstance () method of the generated instance is locked.
*
*/
Class Singleton
{
// Declare a signleton variable to store the created instance
Private Static Singleton = NULL;
// Privatize the constructor
Private Singleton ()
{
// Code to be executed
}
// Obtain the sington Instance Object
Public static Singleton getinstance ()
{
// Determine whether Singleton is null. If yes, It is instantiated.
If (null = Singleton)
{
// Catch exceptions
Try
{
// Sleep the thread. The sleep time is randomly generated.
Thread. Sleep (long) (math. Random () * 1000 ));
// Use the synchronous code block to create instances in thread-Safe Mode
Synchronized (singleton. Class)
{
// Check whether the instance exists again. If the instance does not exist
If (null = Singleton)
Singleton = new Singleton ();
}
}
Catch (interruptedexception E)
{
E. printstacktrace ();
}
}
// Return the Instance Object
Return Singleton;
}
} Copy the code |-Test Result com. shengsiyuan. javalinux. singleton05.singleton @ 1a758cb
Com. shengsiyuan. javalinux. singleton05.singleton @ 1a758cb copy the code and print the same result. The generated singleton object is the same object.
The above is the singleton mode in the Java design mode I understand. Which may be inappropriate. Please advise.

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.