Single-Case mode

Source: Internet
Author: User

in Java, the singleton pattern is a common design pattern, there are several types of single-instance pattern, here are three kinds: lazy type single case, a hungry man type single case, registration type single case. The singleton mode has the following characteristics:1, a singleton class can have only one instance. 2, the Singleton class must create its own unique instance. 3, the Singleton class must provide this instance to all other objects. Singleton mode ensures that a class has only one instance, and instantiates itself and provides this instance to the entire system. In a computer system, the driver objects of the thread pool, cache, log Object, dialog box, printer, and video card are often designed as singleton. These apps have more or less the functionality of the resource manager. Each computer can have several printers, but only one printer Spooler to prevent both print jobs from being output to the printer at the same time. Each computer can have several communication ports, and the system should centrally manage these communication ports to prevent a communication port from being called simultaneously by two requests. In short, the selection of Singleton mode is to avoid inconsistent state, to avoid long-running political. One, the lazy one-case [Java] View Plaincopyprint?View code slices to my Code slice//Lazy Type Singleton class. Instantiate yourself at the first call     Public classSingleton {PrivateSingleton () {}Private StaticSingleton single=NULL; //Static Factory Method         Public StaticSingleton getinstance () {if(single =NULL) { single=NewSingleton (); }                returnSingle ; }} Singleton by restricting the construction method to private to prevent the class from being instantiated externally, the unique instance of Singleton can only be accessed through the getinstance () method within the same virtual machine scope. (In fact, the Java reflection mechanism is the ability to instantiate a class constructed as private, which basically invalidates all Java Singleton implementations.) This issue is not discussed here, and it is deceiving to assume that the reflection mechanism does not exist. But the above-mentioned lazy singleton implementation does not consider the thread security problem, it is thread insecure, the concurrency environment is likely to have multiple singleton instances, to achieve thread safety, there are the following three ways, is to getinstance this method of transformation, to ensure that the lazy one-case thread safety, If you first contact the singleton mode, the thread safety is not very understanding, you can skip the following three strips, to see the A hungry man-type singleton, and then look back to consider the issue of thread safety:1, adding synchronization to the GetInstance method [Java] View Plaincopyprint?View code slices to my Code slice Public Static synchronizedSingleton getinstance () {if(single =NULL) { single=NewSingleton (); }                returnSingle ; }  2, double check lock [Java] View Plaincopyprint?View code slices to my Code slice Public StaticSingleton getinstance () {if(Singleton = =NULL) {                    synchronized(Singleton.class) {                       if(Singleton = =NULL) {Singleton=NewSingleton (); }                    }                }                returnSingleton; }  3, static inner class [Java] View Plaincopyprint?View code slices to my Code slice Public classSingleton {Private Static classLazyholder {Private Static FinalSingleton INSTANCE =NewSingleton (); }            PrivateSingleton () {} Public Static FinalSingleton getinstance () {returnlazyholder.instance; }} This is better than the above 1, 2, both for thread safety and to avoid the performance impact of synchronization. Second, a hungry man type single example [Java] View Plaincopyprint?View code slices to my Code slice//a hungry man Singleton class. When class is initialized, it is self-instantiated     Public classSingleton1 {PrivateSingleton1 () {}Private Static FinalSingleton1 single =NewSingleton1 (); //Static Factory Method         Public StaticSingleton1 getinstance () {returnSingle ; }} A hungry man created a static object at the same time as the class was created for the system to use, and no longer changed, so it is inherently thread-safe. 

Single-Case 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: 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.