Singleton

Singleton只需把一个类的构造函数变成私有的,并且在其中增加相应的静态函数和变量,就可以把这个类变为singleton。

public class NodeFormatExtractor {
    private static NodeFormatExtractor sExtractorInstance;
    
    private NodeFormatExtractor() {
    }

    public static synchronized NodeFormatExtractor getInstance(Context context) {
        if (sExtractorInstance == null) {
            sExtractorInstance = new NodeFormatExtractor();
        }

        return sExtractorInstance;
    }
}

 

posted @ 2015-03-20 22:05  牧 天  阅读(125)  评论(0)    收藏  举报