LintCode_204 Singleton
来源:互联网 发布:mac如何切换搜狗输入法 编辑:程序博客网 时间:2024/06/03 13:16
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();
定义一个静态变量来解决:
class Solution {public: /** * @return: The same instance of this class every time */ static Solution* instance; static Solution* getInstance() { if (instance == NULL) { instance = new Solution(); } return instance; }};Solution* Solution::instance = NULL;
0 0
- LintCode_204 Singleton
- Singleton
- Singleton
- singleton
- Singleton
- Singleton
- Singleton
- Singleton
- Singleton
- Singleton
- Singleton
- Singleton
- Singleton
- singleton
- Singleton
- Singleton
- Singleton
- Singleton
- 程序员的八个级别
- 如何加密/弄乱C源代码
- 51Nod-1417-天堂里的游戏
- Git学习
- LintCode_200 Longest Palindromic Substring
- LintCode_204 Singleton
- LintCode_223 Palindrome Linked List
- LintCode_249 Count of Smaller Number before itself
- LintCode_362 Trapping Rain Water
- LintCode_365 Count 1 in Binary
- make j* make j4 make j8 区别
- find the largest subsquare surrounded by ‘w’ 和‘W’
- 第56课:Spark中的Tungsten内存和CPU的优化使用
- WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!解决办法