Java single-Instance design pattern

Source: Internet
Author: User

The singleton design pattern is a Java design pattern, which means that when designing a class, there is only one instance object for that class of memory during the entire program run.

The single-case design pattern includes two modes: a hungry man and lazy.

A hungry man: Class One is loaded into memory and the object is created;

The code for the A Hungry man type is as follows:

 PackageCom.sjk.sheng;/*** A hungry man type of singleton mode *@authorSJK **/ Public classSingle {//privatize the constructor to keep the class object from being created by another class    PrivateSingle () {}//build yourself an object    Private Static FinalSingle S =NewSingle (); //provide a public access method     Public StaticSingle getinstance () {returns; }}

Lazy: When the class is loaded into memory, the object does not exist, and only when the getinstance () method is called does the object start to create.

The lazy type code is as follows:

 PackageCom.sjk.sheng;/*** Lazy type of singleton mode *@authorSJK **/ Public classSingle {PrivateSingle () {}Private StaticSingle S;  Public StaticSingle getinstance () {if(s==NULL)            //if the object is empty, create as=NewSingle (); returns; }}

Lazy is lazy loading, if more than one thread while the lazy type can be a thread security problem, to solve the thread safety problems can be synchronized to resolve. But after the synchronization, each time to compare the lock, efficiency becomes slow, so you can add double judgment to improve the efficiency of the program.

If the lazy-instance function is changed to synchronous:

 PackageCom.sjk.sheng;/*** Single-mode lazy thread safety solution * *@authorSJK **/ Public classSingle {//declare a private member variable of this class    Private StaticSingle ; //First Step: Privatization construction Method    PrivateSingle () {}//Step Three: Provide a public way to get the instance object of the class     Public StaticSingle getinstance () {if(single =NULL) {            synchronized(single) {if(single =NULL) { single=NewSingle (); }            }        }        returnSingle ; }}

The above through the A Hungry man type and the lazy type realizes the single case pattern, it has the following characteristic:

1) The constructor method of the class is declared private by using the private adornment, so that the instance object cannot be created outside the class using the New keyword.

2) Create an instance object of the class within the class, and use the static variable s to refer to the object, since the variable should prohibit direct access to the outside world, and use the private adornment to declare it as a private member.

3) in order for the class to be externally able to obtain an instance object of the class, a static method, getinstance (), needs to be defined to return the instance s of the class. Because it is static, the external can be accessed by means of the class name. Method Name.

Thus the A hungry man pattern can also be written in the following form:

 Package Com.sjk.sheng; /**  @author  SJK *  * */publicclass single {     Private Single () {}      Public Static Final New Single ();}

In the above code, the construction method of the class is first privatized to prevent the outside world from creating instances of the class. An instance object of the class is created inside the class and referenced by the static variable s, with three modifiers in front of the variable s, where public is allowed to directly access the variable externally, and static is used to allow external access to the variable using the "class name. Variable name" method. The final function is to prohibit external modification of the variable. Because the access variable s is the only way to get an instance object of single class, the class implements a singleton.

Java single-Instance design pattern

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.