单例模式

  1. 类只有唯一实例
  2. 自行创建这个实例
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class TaskManager {
  private static TaskManager tm = null;
  private TaskManager() {...}
  public static TaskManager getInstance() {
    if (tm == null) {
      tm = new TaskManager();
    }
    return tm;
  }
}