Java Singleton design pattern (a class that implements Java has only one object)

Source: Internet
Author: User

the definition of a singleton design pattern: The singleton design pattern is a software design pattern in which a core class called a singleton class is included. The core is the hope that a class has only one object.

How do I implement a class that has only one object in memory?

First step: Construct the private;
The second step: Provide an object by itself;
Step three: Let the outside world access by public means.

The following is an implementation of the Singleton class:

The static keyword decorates the object, then the object becomes a static resource, shared, the image Point said, she slipped, who can on!
classsingle{Private StaticSingle S =NewSingle ();//declare a reference-type variable for this class, and use that variable to point to this class of objects. Use the static keyword so that s has only one copy in memory.     PrivateSingle () {}//declares a private constructor so that this class object cannot be instantiated outside of this class     Public StaticSingle Getsingleobject () {returns; }    //provides a public static method that can return this unique object. }classdemo{ Public Static voidMain (String []args) {single S1=Single.getsingleobject (); Single S2=Single.getsingleobject (); System.out.Println ("Is it an object?" "+ (S1 = =S2)); //= = is used to determine if the memory address of the two objects is the same, if the same, the same object is indicated.     }}

The single design pattern above is called the a Hungry man mode , meaning that the object is not needed, but when the class file is loaded, the object is instantiated , and if the class object is not used at all, then this class object will waste memory space. Therefore, the following lazy mode, that is, in use, the creation of objects, the code is implemented as follows:

classsingle{Private StaticSingle S;//declares a reference to the object, but does not instantiate, the default value of S is null when static is not initialized    PrivateSingle () {}//guaranteed to instantiate objects only in this class     Public StaticSingle Getsingleobject () {if(s==NULL){//Determines whether the reference is emptys =NewSingle (); }        returns; }}classdemo{ Public Static voidMain (String []args) {single S1=Single.getsingleobject (); Single S2=Single.getsingleobject (); System.out.pringln ("Is it the same object?" "+ (s1==S2)); }}

The above is a lazy single example of the implementation of design patterns, but the model has a flaw in the multi-threaded access when there is a certain security risks. Therefore, it is recommended to use a hungry man mode at present.

Java Singleton design pattern (a class that implements Java has only one object)

Related Article

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.