Singleton is a most widely used design pattern. If a class has and only has one instance at every moment, we call this design as singleton. For example, for class Mouse (not a animal mouse), we should design it in singleton.

You job is to implement a getInstance method for given class, return the same instance of this class every time you call this method.

Example
In Java: A a = A.getInstance();
A b = A.getInstance();
a should equal to b. Challenge
If we call getInstance concurrently, can you make sure your code could run correctly?

单例模式,这是一道OOD的题

Eager initialization

This is a design pattern where an instance of a class is created much before it is actually required. Mostly it is done on system start up. In singleton pattern, it refers to create the singleton instance irrespective of whether any other class actually asked for its instance or not.

 public class EagerSingleton {
private static volatile EagerSingleton instance = new EagerSingleton(); // private constructor
private EagerSingleton() {
} public static EagerSingleton getInstance() {
return instance;
}
}

Above method works fine, but has one drawback. Instance is created irrespective of it is required in runtime or not. If this instance is not big object and you can live with it being unused, this is best approach.

Lets solve above problem in next method.

Lazy initialization

In computer programming, lazy initialization is the tactic of delaying the creation of an object, the calculation of a value, or some other expensive process until the first time it is needed. In singleton pattern, it restricts the creation of instance until requested first time. Lets see in code:

 public final class LazySingleton {
private static volatile LazySingleton instance = null; // private constructor
private LazySingleton() {
} public static LazySingleton getInstance() {
if (instance == null) {
synchronized (LazySingleton.class) {
instance = new LazySingleton();
}
}
return instance;
}
}

On first invocation, above method will check if instance is already created using instance variable. If there is no instance i.e. instance is null, it will create an instance and will return its reference. If instance is already created, it will simply return the reference of instance.

But, this method also has its own drawbacks. Lets see how. Suppose there are two threads T1 and T2. Both comes to create instance and execute “instance==null”, now both threads have identified instance variable to null thus assume they must create an instance. They sequentially goes to synchronized block and create the instances. At the end, we have two instances in our application.

This error can be solved using double-checked locking. This principle tells us to recheck the instance variable again in synchronized block in given below way:

Double-Checking Locking: (correct version)

 public class EagerSingleton {
private static volatile EagerSingleton instance = null; // private constructor
private EagerSingleton() {
} public static EagerSingleton getInstance() {
if (instance == null) {
synchronized (EagerSingleton.class) {
// Double check
if (instance == null) {
instance = new EagerSingleton();
}
}
}
return instance;
}
}

最新文章

  1. PlayerPrefs游戏存档
  2. Lucene-Analyzer
  3. jmeter随笔(7)--查看请求响应,为空
  4. Xamarin.Android 入门之:Android的生命周期
  5. 浅谈C#关于AOP编程的学习总结
  6. 《Java程序员面试笔试宝典》之Java与C/C++有什么异同
  7. 字节、字、bit、Byte、byte的关系区分
  8. python 识别验证码
  9. TensorFlow迭代速度变慢的问题
  10. ubuntu拒绝root用户ssh远程登录解决办法
  11. BZOJ5091 摘苹果 BZOJ2017年11月月赛 概率,期望
  12. java.util.Queue(队列)的简单使用
  13. java 中多播、广播编程
  14. 异常检测(Anomaly Detection)
  15. debian7(wheezy)升级安装mercurial hg最新版2.8-RC,解决tortoisehg2.9.2不能使用。
  16. CentOS 6.5 安装和使用Gitlab
  17. HTML5 <iframe> 标签
  18. 关于使用jquery的Ajax结合java的Servlet后台判定用户名是否存在
  19. 按的第一个greasemonkey插件:评论时可以粘贴啦~~
  20. runloop与线程的关系

热门文章

  1. Andrew Ng机器学习公开课笔记–Independent Components Analysis
  2. Python之 continue继续循环和多重循环
  3. Solr4.3之检索建议suggest
  4. SVN hooks强制提交时填写日志
  5. cdecl和stdcall调用约定-汇编演示
  6. Linux GDB调试全面解析
  7. JS实现操作成功定时回到主页效果
  8. RTSP协议详解
  9. Openmpi 编译安装+集群配置 + Ubuntu14.04 + SSH无密码连接 + NFS共享文件系统
  10. 提取日志中的json请求发送到另外一台机器