设计模式-1单例设计模式

单例设计模式

1.饿汉式(静态常量)(可用)

  1. 构造器私有化
  2. 类的内部建立对象
  3. 向外暴露一个静态的公共方法
class Singleton{
    //使用private防止new
    private Singleton(){
       
    }
    //类的内部建立对象
    private final static Singleton instance = new Singleton();
    //向外暴露一个静态的公共方法
    public static Singleton getInstance(){
        return instance;
    }
}

优缺点:java

优势 写法简单,在类装载的时候完成实例化,避免了线程同步问题
缺点 在类装载的时候就完成实例化,没有达到lazy Loading的效果,若是一直没有用到,会形成内存的浪费

没法保证是本身触发的静态的方法数据库

2.饿汉式(静态代码块)(可用)

class Singleton{
    //使用private防止new
    private Singleton(){
       
    }
    //类的内部建立对象
    private static Singleton instance;
    
    static{ //在静态代码块里建立
        instance = new Singleton();
    }
    
    //向外暴露一个静态的公共方法
    public static Singleton getInstance(){
        return instance;
    }
}

优缺点和静态常量同样设计模式

3.懒汉式(线程不安全)

class Singleton{
    //类的内部建立对象
    private static Singleton instance;
    //使用private防止new
    private Singleton(){

    }

    //使用的時候才建立出来 懒汉式
    public static Singleton getInstance(){
        if(instance == null){
            instance = new Singleton();
        }
        return instance;
    }
}
优势 起到了lazy Loading的效果
缺点 只能在单线程下使用,在多线程下,一个线程进入了if语句中,没来得及执行完,另外一个线程也经过了这个判断语句,这时候就会产生多个实例,因此在多线程下不能够使用和这个方式

4.懒汉式(线程安全,同步方法)

class Singleton{
    //类的内部建立对象
    private static Singleton instance;
    //使用private防止new
    private Singleton(){ }

    //加入了同步代码,解决线程不安全问题
    public static synchronized Singleton getInstance(){
        if(instance == null){
            instance = new Singleton();
        }
        return instance;
    }
}
优势 解决了线程不安全的问题
缺点 效率很低,方法只须要执行一次就能够了,可是使用这个方法须要执行不少次,都要进行同步

5.懒汉式(线程安全,同步代码块)

class Singleton{
    //类的内部建立对象
    private static Singleton instance;
    //使用private防止new
    private Singleton(){ }

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

线程不安全(有人写)不能够启到线程同步的做用(==错误==)安全

6.双重检查(推荐使用)

class Singleton{
    //volatile防止指令重排
    private static volatile Singleton instance;
    //使用private防止new
    private Singleton(){ }

    //双重检查,解决线程安全问题,实现了懒加载的方式
    public static Singleton getInstance(){
        if(instance == null){
            synchronized (Singleton.class){
                if(instance == null){
                     instance = new Singleton();
                }
            }
        }
        return instance;
    }
}
优势 Double-Check多线程中常常使用,保证了线程的安全,避免重复的方法同步,延迟加载,效率较高
结论 推荐使用

7.静态内部类(推荐使用)

class Singleton{
   
    //使用private防止new
    private Singleton(){ }

    //双重检查,解决线程安全问题,实现了懒加载的方式
    public static SingletonInstance{
       private static final Stingleton INSTANCE = new Singleton();
    }
    public static Singleton getInstance(){
       return SingletonInstance.INSTANCE;
    }
}

实现了线程安全,使用延迟加载(很好的一种模式),效率高session

静态内部类装载的时候是安全的多线程

8.枚举(推荐使用)

enum Singleton{
    INSTANCE;
    public void say(){
        System.out.println("ok");
    }
}

借助JDK1.5的枚举来实现,避免多线程同步问题,还能防止反序列化从新建立新的对象app

是Josh Bloch提倡的方式工具

9.单例模式在Runtime使用

🌰Runtime:性能

public class Runtime {
    private static Runtime currentRuntime = new Runtime();

    /**
     * Returns the runtime object associated with the current Java application.
     * Most of the methods of class <code>Runtime</code> are instance
     * methods and must be invoked with respect to the current runtime object.
     *
     * @return  the <code>Runtime</code> object associated with the current
     *          Java application.
     */
    public static Runtime getRuntime() {
        return currentRuntime;
    }

    /** Don't let anyone else instantiate this class */
    private Runtime() {}

10.单例模式注意事项

  • 单例模式保证了系统内存中该类只存在一个对象,节省了系统资源,对于一些须要频繁建立销毁的对象,使用单例模式能够提升系统性能
  • 当想实例化一个单例类的时候,必需要记住使用相应的获取对象的方法,而不是用new
  • 单例模式使用的场景:
    • 须要频繁的进行建立和销毁的对象
    • 建立对象时耗时过多或耗费资源过多(重量级对象)但又常常使用到的对象、工具类对象、频繁访问数据库或文件的对象(好比数据源、session工厂)
相关文章
相关标签/搜索