zoukankan      html  css  js  c++  java
  • 单例设计模式以及使用场景

    单例模式的使用场景

    例如:数据源、session工厂

    1. 需要频繁的创建和销毁对象
    2. 经常需要使用的对象
    3. 创建的对象销毁过多的资源
    4. 工具类对象

    饿汉式

    静态常量

    class Singleton {
    	
    	//1. 构造器私有化, 外部不能new获取
    	private Singleton() {}
    	
    	//2.本类内部创建对象实例
    	private final static Singleton instance = new Singleton();
    	
    	//3. 提供一个公有的静态方法,返回实例对象
    	public static Singleton getInstance() {
    		return instance;
    	}
    }
    

    静态代码块

    class Singleton {
    	
    	//1. 构造器私有化, 外部能new
    	private Singleton() {}
    	
    	//2.本类内部创建对象实例
    	private  static Singleton instance;
    	
    	static { // 在静态代码块中,创建单例对象
    		instance = new Singleton();
    	}
    	
    	//3. 提供一个公有的静态方法,返回实例对象
    	public static Singleton getInstance() {
    		return instance;
    	}
    	
    }

    懒汉式

    懒汉式(单线程内安全,多线程不安全)

    class Singleton {
    	private static Singleton instance;
    	
    	private Singleton() {}
    	
    	//提供一个静态的公有方法,当使用到该方法时,才去创建 instance
    	//即懒汉式
    	public static Singleton getInstance() {
    		if(instance == null) {
    			instance = new Singleton();
    		}
    		return instance;
    	}
    }
    

    懒汉式(线程安全,同步锁方法,但是效率低)

    class Singleton {
    	private static Singleton instance;
    	
    	private Singleton() {}
    	
    	//提供一个静态的公有方法,加入同步处理的代码,解决线程安全问题
    	//即懒汉式
    	public static synchronized Singleton getInstance() {
    		if(instance == null) {
    			instance = new Singleton();
    		}
    		return instance;
    	}
    }
    

    懒汉式(线程安全,双重检查instance变量是否已经实例化) volatile关键字 推荐的方法!

    需要注意的是这里使用的volatile 关键字,如果没有线程共享volatile则多线程不安全

    class Singleton {
    	private static volatile Singleton instance;
    	
    	private Singleton() {}
    	
    	//提供一个静态的公有方法,加入双重检查代码,解决线程安全问题, 同时解决懒加载问题
    	//同时保证了效率, 推荐使用
    	
    	public static synchronized Singleton getInstance() {
    		if(instance == null) {
    			synchronized (Singleton.class) {
    				if(instance == null) {
    					instance = new Singleton();
    				}
    			}
    			
    		}
    		return instance;
    	}
    }
    

    懒汉式:静态内部类完成, 推荐使用

    静态内部类SingletonInstance在调用getInstance方法是才会装载SingletonInstance,因此是安全的。

    class Singleton {
    	
    	//构造器私有化
    	private Singleton() {}
    	
    	//写一个静态内部类,该类中有一个静态属性 Singleton
    	private static class SingletonInstance {
    		private static final Singleton INSTANCE = new Singleton(); 
    	}
    	
    	//提供一个静态的公有方法,直接返回SingletonInstance.INSTANCE
    	
    	public static synchronized Singleton getInstance() {
    		
    		return SingletonInstance.INSTANCE;
    	}
    }
    

    枚举,可以实现单例, 推荐使用

    enum Singleton {
    	INSTANCE; //属性
    	public void method() {
    		System.out.println("枚举调用方法");
    	}
    }
    

    原文链接:https://blog.csdn.net/qq_41813208/article/details/106994203

  • 相关阅读:
    .NET/C#程序开发中如何更优美地实现失败任务重试的逻辑?
    ava.security.cert.CertPathValidatorException: Path does not chain with any of the trust anchors
    mysql安装到这里不能下去了,怎么解决
    win10安装jdk8点击下一步没反应,点击下一步闪退,win10安装jdk8失败
    运行打包的jar,jar中没有主清单属性
    idea如何打包项目(java)
    关于idea(eclipse同样适用,都是一样的步骤)无法导入Javafx包的问题及解决方案:
    Intellij idea 报错:Error : java 不支持发行版本5
    Class.getResource("xxx.css")得到值为null
    jdk11安装没有jre文件夹
  • 原文地址:https://www.cnblogs.com/ljstudy/p/14495717.html
Copyright © 2011-2022 走看看