目录
“饿汉”模式
完整代码
“懒汉”模式
完整代码
单例模式:保证某个类在程序中只存在唯一一份实例, 而不会创建出多个实例。
单例模式可以通过实例创建的时间来分为两种:“饿汉”和“懒汉”模式。
“饿汉”模式
所谓的“饿汉”模式实则就是在类加载的时候创建出实例。
首先我们先创建一个类Singleton再在类中写一个静态私有的常量,而这个常量的值就是唯一对象的引用。
class Singleton{
    private static final Singleton singleton = new Singleton();
}因为这个唯一对象是私有的所以还需要一个get方法。
public static Singleton getSingleton() {
    return singleton;
}可是现在我们从别的地方还是可以直接new出这个类的其它实例,这个该怎么解决呢?我们只需要再写一个私有的构造方法就可以解决了。
private Singleton() {}
这就是一个简单的单例模式了(“饿汉”模式)
完整代码
class Singleton{
    private static final Singleton singleton = new Singleton();
    public static Singleton getSingleton() {
        return singleton;
    }
    private Singleton() {}
}但是由于“饿汉”模式的实例是在类加载时就创建了,并没有考虑这个实例在代码中是否使用,这就有可能会导致代码中并没有用这个类可是你却已经创建了,这就会导致内存浪费,解决办法就是“懒汉”模式。
“懒汉”模式
“懒汉”模式是在线程的一次调用该类的get方法时进行唯一实例的创建。
先创建一个类,该类中有一个私有的类属性,该属性的值为null或唯一实例的引用。
class Singleton{
    private static Singleton singleton = null;
}为了保证实例的唯一性,将构造方法写为私有的。
private Singleton() {}写一个get方法,该方法在第一次被调用时会创建出一个唯一实例。
public static Singleton getSingleton() {
    if (singleton == null) {
        singleton = new Singleton();
    }
    return singleton;
}这个get方法在单线程中看是没有任何问题的,但是如果放在多线程代码中就会出现线程安全问题,例如如果出现以下的执行顺序那么就不是单例模式了。

解决办法就是加锁
public static Singleton getSingleton() {
    synchronized (Singleton.class) {
        if (singleton == null) {
            singleton = new Singleton();
        }
    }
    return singleton;
}但是此时代码又面临了一个效率问题,由于我们只有第一次调用get时才会创建实例才会出现线程安全问题,可是现在我们每次调用get方法都会进行加锁操作,而加锁就会有锁竞争从而导致代码效率过低的问题,解决方法就是再加一层 if 判断。
public static Singleton getSingleton() {
    if (singleton == null) {
        synchronized (Singleton.class) {
            if (singleton == null) {
                singleton = new Singleton();
            }
        }
    }
    return singleton;
}因为new操作很有可能触发指令重排序,所以为了防止编译器对其进行优化建议加上volatile
private static volatile Singleton singleton = null;完整代码
class Singleton{
    private static volatile Singleton singleton = null;
    private Singleton() {}
    public static Singleton getSingleton() {
        if (singleton == null) {
            synchronized (Singleton.class) {
                if (singleton == null) {
                    singleton = new Singleton();
                }
            }
        }
        return singleton;
    }
}















![街机模拟游戏逆向工程(HACKROM)教程:[0]工具](https://img-blog.csdnimg.cn/direct/95d94f5229324ddfa99495fa80a0dca8.png)


