饿汉式:
public User(){
private static User s=new User();
private User(){
}
public static User getUser()
{
return s;
}
}
懒汉式:
class User{
private static User s=null;
public User(){
if(s==null)
s=new User();
return s;
}
}
同步锁下的懒汉式:
class User{
private static User s=null;
public User(){
public static Single1 getInStanceBlock(){
if(s==null)
synchronized (User.class) {
if(s==null)
s = new User();
}
return s;
}
}
本文介绍了单例模式的三种实现方式:饿汉式、懒汉式和同步锁下的懒汉式。通过具体的Java代码示例展示了每种方式的特点及其实现过程。
2313

被折叠的 条评论
为什么被折叠?



