首页 > 其他 > 详细

单利模式

时间:2018-05-05 16:23:06      阅读:191      评论:0      收藏:0      [点我收藏+]

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

/*
*
// 单利饿汉式
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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!