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;
}