设计模式-单例模式

详细讲解请看:http://www.cnblogs.com/cielosun/p/6582333.html

饿汉模式:

public class Singleton {
private static Singleton instance = new Singleton();

private Singleton(){}

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

懒汉模式:
public class Singleton {
private static Singleton instance;

private Singleton() {
}

public static Singleton getInstance() {
if (instance == null)
return new Singleton();
else return instance;
}
}

加锁的的单例:
public class Singleton {
private static Singleton instance;

private Singleton() {
}

public static synchronized Singleton getInstance() {
if (instance == null)
return new Singleton();
else return instance;
}
}
posted @ 2017-10-30 11:11  诺-诺  阅读(156)  评论(0编辑  收藏  举报