Design Pattern Singleton mode

Source: Internet
Author: User

Singleton mode: guarantees that a class has only one instance and provides a global access point.

Advantages:

1) The singleton mode only generates one instance, reduces system performance overhead, and when an object is generated that requires more resources, such as reading the configuration and generating other dependent objects, it can be resolved by producing a singleton object directly at application startup and then permanently residing in memory.

2) The singleton mode can set the global access point of the system, optimize the access of shared resources, for example, can design a singleton class, responsible for all data table mapping processing.

Common Singleton mode implementations:

Mainstream:

1) A Hungry man type (thread safe, high efficiency, but can not delay load)

2) Lazy (thread-safe, call efficiency is not high, but can delay loading)

Other:

1) Double-detection lock (occasionally problematic due to JVM underlying internal model causes)

2) static internal class (thread-safe, high-efficiency call, and can delay loading)

3) enumeration of singleton (thread-safe, high-efficiency calls, but not delayed loading)

Application: Windows Task Manager, each bean in spring by default is a singleton and the database connection pool design is generally a single case, database connection is a database resource, the website counter generally also take a singleton mode, convenient synchronization, servlet programming, Each servlet is also a singleton.

Implementation code:

1) A Hungry man type:

Package com. singleton;/** * Test a hungry man singleton mode * @author Dell * */public class SingletonDemo01 {/** * class is initialized when loading immediately * Load class is natural thread-safe */private stat IC SINGLETONDEMO01 instance = new SingletonDemo01 ();p rivate SingletonDemo01 () {}//method does not need to be synchronized, call high efficiency public static SingletonDemo01 getinstance () {return instance;}}

2) Lazy Type:

Package com. Singleton;public class SingletonDemo02 {//Do not instantiate in this, lazy load, real use when loading private static SingletonDemo02 instance;// Privatization constructor Private SingletonDemo02 () {}//method synchronous, call inefficient public static synchronized SingletonDemo02 getinstance () {if (instance== NULL) {instance = new SingletonDemo02 ();} return instance;}}

3) Static Inner class

Package com.  singleton;/** * Static inner class implementation Singleton mode * This approach is thread-safe, highly efficient and time-delay loaded * @author Dell * */public class SingletonDemo03 {private static class Singletonclassinstance{private static final SingletonDemo03 instance = new SingletonDemo03 (); public static SingletonDemo03 getinstance () {return singletonclassinstance.instance;} Private SingletonDemo03 () {}}

4) Enumerate the singleton patterns

Package com. singleton;/** * Avoids the vulnerability of reflection and deserialization, high efficiency * enumeration mode without delay loading effect * @author Dell * */public enum SINGLETONDEMO04 {//This enumeration element itself is a singleton object instance ;p ublic void SingletonDemo04 () {}}

Test class:

Package com. Singleton;public class Client {public static void main (string[] args) {SingletonDemo01 S1 = singletondemo01.getinstance () ; SINGLETONDEMO01 s2 = singletondemo01.getinstance (); System.out.println (S1); SYSTEM.OUT.PRINTLN (S2); SingletonDemo02 s3 = Singletondemo02.getinstance (); SingletonDemo02 S4 = Singletondemo02.getinstance (); System.out.println (S3); System.out.println (S4); SingletonDemo03 S5 = Singletondemo03.getinstance (); SingletonDemo03 s6 = Singletondemo03.getinstance (); System.out.println (S5); System.out.println (S6); SingletonDemo04 s7 = singletondemo04.instance; SingletonDemo04 s8 = singletondemo04.instance; System.out.println (S7); System.out.println (S8);}}

Test results:

Design Pattern Singleton mode

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.