Java design mode -- Singleton mode and java design mode --

Source: Internet
Author: User

Java design mode -- Singleton mode and java design mode --

 Singleton Design Mode

Singleton is a creation mode. A Class adopts the Singleton mode. After this class is created, only one instance may be generated for external access and a global access point is provided.

The core knowledge points are as follows:

(1) privatize the construction methods of classes using a single-case design model (using private modification ).

(2) generate an instantiated object of the class within it and encapsulate it into a private static type.

(3) define a static method to return the instance of this class.

/*** Method 1 * Implementation of Singleton mode: Hungry, thread security, but low efficiency */public class SingletonTest {
// Define a private constructor private SingletonTest (){}
// Set your instance object to an attribute, and add the Static and final modifiers private static final SingletonTest instance = new SingletonTest ();
// The static method returns the public static SingletonTest getInstancei () {return instance ;}}

Method 1: the legendary hunger Model
Advantages: It is relatively simple to write, and there is no multi-thread synchronization problem, avoiding performance problems caused by synchronized;
Disadvantage: When the SingletonTest class is loaded, the static instance will be initialized, and the static variables will be created and allocated memory space, this static instance Object occupies the memory (even if you haven't used this instance). When the class is detached, the static variable is destroyed and the occupied memory is released, therefore, memory is consumed under certain conditions.

/*** Method 2 ** Singleton mode implementation: full Chinese style, non-thread security **/public class SingletonTest {

// Define the private constructor (to prevent instantiation through new SingletonTest () private SingletonTest (){}
// Define a SingletonTest type variable (not initialized, note that the final keyword is not used here) private static SingletonTest instance;
// Define a static method (SingletonTest is initialized during the call, but multi-threaded access may cause repeated initialization issues) public static SingletonTest getInstance () {if (instance = null) instance = new SingletonTest (); return instance ;}}

The second method is the legendary fooham pattern.
Advantage: It is relatively simple to write. When the SingletonTest class is loaded, the static instance is not created and the memory space is allocated. When the getInstance method is called for the first time, the instance variable is initialized, and allocate memory. Therefore, memory is saved under certain conditions;
The disadvantage is that multiple SingletonTest instances may appear in the concurrent environment.

/*** Method 3 ** Singleton mode implementation: full Chinese style, simple thread security implementation **/public class SingletonTest {
// Define the private constructor (to prevent instantiation through new SingletonTest () private SingletonTest (){}
// Define a SingletonTest type variable (not initialized, note that the final keyword is not used here) private static SingletonTest instance;
// Define a static method (SingletonTest is initialized when calling, and synchronized is used to avoid multi-threaded access, which may cause re-initialization problems) public static synchronized SingletonTest getInstance () {if (instance = null) instance = new SingletonTest (); return instance ;}}

Method 3: simple Optimization of method 2
Advantage: Multiple SingletonTest instances appear when the synchronized keyword is used to avoid multi-threaded access.
The disadvantage is that the efficiency is slightly lower when the synchronization method is frequently called.

/*** Method 4 ** Optimal Solution for Singleton mode * thread security and high efficiency **/public class SingletonTest {
// Define a private constructor private SingletonTest (){
} // Define a static private variable (without initialization or using the final keyword, volatile ensures the visibility of the instance variable during multi-thread access, avoid being called by another thread when other variable attributes are not assigned a value during instance initialization.) private static volatile SingletonTest instance;
// Define a common static method and return the public static SingletonTest getIstance (){
// Determines whether an object is instantiated. (if the value of instance is not null, an object is directly returned to improve the running efficiency.) if (instance = null ){
// Synchronous Code block (Synchronous Code block is used when the object is not initialized to ensure that the object will not be repeatedly created after the first creation of the object during multi-threaded access) synchronized (SingletonTest. class ){
// If not initialized, the initial instance variable if (instance = null) {instance = new SingletonTest () ;}} return instance ;}}

Method 4 is the best implementation of the singleton mode. Memory footprint, high efficiency, thread security, and atomic multi-thread operations.

 

(In fact, you can instantiate the constructor of the private type through the Java reflection mechanism, which will basically invalidate all Java Singleton implementations. This post does not discuss reflection issues. By default, there is no reflection, which is also a common application scenario for interviews)

 

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.