单例模式

什么是单例模式

  单例模式是指在内存中只会创建且仅创建一次对象的设计模式。在程序中多次使用同一个对象且作用相同时,为了防止频繁地创建对象使得内存飙升,单例模式可以让程序仅在内存中创建一个对象,让所有需要调用的地方都共享这一单例对象。

单例模式的类型

  懒汉式:在真正需要使用对象时才去创建该单例类对象。

  懒汉式创建对象的方法是在程序使用对象前,先判断该对象是否已经实例化(判空),若已实例化直接返回该类对象。,否则则先执行实例化操作。

package other;

public class LazySingle {

    private static LazySingle lazySingle = null;

    private LazySingle() {
}
public static synchronized LazySingle getLazySingle() { if (lazySingle == null) {
        //线程安全问题
        synchronized(lazySingle.class){
          if (lazySingle == null) {
              lazySingle = new LazySingle();
            }
        }
} return lazySingle; } }

  饿汉式

  饿汉式在类加载时已经创建好该对象,在程序调用时直接返回该单例对象即可,即我们在编码时就已经指明了要马上创建这个对象,不需要等到被调用时再去创建。

package other;

public class HungrySingle {

    private static final HungrySingle singleton = new HungrySingle();

    private HungrySingle(){

    }

    public static HungrySingle getSingleton(){
        return singleton;
    }
}

单例模式的优缺点

优点: 1、在内存里只有一个实例,减少了内存的开销,尤其是频繁的创建和销毁实例(比如管理学院首页页面缓存)。

    2、避免对资源的多重占用(比如写文件操作)。

缺点:没有接口,不能继承,与单一职责原则冲突,一个类应该只关心内部逻辑,而不关心外面怎么样来实例化。

单例模式的破坏

无论是饿汉式还是懒汉式,都可以通过反射和序列化进行破坏(产生多个对象)

1.反射

public static void main(String[] args) {
    // 获取类的显式构造器
    Constructor<Singleton> construct = Singleton.class.getDeclaredConstructor();
    // 可访问私有构造器
    construct.setAccessible(true); 
    // 利用反射构造新对象
    Singleton obj1 = construct.newInstance(); 
    // 通过正常方式获取单例对象
    Singleton obj2 = Singleton.getInstance(); 
    System.out.println(obj1 == obj2); // false
}

2.序列化与反序列化

public static void main(String[] args) {
    // 创建输出流
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Singleton.file"));
    // 将单例对象写到文件中
    oos.writeObject(Singleton.getInstance());
    // 从文件中读取单例对象
    File file = new File("Singleton.file");
    ObjectInputStream ois =  new ObjectInputStream(new FileInputStream(file));
    Singleton newInstance = (Singleton) ois.readObject();
    // 判断是否是同一个对象
    System.out.println(newInstance == Singleton.getInstance()); // false
}

使用枚举来实现单例模式

public enum HungrySingle{
    INSTANCE;
}
class HungrySingleTest{
    public static void main(String[] args) {
        HungrySingle instance1 = HungrySingle.INSTANCE;
        HungrySingle instance2 = HungrySingle.INSTANCE;
        System.out.println(instance1 == instance2); //true
    }
}

枚举方式属于饿汉式方式

枚举类实现单例模式是极力推荐的单例实现模式,因为枚举是线程安全的,并且只会装载一次,枚举类是所有单例类实现中唯一不会被破坏的单例模式。

 

posted @ 2023-10-01 16:43  因为在乎  阅读(16)  评论(0)    收藏  举报