码迷,mamicode.com
首页 > 其他好文 > 详细

lintcode-easy-Singleton

时间:2016-03-07 07:46:01      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:

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.

In Java:

A a = A.getInstance();
A b = A.getInstance();

a should equal to b.

 

class Solution {
    /**
     * @return: The same instance of this class every time
     */
    public Solution(){
        
    }
    
    public static Solution instance;
    
    public static Solution getInstance() {
        // write your code here
        if(instance == null){
            instance = new Solution();
            return instance;
        }
        else
            return instance;
    }
};

 

lintcode-easy-Singleton

标签:

原文地址:http://www.cnblogs.com/goblinengineer/p/5249277.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!