{"id":211,"date":"2014-08-29T10:40:53","date_gmt":"2014-08-29T02:40:53","guid":{"rendered":"http:\/\/www.codeeel.com\/?p=211"},"modified":"2014-08-29T10:45:29","modified_gmt":"2014-08-29T02:45:29","slug":"%e5%8d%95%e4%be%8b%e6%a8%a1%e5%bc%8f%e7%9a%84%e4%b8%83%e7%a7%8d%e5%86%99%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.codeeel.com\/?p=211","title":{"rendered":"\u8bbe\u8ba1\u6a21\u5f0f-\u5355\u4f8b\u6a21\u5f0f\u7684\u4e03\u79cd\u5199\u6cd5"},"content":{"rendered":"<p><strong>\u7b2c\u4e00\u79cd\uff08\u61d2\u6c49\uff0c\u7ebf\u7a0b\u4e0d\u5b89\u5168\uff09\uff1a<\/strong><\/p>\n<pre class=\"lang:default decode:true\">public class Singleton {  \r\n    private static Singleton instance;  \r\n    private Singleton (){}  \r\n\r\n    public static Singleton getInstance() {  \r\n    if (instance == null) {  \r\n        instance = new Singleton();  \r\n    }  \r\n    return instance;  \r\n    }  \r\n}<\/pre>\n<p>\u8fd9\u79cd\u5199\u6cd5lazy loading\u5f88\u660e\u663e\uff0c\u4f46\u662f\u81f4\u547d\u7684\u662f\u5728\u591a\u7ebf\u7a0b\u4e0d\u80fd\u6b63\u5e38\u5de5\u4f5c\u3002<\/p>\n<p><strong>\u7b2c\u4e8c\u79cd\uff08\u61d2\u6c49\uff0c\u7ebf\u7a0b\u5b89\u5168\uff09\uff1a<\/strong><\/p>\n<pre class=\"lang:default decode:true\">public class Singleton {  \r\n    private static Singleton instance;  \r\n    private Singleton (){}  \r\n    public static synchronized Singleton getInstance() {  \r\n    if (instance == null) {  \r\n        instance = new Singleton();  \r\n    }  \r\n    return instance;  \r\n    }  \r\n}<\/pre>\n<p>\u8fd9\u79cd\u5199\u6cd5\u80fd\u591f\u5728\u591a\u7ebf\u7a0b\u4e2d\u5f88\u597d\u7684\u5de5\u4f5c\uff0c\u800c\u4e14\u770b\u8d77\u6765\u5b83\u4e5f\u5177\u5907\u5f88\u597d\u7684lazy loading\uff0c\u4f46\u662f\uff0c\u9057\u61be\u7684\u662f\uff0c\u6548\u7387\u5f88\u4f4e\uff0c99%\u60c5\u51b5\u4e0b\u4e0d\u9700\u8981\u540c\u6b65\u3002<\/p>\n<p><strong>\u7b2c\u4e09\u79cd\uff08\u997f\u6c49\uff09\uff1a<\/strong><\/p>\n<pre class=\"lang:default decode:true\">public class Singleton {  \r\n    private static Singleton instance = new Singleton();  \r\n    private Singleton (){}  \r\n    public static Singleton getInstance() {  \r\n    return instance;  \r\n    }  \r\n}<\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u57fa\u4e8eclassloder\u673a\u5236\u907f\u514d\u4e86\u591a\u7ebf\u7a0b\u7684\u540c\u6b65\u95ee\u9898\uff0c\u4e0d\u8fc7\uff0cinstance\u5728\u7c7b\u88c5\u8f7d\u65f6\u5c31\u5b9e\u4f8b\u5316\uff0c\u867d\u7136\u5bfc\u81f4\u7c7b\u88c5\u8f7d\u7684\u539f\u56e0\u6709\u5f88\u591a\u79cd\uff0c\u5728\u5355\u4f8b\u6a21\u5f0f\u4e2d\u5927\u591a\u6570\u90fd\u662f\u8c03\u7528getInstance\u65b9\u6cd5\uff0c\u00a0\u4f46\u662f\u4e5f\u4e0d\u80fd\u786e\u5b9a\u6709\u5176\u4ed6\u7684\u65b9\u5f0f\uff08\u6216\u8005\u5176\u4ed6\u7684\u9759\u6001\u65b9\u6cd5\uff09\u5bfc\u81f4\u7c7b\u88c5\u8f7d\uff0c\u8fd9\u65f6\u5019\u521d\u59cb\u5316instance\u663e\u7136\u6ca1\u6709\u8fbe\u5230lazy loading\u7684\u6548\u679c\u3002<\/p>\n<p><strong>\u7b2c\u56db\u79cd\uff08<strong>\u997f<\/strong>\u6c49\uff0c\u53d8\u79cd\uff09\uff1a<\/strong><\/p>\n<pre class=\"lang:default decode:true\">public class Singleton {  \r\n    private Singleton instance = null;  \r\n    static {  \r\n    instance = new Singleton();  \r\n    }  \r\n    private Singleton (){}  \r\n    public static Singleton getInstance() {  \r\n    return this.instance;  \r\n    }  \r\n}<\/pre>\n<p>\u8868\u9762\u4e0a\u770b\u8d77\u6765\u5dee\u522b\u633a\u5927\uff0c\u5176\u5b9e\u66f4\u7b2c\u4e09\u79cd\u65b9\u5f0f\u5dee\u4e0d\u591a\uff0c\u90fd\u662f\u5728\u7c7b\u521d\u59cb\u5316\u5373\u5b9e\u4f8b\u5316instance\u3002<\/p>\n<p><strong>\u7b2c\u4e94\u79cd\uff08\u9759\u6001\u5185\u90e8\u7c7b\uff09\uff1a<\/strong><\/p>\n<pre class=\"lang:default decode:true\">public class Singleton {  \r\n    private static class SingletonHolder {  \r\n    private static final Singleton INSTANCE = new Singleton();  \r\n    }  \r\n    private Singleton (){}  \r\n    public static final Singleton getInstance() {  \r\n    return SingletonHolder.INSTANCE;  \r\n    }  \r\n}<\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u540c\u6837\u5229\u7528\u4e86classloder\u7684\u673a\u5236\u6765\u4fdd\u8bc1\u521d\u59cb\u5316instance\u65f6\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u5b83\u8ddf\u7b2c\u4e09\u79cd\u548c\u7b2c\u56db\u79cd\u65b9\u5f0f\u4e0d\u540c\u7684\u662f\uff08\u5f88\u7ec6\u5fae\u7684\u5dee\u522b\uff09\uff1a\u7b2c\u4e09\u79cd\u548c\u7b2c\u56db\u79cd\u65b9\u5f0f\u662f\u53ea\u8981Singleton\u7c7b\u88ab\u88c5\u8f7d\u4e86\uff0c\u90a3\u4e48instance\u5c31\u4f1a\u88ab\u5b9e\u4f8b\u5316\uff08\u6ca1\u6709\u8fbe\u5230lazy loading\u6548\u679c\uff09\uff0c\u800c\u8fd9\u79cd\u65b9\u5f0f\u662fSingleton\u7c7b\u88ab\u88c5\u8f7d\u4e86\uff0cinstance\u4e0d\u4e00\u5b9a\u88ab\u521d\u59cb\u5316\u3002\u56e0\u4e3aSingletonHolder\u7c7b\u6ca1\u6709\u88ab\u4e3b\u52a8\u4f7f\u7528\uff0c\u53ea\u6709\u663e\u793a\u901a\u8fc7\u8c03\u7528getInstance\u65b9\u6cd5\u65f6\uff0c\u624d\u4f1a\u663e\u793a\u88c5\u8f7dSingletonHolder\u7c7b\uff0c\u4ece\u800c\u5b9e\u4f8b\u5316instance\u3002\u60f3\u8c61\u4e00\u4e0b\uff0c\u5982\u679c\u5b9e\u4f8b\u5316instance\u5f88\u6d88\u8017\u8d44\u6e90\uff0c\u6211\u60f3\u8ba9\u4ed6\u5ef6\u8fdf\u52a0\u8f7d\uff0c\u53e6\u5916\u4e00\u65b9\u9762\uff0c\u6211\u4e0d\u5e0c\u671b\u5728Singleton\u7c7b\u52a0\u8f7d\u65f6\u5c31\u5b9e\u4f8b\u5316\uff0c\u56e0\u4e3a\u6211\u4e0d\u80fd\u786e\u4fddSingleton\u7c7b\u8fd8\u53ef\u80fd\u5728\u5176\u4ed6\u7684\u5730\u65b9\u88ab\u4e3b\u52a8\u4f7f\u7528\u4ece\u800c\u88ab\u52a0\u8f7d\uff0c\u90a3\u4e48\u8fd9\u4e2a\u65f6\u5019\u5b9e\u4f8b\u5316instance\u663e\u7136\u662f\u4e0d\u5408\u9002\u7684\u3002\u8fd9\u4e2a\u65f6\u5019\uff0c\u8fd9\u79cd\u65b9\u5f0f\u76f8\u6bd4\u7b2c\u4e09\u548c\u7b2c\u56db\u79cd\u65b9\u5f0f\u5c31\u663e\u5f97\u5f88\u5408\u7406\u3002<\/p>\n<p><strong>\u7b2c\u516d\u79cd\uff08\u679a\u4e3e\uff09\uff1a<\/strong><\/p>\n<pre class=\"lang:default decode:true\">public enum Singleton {  \r\n    INSTANCE;  \r\n    public void whateverMethod() {  \r\n    }  \r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u662fEffective Java\u4f5c\u8005Josh Bloch \u63d0\u5021\u7684\u65b9\u5f0f\uff0c\u5b83\u4e0d\u4ec5\u80fd\u907f\u514d\u591a\u7ebf\u7a0b\u540c\u6b65\u95ee\u9898\uff0c\u800c\u4e14\u8fd8\u80fd\u9632\u6b62\u53cd\u5e8f\u5217\u5316\u91cd\u65b0\u521b\u5efa\u65b0\u7684\u5bf9\u8c61\uff0c\u53ef\u8c13\u662f\u5f88\u575a\u5f3a\u7684\u58c1\u5792\u554a\uff0c\u4e0d\u8fc7\uff0c\u4e2a\u4eba\u8ba4\u4e3a\u7531\u4e8e1.5\u4e2d\u624d\u52a0\u5165enum\u7279\u6027\uff0c\u7528\u8fd9\u79cd\u65b9\u5f0f\u5199\u4e0d\u514d\u8ba9\u4eba\u611f\u89c9\u751f\u758f\uff0c\u5728\u5b9e\u9645\u5de5\u4f5c\u4e2d\uff0c\u6211\u4e5f\u5f88\u5c11\u770b\u89c1\u6709\u4eba\u8fd9\u4e48\u5199\u8fc7\u3002<\/p>\n<p><strong>\u7b2c\u4e03\u79cd\uff08\u53cc\u91cd\u6821\u9a8c\u9501\uff09\uff1a<\/strong><\/p>\n<pre class=\"lang:default decode:true\">public class Singleton {  \r\n    private volatile static Singleton singleton;  \r\n    private Singleton (){}  \r\n    public static Singleton getSingleton() {  \r\n    if (singleton == null) {  \r\n        synchronized (Singleton.class) {  \r\n        if (singleton == null) {  \r\n            singleton = new Singleton();  \r\n        }  \r\n        }  \r\n    }  \r\n    return singleton;  \r\n    }  \r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>\u8fd9\u4e2a\u662f\u7b2c\u4e8c\u79cd\u65b9\u5f0f\u7684\u5347\u7ea7\u7248\uff0c\u4fd7\u79f0\u53cc\u91cd\u68c0\u67e5\u9501\u5b9a\uff0c\u8be6\u7ec6\u4ecb\u7ecd\u8bf7\u67e5\u770b\uff1a<a href=\"http:\/\/www.ibm.com\/developerworks\/cn\/java\/j-dcl.html\">http:\/\/www.ibm.com\/developerworks\/cn\/java\/j-dcl.html<\/a><\/p>\n<p>\u5728JDK1.5\u4e4b\u540e\uff0c\u53cc\u91cd\u68c0\u67e5\u9501\u5b9a\u624d\u80fd\u591f\u6b63\u5e38\u8fbe\u5230\u5355\u4f8b\u6548\u679c\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u603b\u7ed3<\/strong><\/p>\n<p>\u6709\u4e24\u4e2a\u95ee\u9898\u9700\u8981\u6ce8\u610f\uff1a<\/p>\n<p>1.\u5982\u679c\u5355\u4f8b\u7531\u4e0d\u540c\u7684\u7c7b\u88c5\u8f7d\u5668\u88c5\u5165\uff0c\u90a3\u4fbf\u6709\u53ef\u80fd\u5b58\u5728\u591a\u4e2a\u5355\u4f8b\u7c7b\u7684\u5b9e\u4f8b\u3002\u5047\u5b9a\u4e0d\u662f\u8fdc\u7aef\u5b58\u53d6\uff0c\u4f8b\u5982\u4e00\u4e9bservlet\u5bb9\u5668\u5bf9\u6bcf\u4e2aservlet\u4f7f\u7528\u5b8c\u5168\u4e0d\u540c\u7684\u7c7b\u88c5\u8f7d\u5668\uff0c\u8fd9\u6837\u7684\u8bdd\u5982\u679c\u6709\u4e24\u4e2aservlet\u8bbf\u95ee\u4e00\u4e2a\u5355\u4f8b\u7c7b\uff0c\u5b83\u4eec\u5c31\u90fd\u4f1a\u6709\u5404\u81ea\u7684\u5b9e\u4f8b\u3002<\/p>\n<p>2.\u5982\u679cSingleton\u5b9e\u73b0\u4e86java.io.Serializable\u63a5\u53e3\uff0c\u90a3\u4e48\u8fd9\u4e2a\u7c7b\u7684\u5b9e\u4f8b\u5c31\u53ef\u80fd\u88ab\u5e8f\u5217\u5316\u548c\u590d\u539f\u3002\u4e0d\u7ba1\u600e\u6837\uff0c\u5982\u679c\u4f60\u5e8f\u5217\u5316\u4e00\u4e2a\u5355\u4f8b\u7c7b\u7684\u5bf9\u8c61\uff0c\u63a5\u4e0b\u6765\u590d\u539f\u591a\u4e2a\u90a3\u4e2a\u5bf9\u8c61\uff0c\u90a3\u4f60\u5c31\u4f1a\u6709\u591a\u4e2a\u5355\u4f8b\u7c7b\u7684\u5b9e\u4f8b\u3002<\/p>\n<p>\u5bf9\u7b2c\u4e00\u4e2a\u95ee\u9898\u4fee\u590d\u7684\u529e\u6cd5\u662f\uff1a<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7b2c\u4e00\u79cd\uff08\u61d2\u6c49\uff0c\u7ebf\u7a0b\u4e0d\u5b89\u5168\uff09\uff1a public class Singleton { private static Singleton instance; private Singleton (){} public static Singleton getInstance() { if (instance == null) <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[125],"_links":{"self":[{"href":"https:\/\/www.codeeel.com\/index.php?rest_route=\/wp\/v2\/posts\/211"}],"collection":[{"href":"https:\/\/www.codeeel.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codeeel.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codeeel.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codeeel.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=211"}],"version-history":[{"count":3,"href":"https:\/\/www.codeeel.com\/index.php?rest_route=\/wp\/v2\/posts\/211\/revisions"}],"predecessor-version":[{"id":218,"href":"https:\/\/www.codeeel.com\/index.php?rest_route=\/wp\/v2\/posts\/211\/revisions\/218"}],"wp:attachment":[{"href":"https:\/\/www.codeeel.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codeeel.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codeeel.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}