Singleton模式的两种实现形式

(1)第一种形式

public class AClass {

    private AClass() {
    }

    private static AClass instance = new AClass();

    public static AClass getInstance() {
        return instance;
    }
}

(1)第二种形式

public class AClass {
    private static AClass instance = null;

    public static synchronized AClass getInstance() {

        if (instance == null)
            instance = new AClass();
        return instance;
    }
}

 

 

posted @ 2013-02-12 18:40  心梦缘  阅读(245)  评论(0编辑  收藏  举报