Single-Case mode

Source: Internet
Author: User

When we write a program, most of the time, the class is defined as a public type, and any class can create objects of that class at will. But sometimes, this does not make sense, the frequent creation of objects and the collection of objects caused by memory loss, so there is a singleton mode

A class can only create one object, then this class becomes a singleton class, which becomes a singleton pattern

  The principle of singleton mode is:

1. Hide the construction method of the class

2. Create a method that can create one (and only one) of its own instances

3. This method can be used externally

Singleton modes: Lazy mode, a hungry man mode, and hierarchical mode

  Lazy mode:

/**     * Lazy mode, characterized by the creation of the class object when it is used */     

Public class Single {
    //define a class variable to store the class object    Private StaticSingle ; //Hide Constructor    PrivateSingle () {}//defines a method for external invocation that returns a unique object of that class     Public StaticSingle Getsingle () {if(single =NULL) { single=NewSingle (); }        returnSingle ; }     Public Static voidMain (string[] args) {single S1=Single.getsingle (); Single S2=Single.getsingle (); if(S1 = =S2) {System.out.println ("Single class is a singleton mode"); } Else{System.out.println ("Single class is not a singleton mode"); }    }}

  A Hungry man mode:

/**     * A hungry man mode A hungry man mode feature is created when the class is loaded to be called, and the instance is immutable     */

Public classSingle { //creates an instance of the class directly, and the instance is immutable Private Static FinalSingle =NewSingle (); //Hide Constructors PrivateSingle () {}//creates a method for external invocation, returning an instance of the class Public StaticSingle Getsingle () {returnSingle ; } Public Static voidMain (string[] args) {single S1=Single.getsingle (); Single S2=Single.getsingle (); if(S1 = =S2) {System.out.println ("Single class is a singleton mode"); } Else{System.out.println ("Single class is not a singleton mode"); } }}

  Registration mode:

ImportJava.util.HashMap;ImportJava.util.Map;ImportJava.util.Set; /*** Single-mode registration mode, characterized by the maintenance of a set of maps in advance to store an instance, when creating an instance, the first to see if the map exists, if it exists, then directly return, if not exist, then deposit, and then return*/ Public classSingle {//define a map that is used to register instances    Private Staticmap<string, single> map =NewHashmap<string, single>(); //first, store a single instance in map    Static{ Single=NewSingle ();    Map.put (Single.getclass (). GetName (), single); }    //Hide Constructor    PrivateSingle () {}//defines a method for external invocation to return an instance, which needs to pass in a parameter as a "key" in the map     Public StaticSingle Getsingle (String key) {//if the passed parameter is NULL, the class name is assigned to it        if(Key = =NULL) {Key= Single.class. GetName (); }        //determines whether a value exists based on the key passed in, and if it does not exist, the        if(Map.get (key) = =NULL) {            Try{map.put (key, (single) Class.forName (key). Newinstance ()); } Catch(instantiationexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(illegalaccessexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(ClassNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); }        }        returnMap.get (key); }     Public Static voidMain (string[] args) {single single1= Single.getsingle (NULL); Single Single2= Single.getsingle (NULL); System.out.println (Single1==single2); }}        

  

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