单列模式--代码

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;
}
}

posted on 2017-10-10 16:43  jackyu126  阅读(261)  评论(0编辑  收藏  举报

导航