设计模式

82 阅读2分钟

单例模式

定义:一个对象只能创建一个实例,并且提供了对实例的全局访问方法。 单例模式

实现:

class Singleton {
    private static Singleton instance;

    public static Singleton getInstance() {
        if (instance == null)
            instance = new Singleton();
        return instance;
    }
    public void doSomething(){
        System.out.println("Something id Done");
    }
}

使用:

Singleton.getInstance().doSomething();

说明:在getInstance() 方法中,判断实例是否为空。如果为空,说明未实例化该对象,则进行实例化;否则,返回实例。

同步锁单例模式

多线程应用中使用这种模式,如果实例为空,可能存在两个线程同时调用getInstance方法的情况。如果发生这种情况,第一个线程会首先使用新构造器实例化单例对象,同时第二个线程也会检查单例实例是否为空,由于第一个线程还没完成单例对象的实例化操作,所以第二个线程会发现这个实例是空的,也会开始实例化单例对象。我们只需要创建一个代码块来检查实例是否空线程安全。可以通过以下两种方式来实现。

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

双重校验锁机制的同步锁单例模式

保证线程安全,但同时带来了延迟。用来检查实例是否被创建的代码是线程同步的,也就是说此代码块在同一时刻只能被一个线程执行,但是同步锁(locking)只有在实例没被创建的情况下才起作用。如果单例实例已经被创建了,那么任何线程都能用非同步的方式获取当前的实例。只有在单例对象未实例化的情况下,才能在synchronized代码块前添加附加条件移动线程安全锁。

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