java 设计模式之单例模式:
java 设计模式 单例模式:
1. private class Singleton{
private Singleton(){
//create information
}
private static Singleton singleton = new Singleton();
public static Singleton getInstance(){
return singleton;
}
}
类加载时候初始化创建类,没有实现延迟加载
2.
private class Singleton{
private Singleton(){
//create information
}
private static Singleton singleton = null;
public synchronized static Singleton getInstance(){
return singleton;
}
}
实现了延迟加载 ,性能不好
3.
private class Singleton{
private Singleton(){
//create information
}
private static class CreateSingleton{
private static Singleton instance = new Single();
}
private static Singleton singleton = new Singleton();
public static Singleton getInstance(){
return Singleton.instance;
}
}
完美实现延时加载,多线程还不影响性能
java 设计模式 单例模式:
1. private class Singleton{
private Singleton(){
//create information
}
private static Singleton singleton = new Singleton();
public static Singleton getInstance(){
return singleton;
}
}
类加载时候初始化创建类,没有实现延迟加载
2.
private class Singleton{
private Singleton(){
//create information
}
private static Singleton singleton = null;
public synchronized static Singleton getInstance(){
return singleton;
}
}
实现了延迟加载 ,性能不好
3.
private class Singleton{
private Singleton(){
//create information
}
private static class CreateSingleton{
private static Singleton instance = new Single();
}
private static Singleton singleton = new Singleton();
public static Singleton getInstance(){
return Singleton.instance;
}
}
完美实现延时加载,多线程还不影响性能