//饿汉式 public class SingleTon { private SingleTon(){ } private static SingleTon singleTon = new SingleTon(); public static SingleTon getInstance(){ return singleTon; } }
=========================================================
//懒汉式,可能存在线程安全问题 public class SingleTon1 { private SingleTon1(){ } private static SingleTon1 singleTon = null; public static SingleTon1 getInstance(){ if (singleTon == null) { singleTon = new SingleTon1(); } return singleTon; } }
详细设计模式讲解请前往:https://blog.csdn.net/zhangerqing/article/details/8194653