Java Singleton mode implementation methods

Source: Internet
Author: User

1. The class has been self-instantiated during class initialization.

Class eagersingleton {Private Static final eagersingleton m_instance = new eagersingleton ();/*** default private constructor */private eagersingleton () {}/*** static factory Method */public static eagersingleton getinstance () {return m_instance ;}}

2. Lazy Singleton class. instantiated during the first call

Class lazysingleton {// note that there is no final Private Static lazysingleton m_instance = NULL;/*** private default constructor */private lazysingleton () {}/*** static factory Method */Public synchronized static lazysingleton getinstance () {If (m_instance = NULL) {m_instance = new lazysingleton ();} return m_instance ;}}

In the preceding lazy Singleton implementation, synchronization is used for static factory methods to process multi-threaded environments. Some designers are here

We recommend that you use the so-called "double check Examples". It must be noted that "double check Examples" cannot be used in Java. Not very familiar

Readers can refer to the section below.

Similarly, because the constructor is private, this class cannot be inherited. The hungry Chinese Singleton class will take its own instance when it is loaded

. Even if the loader is static, it will still instantiate itself when the hungry Chinese Singleton class is loaded. From the perspective of resource utilization efficiency,

This is slightly worse than the lazy Singleton class.

In terms of speed and response time, it is better than the lazy Singleton class. However, the lazy Singleton class must be

It is better to solve the access restriction problem when multiple threads reference this class for the first time. Especially when a singleton class is used as a resource controller

However, Resource Initialization is involved, and Resource Initialization is likely to take time. This means that the probability of this type being referenced by multiple threads for the first time changes.

.

The hungry Chinese Singleton class can be implemented in Java, but it is not easy to implement in C ++, because static Initialization is not fixed in C ++.

So the initialization of the static m_instance variable and the loading sequence of the class are not guaranteed, and problems may occur. That's why

When gof introduces the concept of a singleton class, the example is lazy. The impact of their books is so great that examples of Single-case classes in Java

Most of them are lazy. In fact, this book believes that the hungry Chinese Singleton class is more in line with the characteristics of the Java language itself.

3. The registration type Singleton class is similar to the method in spring. register the class name and obtain it directly from it next time. View copies to clipboard Printing

Import Java. util. hashmap; Class regsingleton {static private hashmap m_registry = new hashmap (); static {regsingleton x = new regsingleton (); m_registry.put (X. getclass (). getname (), x);}/*** protected default constructor */protected regsingleton () {}/ *** static factory method, return the unique instance */static public regsingleton getinstance (string name) {If (name = NULL) {name = regsingleton. class. getname () ;}if (m_registry.get (name) = NULL) {try {m_registry.put (name, class. forname (name ). newinstance ();} catch (exception e) {system. out. println ("error happened. ");} return (regsingleton) (m_registry.get (name);} return NULL;}/*** a schematic commercial Method */Public String about () {return "Hello, I am regsingleton. ";}} class regsingletonchild extends regsingleton {public regsingletonchild () {}/ *** static factory Method */static public regsingletonchild getinstance () {return (regsingletonchild) regsingleton. getinstance ("com. javapatterns. singleton. demos. regsingletonchild ");}/*** a schematic commercial Method */Public String about () {return" Hello, I am regsingletonchild. ";}}

From: http://langfangwangbin.blog.163.com/blog/static/1005436342008112711449788/

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.