单列模式--代码

package com.test.interfaces;
public class Cnblog04 {
//单列模式--懒汉
private static Cnblog04 instance;
private Cnblog04(){};
public static Cnblog04 getInstance(){
if(instance == null){
instance = new Cnblog04();
}
return instance;
}
}
//懒汉,线程安全的
class SingleTon{
private static SingleTon instance;
private SingleTon(){};
public static synchronized SingleTon getInstance(){
if(instance == null){
instance = new SingleTon();
}
return instance;
}
}
//饿汉
class Singleton{
private static Singleton instance = new Singleton();
private Singleton(){};
public static synchronized Singleton getInstance(){
return instance;
}
}

原文地址:https://www.cnblogs.com/jackyu126/p/7646484.html