单利模式

it2025-02-01  7

单利模式,饿汉式与赖汉式写法,私有构造器保证了类在其他地方不能被实例化只能通过公用方法实例化对象。而懒汉式需要保证对象线程安全,否则会出现有多个对象的情况。

/* * // 单利饿汉式 private static Single instance = new Single(); private Single (){} public static Single getInstance(){ return instance; }*/ /** * 单利赖汉式 */ private static Single instance = null; private Single (){} public static synchronized Single getInstance(){ if(null == instance){ instance = new Single(); } return instance; }

转载于:https://www.cnblogs.com/shuanglin/p/8995011.html

相关资源:Kotlin中的5种单例模式示例详解
最新回复(0)