{"id":751,"date":"2025-05-11T15:25:57","date_gmt":"2025-05-11T07:25:57","guid":{"rendered":"https:\/\/www.notown.top\/?p=751"},"modified":"2025-05-22T19:28:14","modified_gmt":"2025-05-22T11:28:14","slug":"%e5%8d%95%e4%be%8b%e6%a8%a1%e5%bc%8f%e5%92%8c%e4%ba%ab%e5%85%83%e6%a8%a1%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/www.notown.top\/?p=751","title":{"rendered":"\u5355\u4f8b\u6a21\u5f0f\u548c\u4eab\u5143\u6a21\u5f0f"},"content":{"rendered":"<h2>\u5355\u4f8b\u6a21\u5f0f\uff08Singleton\uff09<\/h2>\n<p>\u5728\u8f6f\u4ef6\u7cfb\u7edf\u4e2d\uff0c\u7ecf\u5e38\u6709\u8fd9\u6837\u4e00\u4e9b\u7279\u6b8a\u7684\u7c7b\uff0c\u5fc5\u987b\u4fdd\u8bc1\u5b83\u4eec\u5728\u7cfb\u7edf\u4e2d\u53ea\u5b58\u5728\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u624d\u80fd\u786e\u4fdd\u5b83\u4eec\u7684\u903b\u8f91\u6b63\u786e\u6027\u4ee5\u53ca\u826f\u597d\u7684\u6548\u7387<\/p>\n<pre><code class=\"language-cpp line-numbers\">\/\/ \u997f\u6c49\u5f0f\uff1a\nclass SingletonHungry{\nprivate:\n    SingletonHungry() {}\n    static SingletonHungry *instance;\npublic:\n\n    SingletonHungry(const SingletonHungry *other) = delete;\n    static SingletonHungry *getInstance(){\n        return instance;\n    }\n};\n\nSingletonHungry *SingletonHungry::instance = new SingletonHungry();\n\n\/\/ \u61d2\u6c49\u5f0f\nclass Singleton{\nprivate:\n    Singleton(); \/\/ \u6784\u9020\u51fd\u6570\n    Singleton(const Singleton &amp;other); \/\/\u62f7\u8d1d\u6784\u9020 \u58f0\u660e\u4e3a\u79c1\u6709\u6216=delete\u90fd\u53ef\u4ee5\u4e0d\u5141\u8bb8\u5916\u90e8\u8c03\u7528\npublic:\n    static Singleton *getInstance();\n    static Singleton *m_instance;\n};\nSingleton* Singleton::m_instance = nullptr;\n\n\/\/ \u5b9e\u73b0getInstance\u7684\u591a\u4e2a\u7248\u672c\uff1a\n\n\/\/ \u5355\u7ebf\u7a0b\u7248\u672c\nSingleton* Singleton::getInstance(){\n    if (m_instance == nullptr){\n        m_instance = new Singleton();\n    }\n    return m_instance;\n}\n\n\/\/ \u7ebf\u7a0b\u5b89\u5168\u7248\u672c\uff08\u4f46\u5982\u679c\u5728\u9ad8\u5e76\u53d1\u7684\u9700\u6c42\u91cc\uff0c\u4ee3\u4ef7\u4f1a\u5f88\u5927\uff0c\u5904\u7406\u53d8\u6162\uff09\nSingleton* Singleton::getInstance(){\n    Lock lock; \/\/ \u52a0\u9501\uff0c\u5c40\u90e8\u53d8\u91cf\uff0c\u79bb\u5f00\u4f5c\u7528\u57df\u540e\u81ea\u52a8\u91ca\u653e\n    if (m_instance == nullptr){\n        m_instance = new Singleton();\n    }\n    return m_instance;\n}\n\n\/\/ \u53cc\u68c0\u67e5\u9501\uff08\u4f46\u56e0\u4e3a\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u51fa\u4e8e\u4f18\u5316\u9700\u8981\uff0creorder\u5b9e\u9645\u8fd0\u884c\u7684\u6b65\u9aa4\uff0c\u6240\u4ee5\u4f1a\u5b58\u5728\u95ee\u9898\uff09\nSingleton* Singleton::getInstance(){\n    if (m_instance == nullptr){\n        Lock lock;\n        if (m_instance == nullptr){\n            \/\/ \u6211\u4eec\u9884\u60f3\u8fd9\u6761\u6307\u4ee4\u7684\u4e09\u4e2a\u6b65\u9aa4 1.\u5206\u914d\u4e00\u6bb5\u5185\u5b58 2.\u6267\u884c\u6784\u9020\uff0c\u521d\u59cb\u5316 3.\u8fd4\u56de\u5185\u5b58\u5730\u5740\n            m_instance = new Singleton(); \n        }\/\/ \u4f46\u5b9e\u9645\u4e0a\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u63091\u30013\u30012\u6267\u884c\uff0c\u5982\u679c\u5728\u6267\u884c\u52303\u8fd8\u6ca1\u6267\u884c2\u7684\u8fc7\u7a0b\u4e2d\u5207\u4e86\u522b\u7684\u7ebf\u7a0b\u6267\u884cgetInstance\n        \/\/\u90a3\u4e48\u4f1a\u5bfc\u81f4m_instance\u4e0d\u4e3a\u7a7a\u6307\u9488\uff0c\u4f46\u6307\u5411\u7684\u5185\u5b58\u65e0\u6548\uff0c\u5bfc\u81f4\u540e\u7eed\u4ee3\u7801\u62a5\u9519\n    }\n    return m_instance;\n}\n\n\n\/\/ C++ 11\u540e\u7684\u8de8\u5e73\u53f0\u5b9e\u73b0\nstd::atomic&lt;Singleton *&gt; Singleton::m_instance;\nstd::mutex Singleton::m_mutex;\n\nSingleton* Singleton::getInstance(){\n    Singleton *tmp = m_instance.load(std::memory_order_relaxed);\n    std::atomic_thread_fence(std::memory_order_acquire); \/\/ \u83b7\u53d6\u5185\u5b58fence\n    if (tmp == nullptr){\n        std::lock_guard&lt;std::mutex&gt; lock(m_mutex);\n        tmp = m_instance.load(std::memory_order_relaxed);\n        if (tmp == nullptr){\n            tmp = new Singleton();\n            std::atomic_thread_fence(std::memory_order_release);\/\/ \u91ca\u653e\u5185\u5b58fence\n            m_instance.store(tmp, std::memory_order_relaxed);\n        }\n    }\n    return tmp\n}\n\n\/\/ \u65b0\u589e\n\/\/ \u6216\u8005\u7528volatile\u5b9e\u73b0\u4f1a\u66f4\u7b80\u5355\uff08\u4f46\u597d\u50cf\u4e0d\u80fd\u8de8\u5e73\u53f0\uff1f\uff09\n\/\/ \u61d2\u6c49\u5f0f\uff08double check + volatile\u7248\uff09\uff1a\nclass SingletonLazyDoubeCheck{\nprivate:\n    SingletonLazyDoubeCheck() {}\n    static SingletonLazyDoubeCheck *instance;\n    static std::mutex mtx;\npublic:\n    SingletonLazyDoubeCheck(const SingletonLazyDoubeCheck &amp;other) = delete;\n\n    static SingletonLazyDoubeCheck *getInstance(){\n        if (instance == nullptr){\n            std::unique_lock&lt;std::mutex&gt; lock(mtx);\n            if (instance == nullptr){\n                volatile auto temp = new SingletonLazyDoubeCheck();\n                instance = temp;\n            }\n        }\n        return instance;\n    }\n};\n\/\/ \u76f4\u63a5\u7528Static \u5b9a\u4e49getInstance\u51fd\u6570\uff0c\u5e76\u5728\u91cc\u9762\u7528static\u5b9a\u4e49\u6240\u9700\u5b9e\u4f8b\u53d8\u91cf\uff0c\u8fd4\u56de\uff0c\u4e5f\u80fd\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\uff08C++11\u540e\uff09\nclass SingletonLazy{\nprivate:\n    SingletonLazy() {}\npublic:\n    SingletonLazy(const SingletonLazy &amp;other) = delete;\n    static SingletonLazy *getInstance(){\n        static SingletonLazy inst;\n        return &amp;inst;\n    }\n};\n<\/code><\/pre>\n<h2>\u4eab\u5143\u6a21\u5f0f\uff08Flyweight\uff09<\/h2>\n<p>\u5728\u8f6f\u4ef6\u7cfb\u7edf\u91c7\u7528\u7eaf\u7cb9\u5bf9\u8c61\u65b9\u6848\u7684\u95ee\u9898\u5728\u4e8e\u5927\u91cf\u7ec6\u7c92\u5ea6\u7684\u5bf9\u8c61\u4f1a\u5f88\u5feb\u5145\u65a5\u5728\u7cfb\u7edf\u4e2d\uff0c\u4ece\u800c\u5e26\u6765\u5f88\u9ad8\u7684\u8fd0\u884c\u65f6\u4ee3\u4ef7\u2014\u2014\u2014\u2014\u4e3b\u8981\u6307\u5185\u5b58\u9700\u6c42\u65b9\u9762\u7684\u4ee3\u4ef7<br \/>\n\u4eab\u5143\u6a21\u5f0f\u4e3b\u8981\u662f\u5728\u907f\u514d\u5927\u91cf\u7ec6\u7c92\u5ea6\u5bf9\u8c61\u95ee\u9898\u7684\u540c\u65f6\uff0c\u8ba9\u5916\u90e8\u5ba2\u6237\u7a0b\u5e8f\u4ecd\u7136\u80fd\u591f\u900f\u660e\u5730\u4f7f\u7528\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u5f0f\u8fdb\u884c\u64cd\u4f5c\u3002<br \/>\n\u5373\u4f7f\u7528\u5171\u4eab\u6280\u672f<\/p>\n<p>\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"language-cpp line-numbers\">\/\/ \u4e00\u4e2afont\u7c7b\u63cf\u8ff0\u5b57\u4f53\nclass Font{\nprivate:\n    string fontName;\n    \/\/ ...\npublic:\n    Font(const string &amp;name) {}\n};\n\nclass FontFactory{\nprivate:\n    map&lt;string, Font*&gt; fontPool;    \/\/ \u5efa\u7acb\u5b57\u4f53\u540d\u5230\u5b57\u4f53\u6307\u9488\u7684\u6620\u5c04\uff0c\u4f5c\u4e3a\u5bf9\u8c61\u6c60\npublic:\n    Font *GetFont(const string &amp;name){\n        map&lt;string, Font*&gt;::iterator it = fontPool.find(name);\n        if (it != fontPool.end()){      \/\/ \u5982\u679c\u5171\u4eab\u6c60\u91cc\u6709\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n            return fontPool[name];\n        }\n        Font *f = new Font(name);       \/\/ \u5426\u5219\u624d\u521b\u5efa\u65b0\u5bf9\u8c61\uff0c\u5e76\u4e14\u65b0\u5bf9\u8c61\u5165\u6c60\n        fontPool[name] = f;\n        return f;\n    }\n};\n\n<\/code><\/pre>\n<ul>\n<li>\u6574\u4e2a\u8bbe\u8ba1\u6a21\u5f0f\u91cc\uff0c\u53ea\u6709Singleton\u548cFlyweight\u662f\u89e3\u51b3\u6027\u80fd\u95ee\u9898\u7684\uff0c\u5176\u4ed6\u90fd\u662f\u89e3\u51b3\u62bd\u8c61\u6027\u95ee\u9898\uff0c\u63d0\u9ad8\u590d\u7528\u6027\u3002<\/li>\n<li>\u901a\u5e38\u5bf9\u8c61\u6c60\u91cc\u7684\u5bf9\u8c61\u521b\u5efa\u51fa\u6765\u540e\u90fd\u662f<strong>\u53ea\u8bfb\u7684<\/strong>\uff0c\u907f\u514d\u591a\u5904\u4f7f\u7528\u540c\u4e00\u5b9e\u9645\u5bf9\u8c61\u5e76\u5b58\u5728\u4fee\u6539\u7684\u95ee\u9898\u3002<\/li>\n<li>\u5bf9\u8c61\u6570\u91cf\u592a\u591a\u624d\u4f1a\u5bfc\u81f4\u5bf9\u8c61\u5185\u5b58\u5f00\u9500\u8f83\u5927\uff0c\u5177\u4f53\u8981\u5bf9\u5bf9\u8c61\u7684\u5927\u5c0f\u548c\u521b\u5efa\u7684\u6570\u91cf\u8fdb\u884c\u8bc4\u4f30\uff0c\u5c31\u5224\u65ad\u662f\u5426\u6709\u7528\u4eab\u5143\u6a21\u5f0f\u7684\u5fc5\u8981\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5355\u4f8b\u6a21\u5f0f\uff08Singleton\uff09 \u5728\u8f6f\u4ef6\u7cfb\u7edf\u4e2d\uff0c\u7ecf\u5e38\u6709\u8fd9\u6837\u4e00\u4e9b\u7279\u6b8a\u7684\u7c7b\uff0c\u5fc5\u987b\u4fdd\u8bc1\u5b83\u4eec\u5728\u7cfb\u7edf\u4e2d\u53ea\u5b58\u5728\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u624d\u80fd&hellip;<a href=\"https:\/\/www.notown.top\/?p=751\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">\u5355\u4f8b\u6a21\u5f0f\u548c\u4eab\u5143\u6a21\u5f0f<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[22],"tags":[],"class_list":["post-751","post","type-post","status-publish","format-standard","hentry","category-22"],"_links":{"self":[{"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/posts\/751","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.notown.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=751"}],"version-history":[{"count":6,"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/posts\/751\/revisions"}],"predecessor-version":[{"id":772,"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/posts\/751\/revisions\/772"}],"wp:attachment":[{"href":"https:\/\/www.notown.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=751"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.notown.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=751"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.notown.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}