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

  • 相关阅读:
    Linux查看文件夹大小
    mysql按照天或小时group分组统计
    eclipse可以调试但是无法打开网页,提示一直在加载
    自定义spring valid方式实现验证
    UniCode编码表及部分不可见字符过滤方案
    shiro中移除jsessionid的解决方案
    Apache Shiro去掉URL中的JSESSIONID
    shiro开启realm
    shiro注解@RequiresPermissions多权限任选一参数用法
    linux 复制粘贴
  • 原文地址:https://www.cnblogs.com/ljstudy/p/14495717.html
Copyright © 2011-2022 走看看