【设计模式(1) Java】单例模式

题目:设计一个类,我们只能生成该类的一个实例。

代码如下:

package demo;

public class TestSingleton {
    /**
     * 懒汉式,线程安全
     */
    public static class Singleton1 {
        private static final Singleton1 INSTANCE = new Singleton1();
        private Singleton1() {
        }
        public static Singleton1 getInstance() {
            return INSTANCE;
        }
    }

    /**
     * 懒汉式,变种,线程安全
     */
    public static class Singleton2 {
        private static Singleton2 instance = null;
        static {
            instance = new Singleton2();
        }
        private Singleton2() {
        }
        public static Singleton2 getInstance() {
            return instance;
        }
    }

    /**
     * 饿汉式,线程不安全
     */
    public static class Singleton3 {
        private static Singleton3 instance = null;
        private Singleton3() {
        }
        public static Singleton3 getInstance() {
            if(instance == null) {
                instance = new Singleton3();
            }
            return instance;
        }
    
        /**
         * 饿汉式,线程安全,多线程环境下效率不高
         */
        public static class Singleton4 {
            private static Singleton4 instance = null;
            private Singleton4() {
            }
            public static synchronized Singleton4 getInstance() {
                if(instance == null) {
                    instance = new Singleton4();
                }
                return instance;
            }
        }
    
        /**
         * 使用静态内部类,线程安全
         */
        public static class Singleton5 {
            private static final class SingletonHolder {
                private static final Singleton5 INSTANCE = new Singleton5();
            }
            private Singleton5() {
            }
            public static Singleton5 getInstance() {
                return SingletonHolder.INSTANCE;
            }
        }
    
        /**
         * 静态内部类,使用双重校验锁,线程安全
         */
        public static class Singleton6 {
            private volatile static Singleton6 instance = null;
            private Singleton6() {
            }
            public static Singleton6 getInstance() {
                if(instance == null) {
                    synchronized (Singleton6.class) {
                        if(instance == null) {
                            instance = new Singleton6();
                        }
                    }
                }
                return instance;
            }
        }
    
        /**
         * 静态内部类,使用枚举方式,线程安全
         */
        public enum Singleton7 {
            INSTANCE;
            public void whateverMethon() {
            }
        }
    
    
        public static void main(String[] args) {
            System.out.println(Singleton1.getInstance() == Singleton1.getInstance());
            System.out.println(Singleton2.getInstance() == Singleton2.getInstance());
            System.out.println(Singleton3.getInstance() == Singleton3.getInstance());
            System.out.println(Singleton4.getInstance() == Singleton4.getInstance());
            System.out.println(Singleton5.getInstance() == Singleton5.getInstance());
            System.out.println(Singleton6.getInstance() == Singleton6.getInstance());
            System.out.println(Singleton7.INSTANCE == Singleton7.INSTANCE);
        }
    }
}
运行结果

来源:http://blog.csdn.net/derrantcm/article/details/45330779

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容