单例模式

2020-05-16  本文已影响0人  俊果果

一、简单介绍

类的单例设计模式,就是采取一定的方法保证在整个软件系统中,对某个类只能存在一个对象实例,
并且该类只提供一个取得其对象实例的静态方法
reference documentJava类加载机制(全套)

二、实现方法

1、饿汉式(静态常量)

a)、代码示例

//饿汉式(静态变量)
class Singleton {

    //2.本类内部创建对象实例
    private final static Singleton instance = new Singleton();

    //1. 构造器私有化, 外部能new
    private Singleton() {
    }

    //3. 提供一个公有的静态方法,返回实例对象
    public static Singleton getInstance() {
        return instance;
    }
}

b)、优缺点说明

c)、总结

这种单例模式可用,但可能造成内存浪费


2、饿汉式(静态代码块)

a)、代码示例

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

b)、优缺点说明

c)、总结

这种单例模式可用,但是可能造成内存浪费


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

a)、代码示例

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

b)、优缺点说明

c)、总结

在实际开发中,不要使用这种方式


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

a)、代码示例

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

b)、优缺点说明

c)、总结

在实际开发中,不要使用这种方式


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

a)、代码示例

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

b)、优缺点说明

c)、总结

在实际开发中,不要使用这种方式


6、 双重检查

a)、代码示例

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

b)、优缺点说明

c)、总结

在实际开发中,推荐使用这种单例设计模式


7、 静态内部类

a)、代码示例

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

b)、优缺点说明

c)、总结

避免了线程不安全,利用静态内部类特点实现延迟加载,效率高。
在实际开发中,推荐使用这种单例设计模式


8、 枚举

a)、代码示例

//使用枚举,可以实现单例, 推荐
enum Singleton {
    INSTANCE; //属性
    public void sayOK() {
        System.out.println("ok~");
    }
}

b)、优缺点说明

c)、总结

可以使用

三、总结

上一篇 下一篇

猜你喜欢

热点阅读