您的位置:首页 > 编程语言 > Java开发

java单例模式

2017-08-28 13:56 337 查看

1、什么时候使用单例模式?

* 单例防止CPU、内存资源浪费;多例防止并发,例如action;

* 频繁的调用,使用单例,防止资源浪费

* 即节约资源,又便于维护,保证只有一个实例

2、java单例写法

/**
 * Returns the single instance of this class, creating it if necessary.
*
* @return the {@link PeopleInfoService} instance
*/
public static PeopleInfoService getInstance() {
if (INSTANCE == null) {
INSTANCE = new PeopleInfoService();
}
return INSTANCE;
}
/*** Used to force {@link #getInstance()} to create a new instance
* next time it's called.
*/
public static void destroyInstance() {
INSTANCE = null;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: