首页 > 其他 > 详细

单例模式

时间:2017-02-15 12:42:50      阅读:158      评论:0      收藏:0      [点我收藏+]

单例模式的一个特点是在整个工程中只返回一个实例。

//饿汉模式

//不管你什么时候用这只“狗”,我现在就给你new出来

private static Dog dog = new Dog();

private Dog(){

}

public static Dog getDog()
{
return dog;
}

//懒汉模式

private static Dog dog = null;

private Dog(){

}

public static Dog getDog()
{

if(dog==null)
{
dog= new Dog();
}
return dog;
}

单例模式

原文:http://www.cnblogs.com/kellybaby/p/6400550.html

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