/**考察知识点单例及Spring Bean的单例设计*/
class Singleton01{
private Singleton01() {}
private static Singleton01 instance;
public static Singleton01 getInstance() {
if(instance==null) {
instance=new Singleton01();
}
return instance;
}
}
class Singleton02{//适合大对象,稀少用
private Singleton02() {}
private static Singleton02 instance;
public synchronized static Singleton02 getInstance() {
if(instance==null) {
instance=new Singleton02();
}
return instance;
}
}
class Singleton03{//适合大对象(合适需要合适加载),稀少用
private Singleton03() {}
private static volatile Singleton03 instance;
public static Singleton03 getInstance() {
if(instance==null) {
synchronized (Singleton03.class) {
if(instance==null) {
instance=new Singleton03();
}
}
}
return instance;
}
}
class Singleton04{//小对象,频繁用
//int[] n=new int[1024];
private Singleton04() {}
private static Singleton04 instance=new Singleton04();
public static Singleton04 getInstance() {
return instance;
}
public static void display() {}
}
class Singleton05{//对04进行优化
private Singleton05() {}
static class Inner{//延迟加载策略
private static Singleton05 instance=
new Singleton05();
}
public static Singleton05 getInstance() {
return Inner.instance;
}
public static void display() {}
public void show() {}
}
enum Singleton06{
instance;
public static void display() {}
}
class Singleton01{
private Singleton01() {}
private static Singleton01 instance;
public static Singleton01 getInstance() {
if(instance==null) {
instance=new Singleton01();
}
return instance;
}
}
class Singleton02{//适合大对象,稀少用
private Singleton02() {}
private static Singleton02 instance;
public synchronized static Singleton02 getInstance() {
if(instance==null) {
instance=new Singleton02();
}
return instance;
}
}
class Singleton03{//适合大对象(合适需要合适加载),稀少用
private Singleton03() {}
private static volatile Singleton03 instance;
public static Singleton03 getInstance() {
if(instance==null) {
synchronized (Singleton03.class) {
if(instance==null) {
instance=new Singleton03();
}
}
}
return instance;
}
}
class Singleton04{//小对象,频繁用
//int[] n=new int[1024];
private Singleton04() {}
private static Singleton04 instance=new Singleton04();
public static Singleton04 getInstance() {
return instance;
}
public static void display() {}
}
class Singleton05{//对04进行优化
private Singleton05() {}
static class Inner{//延迟加载策略
private static Singleton05 instance=
new Singleton05();
}
public static Singleton05 getInstance() {
return Inner.instance;
}
public static void display() {}
public void show() {}
}
enum Singleton06{
instance;
public static void display() {}
}