单件模式:
单件模式是一种用于确保整个应用程序中只有一个类实例
且这个实例所占资源在整个应用程序中是共享时的程序设计方法
package single;
//单件模式 ,只能创建一个对象
//构造类私有或者被保护权限
//static 当前对象
//一个方法 static getInstance()创建唯一的实例
public class Log {
private Log(){
System.out.println("单件");
}
static Log tneOnlyInstance = null;
static Log getInstance(){
if(null == tneOnlyInstance){
tneOnlyInstance = new Log();
}
return tneOnlyInstance;
}
}
package single;
public class application {
public static void main(String[] args) {
System.out.println("单件模式");
//通过类名调用方法
Log.getInstance();
}
}