3, using private constructors or enumeration types to enhance the Singleton property __effective

Source: Internet
Author: User
Conclusions

Single-element enumeration types have become the best way to implement singleton singleton patterns

Common Singleton have two ways, the first:

public class Elvis {
    //is defined as final, immutable public
    static final Elvis INSTANCE = new Elvis ();

    Private Elvis () {
    } public

    void Leavethebuilding () {
        System.out.println ("Whoa baby, I ' outta here!");
    }

    //This code would normally appear outside the class!
    public static void Main (string[] args) {
        Elvis Elvis = elvis.instance;
        Elvis.leavethebuilding ();
    }

The second way:

public class Elvis {
    private static final Elvis INSTANCE = new Elvis ();

    Private Elvis () {
    }
    ///defined as static method public
    static Elvis getinstance () {return
        INSTANCE;
    }

    public void leavethebuilding () {
        System.out.println ("Whoa baby, I ' M outta here!");
    }

    This code would normally appear outside the class!
    public static void Main (string[] args) {
        Elvis Elvis = Elvis.getinstance ();
        Elvis.leavethebuilding ();
    }
let Singleton become serializable.

To create only one object for deserialization

public class Elvis {public
    static final Elvis INSTANCE = new Elvis ();

    Private Elvis () {
    } public

    void Leavethebuilding () {
        System.out.println ("Whoa baby, I ' outta here!");
    }

    private Object Readresolve () {//Return the one true Elvis and let the
        garbage collector
        //Take care of the Elvis impersonator.
        return INSTANCE;
    }

    This code would normally appear outside the class!
    public static void Main (string[] args) {
        Elvis Elvis = elvis.instance;
        Elvis.leavethebuilding ();
    }
using enumerations to implement a single case pattern

Provides a serialization mechanism for free, and absolutely prevents multiple instantiations

public enum Elvis {INSTANCE;
    public void leavethebuilding () {System.out.println ("Whoa baby, I ' M outta here!");
    }//This code would normally appear outside the class!
        public static void Main (string[] args) {Elvis Elvis = elvis.instance;
    Elvis.leavethebuilding (); }
}

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.