{"id":1043,"date":"2024-05-10T00:02:53","date_gmt":"2024-05-09T16:02:53","guid":{"rendered":"http:\/\/www.spiceandwolf.com.cn\/?p=1043"},"modified":"2024-05-10T00:02:58","modified_gmt":"2024-05-09T16:02:58","slug":"design-pattern-singleton-pattern","status":"publish","type":"post","link":"http:\/\/www.spiceandwolf.com.cn\/index.php\/design-pattern-singleton-pattern\/","title":{"rendered":"\u8bbe\u8ba1\u6a21\u5f0f-\u5355\u4f8b\u6a21\u5f0f"},"content":{"rendered":"\n<p>\u5355\u4f8b\u6a21\u5f0f\u7684\u5b9a\u4e49\uff1a\u786e\u4fdd\u4e00\u4e2a\u7c7b\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u5168\u5c40\u8bbf\u95ee\u70b9\u3002<\/p>\n\n\n\n<p>\u521a\u63a5\u89e6\u5355\u4f8b\u6a21\u5f0f\uff0c\u6211\u4eec\u53ef\u80fd\u6709\u4ee5\u4e0b\u95ee\u9898\uff1a\u6211\u4eec\u4e3a\u4ec0\u4e48\u9700\u8981\u5355\u4f8b\u6a21\u5f0f\uff1f\u4ec0\u4e48\u573a\u666f\u4e0b\u9700\u8981\u5355\u4f8b\u6a21\u5f0f\uff1f\u5355\u4f8b\u6a21\u5f0f\u7684\u597d\u5904\u662f\u4ec0\u4e48\uff1f\u901a\u8fc7\u9759\u6001\u53d8\u91cf\u5b9e\u73b0\u5355\u4f8b\u4e0d\u884c\u5417\uff1f<\/p>\n\n\n\n<p>\u4ec0\u4e48\u573a\u666f\u4e0b\u9700\u8981\u5355\u4f8b\u6a21\u5f0f\uff1f\u6709\u4e9b\u5bf9\u8c61\u5728\u5e94\u7528\u4e2d\u53ea\u9700\u8981\u4e00\u4e2a\uff0c\u6bd4\u5982\u7ebf\u7a0b\u6c60\u3001\u7f13\u5b58\u3001\u5bf9\u8bdd\u6846\u3001\u6ce8\u518c\u8868\u3001\u65e5\u5fd7\u548c\u8bbe\u5907\u9a71\u52a8\u5bf9\u8c61\u7b49\uff0c\u8fd9\u4e9b\u5bf9\u8c61\u5728\u4f7f\u7528\u65f6\u5f80\u5f80\u53ea\u80fd\u5b58\u5728\u4e00\u4e2a\uff0c\u4e0d\u7136\u5982\u679c\u591a\u4e2a\u5b9e\u4f8b\u5728\u7a0b\u5e8f\u4e2d\u8fd0\u884c\u53ef\u80fd\u4f1a\u51fa\u73b0\u884c\u4e3a\u5f02\u5e38\u548c\u8d44\u6e90\u4f7f\u7528\u8fc7\u91cf\u7b49\u95ee\u9898\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7\u9759\u6001\u53d8\u91cf\u5b9e\u73b0\u5355\u4f8b\u4e0d\u884c\u5417\uff1f\u5355\u4f8b\u6a21\u5f0f\u7684\u597d\u5904\u662f\u4ec0\u4e48\uff1f<\/p>\n\n\n\n<p>\u597d\u5904\u4e4b\u4e00\u5c31\u662f\u5ef6\u8fdf\u521d\u59cb\u5316\u3002\u5f53\u4f7f\u7528\u9759\u6001\u53d8\u91cf\u65f6\u521b\u5efa\u5b9e\u4f8b\u65f6\uff0c\u4f1a\u5728\u52a0\u8f7d\u7c7b\u7684\u540c\u65f6\u8fdb\u884c\u53d8\u91cf\u7684\u521d\u59cb\u5316\uff0c\u5982\u679c\u53d8\u91cf\u7684\u521d\u59cb\u5316\u8fc7\u7a0b\u5e9e\u5927\u4e14\u8017\u65f6\uff0c\u5373\u4f7f\u6ca1\u6709\u4f7f\u7528\u53d8\u91cf\u4e5f\u4f1a\u8017\u8d39\u5927\u91cf\u7684\u7b49\u5f85\u65f6\u95f4\u3002\u800c\u5355\u4f8b\u6a21\u5f0f\u662f\u80fd\u5ef6\u8fdf\u521d\u59cb\u5316\u8fc7\u7a0b\u7684\uff0c\u53ef\u4ee5\u5728\u5b9e\u9645\u4f7f\u7528\u65f6\u5728\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u4f7f\u5bf9\u8c61\u521b\u5efa\u66f4\u4e3a\u65b9\u4fbf\u3002<\/p>\n\n\n\n<p>\u8fd9\u5c31\u662f\u6211\u4eec\u9700\u8981\u5355\u4f8b\u6a21\u5f0f\u7684\u7406\u7531\u4e4b\u4e00\uff0c\u5f53\u7136\uff0c\u5355\u4f8b\u6a21\u5f0f\u8fd8\u6709\u5176\u4ed6\u597d\u5904\uff0c\u5982\u5355\u4f8b\u6a21\u5f0f\u8fd8\u80fd\u8bbe\u7f6e\u5168\u5c40\u8bbf\u95ee\u70b9\uff0c\u8fd9\u6837\u80fd\u66f4\u597d\u7684\u7ba1\u63a7\u5355\u4f8b\u5bf9\u8c61\uff0c\u4fdd\u8bc1\u5bf9\u8c61\u4f7f\u7528\u7684\u5b89\u5168\u6027\u7b49\u3002<\/p>\n\n\n\n<p>\u5982\u4f55\u5b9e\u73b0\u5355\u4f8b\u6a21\u5f0f\uff1f<\/p>\n\n\n\n<p>\u7b2c\u4e00\u79cd\u5b9e\u73b0\u5355\u4f8b\u6a21\u5f0f\u7684\u4ee3\u7801\u5982\u4e0b\uff0c\u901a\u8fc7\u9759\u6001\u53d8\u91cf\u5c06\u5b9e\u4f8b\u4fdd\u5b58\u5728\u7c7b\u4e2d\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Singleton {\n    private static Singleton uniqueInstance;\n\n    private Singleton() {}\n\n    public static Singleton getInstance() {\n        if (uniqueInstance == null) {\n            uniqueInstance = new Singleton();\n        }\n        return uniqueInstance;\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u5355\u4f8b\u6a21\u5f0f\u8c8c\u4f3c\u6ca1\u6709\u4ec0\u4e48\u95ee\u9898\uff0c\u4f46\u662f\u5728\u591a\u7ebf\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u521b\u5efa\u5355\u4f8b\u7684\u60c5\u51b5\uff0c\u8fd9\u65f6\u5019\u4ee5\u4e0a\u4ee3\u7801\u6ca1\u6709\u591a\u7ebf\u7a0b\u7ba1\u63a7\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0\u521b\u5efa\u591a\u4e2a\u5b9e\u4f8b\u7684\u60c5\u51b5\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\u6211\u4eec\u53ef\u80fd\u9700\u8981\u8003\u8651\u5728\u5355\u4f8b\u521b\u5efa\u8fc7\u7a0b\u4e2d\u7684\u591a\u7ebf\u7a0b\u540c\u6b65\u95ee\u9898\uff0c\u91cd\u65b0\u589e\u52a0\u4e86\u65b9\u6cd5\u540c\u6b65\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Singleton {\n    private static Singleton uniqueInstance;\n\n    private Singleton() {}\n\n    public static synchronized Singleton getInstance() {\n        if (uniqueInstance == null) {\n            uniqueInstance = new Singleton();\n        }\n        return uniqueInstance;\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u5355\u4f8b\u83b7\u53d6\u4e0d\u662f\u5f88\u9891\u7e41\uff0c\u90a3\u5f53\u524d\u5355\u4f8b\u6a21\u5f0f\u4ee3\u7801\u5c31\u80fd\u5e94\u4ed8\u9700\u6c42\u3002\u4f46\u662f\u5982\u679c\u5355\u4f8b\u83b7\u53d6\u5f97\u5f88\u9891\u7e41\uff0c\u8fd9\u5c31\u9700\u8981\u8003\u8651\u540c\u6b65\u5e26\u6765\u7684\u6548\u7387\u964d\u4f4e\u95ee\u9898\u3002\u56e0\u4e3a\u6bcf\u4e2a\u9700\u8981\u83b7\u53d6\u5355\u4f8b\u7684\u7ebf\u7a0b\u90fd\u5fc5\u987b\u6392\u961f\u6765\u7b49\u5f85\u83b7\u53d6\u5355\u5b9e\u4f8b\uff0c\u6240\u4ee5\u5728\u83b7\u53d6\u5355\u4f8b\u65f6\u53ef\u80fd\u4f1a\u51fa\u73b0\u5361\u987f\uff0c\u54cd\u5e94\u6162\u7b49\u95ee\u9898\u3002\u6240\u4ee5\u5728\u9891\u7e41\u83b7\u53d6\u5355\u4f8b\uff0c\u4e14\u662f\u591a\u7ebf\u7a0b\u573a\u666f\u65f6\uff0c\u6211\u4eec\u5e94\u8be5\u5982\u4f55\u8fdb\u884c\u6539\u5584\uff1f<\/p>\n\n\n\n<p>\u65b9\u6cd51\uff1a\u4e0d\u5ef6\u8fdf\u521d\u59cb\u5316\uff0c\u76f4\u63a5\u5728\u7c7b\u52a0\u8f7d\u65f6\u521b\u5efa\u5bf9\u8c61\u3002\u5982\u679c\u5355\u4f8b\u5b9e\u4f8b\u5316\u7684\u8fc7\u7a0b\u5e76\u4e0d\u4f1a\u82b1\u8d39\u5927\u91cf\u65f6\u95f4\uff0c\u90a3\u5728\u7c7b\u52a0\u8f7d\u65f6\u76f4\u63a5\u65f6\u76f4\u63a5\u521b\u5efa\u4e5f\u662f\u6ca1\u95ee\u9898\u7684\uff0c\u8fd9\u6837\u5c31\u4e0d\u9700\u8981\u5728\u5355\u4f8b\u83b7\u53d6\u7684\u4ee3\u7801\u4e2d\u52a0\u5165\u6761\u4ef6\u5224\u65ad\u4e86\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Singleton {\n    private static Singleton uniqueInstance = new Singleton();\n\n    private Singleton() {}\n\n    public static Singleton getInstance() {\n        return uniqueInstance;\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u65b9\u6cd5\u4e8c\uff1a\u53cc\u91cd\u68c0\u67e5\u9501<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Singleton {\n    private volatile static Singleton uniqueInstance;\n\n    private Singleton() {}\n\n    public static Singleton getInstance() {\n        if (uniqueInstance == null) {\n            synchronized (Singleton.class) {\n                if (uniqueInstance == null) {\n                    uniqueInstance = new Singleton();\n                }\n            }\n        }\n        return uniqueInstance;\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u4f7f\u7528\u7684\u662f\u540c\u6b65\u53d8\u91cf\uff0c\u800c\u4e0d\u662f\u540c\u6b65\u65b9\u6cd5\uff0c\u540c\u6b65\u5757\u5185\u90e8\u7684\u8fd8\u8fdb\u884c\u4e00\u6b21\u5224\u65ad\u662f\u56e0\u4e3a\u53d8\u91cf\u7684\u521d\u59cb\u5316\u5e76\u4e0d\u662f\u539f\u5b50\u64cd\u4f5c\uff0c\u4f1a\u5206\u4e3a\u5206\u914d\u6808\u7a7a\u95f4\u548c\u521d\u59cb\u5316\u4e24\u4e2a\u8fc7\u7a0b\uff0c\u5206\u914d\u6808\u7a7a\u95f4\u540e\u53d8\u91cf\u5c31\u4e0d\u662fnull\u4e86\uff0c\u4f46\u8fd9\u65f6\u5019\u7684\u5b50\u8282\u7801\u662f\u4e4b\u524d\u5185\u5b58\u5b58\u5165\u7684\u5b50\u8282\u6570\u636e\uff0c\u6240\u4ee5\u4f1a\u4e0d\u4f1a\u6267\u884c\u540c\u6b65\u5757\u5185\u90e8if\u8bed\u53e5\u7684\u4ee3\u7801\uff0c\u8fd9\u624d\u80fd\u540c\u6b65\u5355\u4f8b\u521b\u5efa\u7684\u8fc7\u7a0b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5355\u4f8b\u6a21\u5f0f\u7684\u5b9a\u4e49\uff1a\u786e\u4fdd\u4e00\u4e2a\u7c7b\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u5168\u5c40\u8bbf\u95ee\u70b9\u3002 \u521a\u63a5\u89e6\u5355\u4f8b\u6a21\u5f0f\uff0c\u6211\u4eec\u53ef\u80fd\u6709\u4ee5\u4e0b\u95ee\u9898\uff1a\u6211\u4eec\u4e3a\u4ec0\u4e48 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[80],"tags":[],"class_list":["post-1043","post","type-post","status-publish","format-standard","hentry","category-design-pattern"],"_links":{"self":[{"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/1043","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1043"}],"version-history":[{"count":3,"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/1043\/revisions"}],"predecessor-version":[{"id":1047,"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/1043\/revisions\/1047"}],"wp:attachment":[{"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1043"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1043"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1043"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}