Java Design Pattern Summary three: Single case mode __ design mode

Source: Internet
Author: User
Tags static class
single case mode definition

Single case mode is a common software design pattern. In its core structure, there is only one special class called a single example. A single example pattern ensures that only one instance of a class in a system is available. That is, a class has only one object instance


How to implement a single example pattern: lazy implementation, but not multiple-threaded under the security

    public class Singleton {  
        private static Singleton instance;  
        Private Singleton () {} public  

        static Singleton getinstance () {  
        if (instance = = null) {  
            instance = new Singleto n ();  
        }  
        return instance  
        }  
    }  
Lazy Implementation 2: Thread-safe (add synchronied keyword) (unfortunately multithreaded execution is inefficient, in most cases, multithreading is not required)
    public class Singleton {  
        private static Singleton instance;  
        Private Singleton () {} public  
        static synchronized Singleton getinstance () {  
        if (instance = null) {  
            Instan CE = new Singleton ();  
        }  
        return instance  
        }  
    }  
A hungry man implementation: thread-safe (because a single instance object of the class is created directly when the class is loaded, so the object is not different because of multithreading, but the implementation obviously does not achieve the lazy loading effect)
    public class Singleton {  
        private static Singleton instance = new Singleton ();  
        Private Singleton () {} public  
        static Singleton getinstance () {return  
        instance;  
        }  
    }  
a hungry man variants (similar to the third, different order of initialization)
    public class Singleton {  
        private Singleton instance = null;  
        static {  
        instance = new Singleton ();  
        }  
        Private Singleton () {} public  
        static Singleton getinstance () {return  
        this.instance;  
        }  
    }  
static inner class: (Implements the effect of the lazy loading, because the static inner class is not used when the outer class is loaded, and the static inner class is loaded when the getinstance method of the outer class is invoked, thus generating the external class instance of the singleton pattern)
    When the public class Singleton {  
        //outer class is loaded, the inner class is not loaded until the GetInstance method is invoked, and therefore the effect of the lazy loading is also implemented as
        private static Class Singletonholder {  
        private static final Singleton INSTANCE = new Singleton ();  
        }  
        Private Singleton () {} public  
        static final Singleton getinstance () {return  
        singletonholder.instance;  
        }  
    }  
enumerations: (less used)


double-check Lock: A single case pattern implementation with the highest thread safety, but it does not guarantee that he will function on a single-processor or multiprocessor computer, which is due to the so-called "unordered input" allowed by the Java platform's memory model

    public class Singleton {  
        private volatile static Singleton Singleton;  
        Private Singleton () {} public  
        static Singleton Getsingleton () {  
        if (Singleton = = null) {  
            //To prevent multithreading concurrency, Locks the current class
            synchronized (singleton.class) {  
            if (Singleton = null) {  
                Singleton = new Singleton ();  
            }  
            }  
        }  
        Return singleton  
        }  
    }  


single Case Mode usage scenario:

A single case mode is used when connecting to the database

A single case pattern used by the servlet container


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.