java单例模式

来源:互联网 发布:软件界面模糊 编辑:程序博客网 时间:2024/06/05 05:27

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