• 2020-03-04


    庚子鼠年 戊寅月 丙午日

    描述

    把昨天的项目完成了,有时间在升级吧

    设计模式之单例模式

    技术博客:null

    随笔

    单例模式

    1. 构造方法私有化
    2. 对外提供获取实例的静态方法

    饿汉式

    方式一:静态属性 可以使用

    public class Singleton {
    
        private final static Singleton INSTANCE = new Singleton();
    
        private Singleton(){}
    
        public static Singleton getInstance(){
            return INSTANCE;
        }
    }
    

    方式二:静态代码块 可以使用

    public class Singleton {
    
        private static Singleton instance;
    
        static {
            instance = new Singleton();
        }
    
        private Singleton() {}
    
        public static Singleton getInstance() {
            return instance;
        }
    }
    

    懒汉式

    方式一:线程不安全 不可以使用

    public class Singleton {  
        private static Singleton instance;  
        private Singleton (){}  
      
        public static Singleton getInstance() {  
      	 	if (instance == null) {  
       		    instance = new Singleton();  
      		}  
        	return instance;  
        }  
    }
    

    方式二:(线程安全,同步方法) 效率不行,不推荐使用

    public class Singleton {
    
        private static Singleton singleton;
    
        private Singleton() {}
    
        public static synchronized Singleton getInstance() {
            if (singleton == null) {
                singleton = new Singleton();
            }
            return singleton;
        }
    }
    

    方式三:线程不安全,不可以使用

    public class Singleton {
    
        private static Singleton singleton;
    
        private Singleton() {}
    
        public static Singleton getInstance() {
            if (singleton == null) {
                synchronized (Singleton.class) {
                    singleton = new Singleton();
                }
            }
            return singleton;
        }
    }
    

    双重检查

    线程安全, 效率高 推荐使用

    public class Singleton {
    
        private static volatile Singleton singleton;
    
        private Singleton() {}
    
        public static Singleton getInstance() {
            if (singleton == null) {
                synchronized (Singleton.class) {
                    if (singleton == null) {
                        singleton = new Singleton();
                    }
                }
            }
            return singleton;
        }
    }
    

    静态内部类

    由于类加载的时候内部类不会加载,使用时才会去加载

    java虚拟机保证了类的加载是线程安全推荐使用

    public class Singleton {
    
        private Singleton() {}
    
        private static class SingletonInstance {
            private static final Singleton INSTANCE = new Singleton();
        }
    
        public static Singleton getInstance() {
            return SingletonInstance.INSTANCE;
        }
    }
    

    枚举

    借助JDK1.5中添加的枚举来实现单例模式。不仅能避免多线程同步问题,而且还能防止反序列化重新创建新的对象。可能是因为枚举在JDK1.5中才添加。

    public enum Singleton {
        INSTANCE;
        public void whateverMethod() {
    
        }
    }
    
  • 相关阅读:
    出现灾难性Bug:Vista RTM跳票内幕曝光
    微软官方反间谍流氓软件WindowsDefender
    在Windows上玩转Mono/Linux
    使用信息架构视图访问数据库元数据
    BPM 与 SOA的演进与展望
    使用Microsoft® .NET Framework 3.0 and Visual Studio® 2005开发的免费课程
    bootstrap源码学习与示例:bootstrapdropdown
    bootstrap源码学习与示例:bootstrapalert
    我的MVVM框架 v3教程——todos例子
    我的MVVM框架 v3教程——类名切换
  • 原文地址:https://www.cnblogs.com/chang1024/p/12416365.html
Copyright © 2020-2023  润新知