您的位置:首页 > 其它

[Lintcode]Singleton

2016-04-11 21:14 246 查看
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.

new时要注意防止在并发情况下被new多次。

class Solution {
/**
* @return: The same instance of this class every time
*/
private static volatile Solution so = null;
private Solution() {}

public static Solution getInstance() {
if(so == null){
synchronized (Solution.class)
so = new Solution();
}
return so;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  lintcode