{"id":260368,"date":"2023-01-21T08:38:22","date_gmt":"2023-01-21T00:38:22","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=260368"},"modified":"2022-12-27T21:38:51","modified_gmt":"2022-12-27T13:38:51","slug":"linux-demo-team","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/linux-demo-team.html","title":{"rendered":"\u7b80\u5355\u8bb2\u89e3\u89c2\u5bdf\u8005\u8bbe\u8ba1\u6a21\u5f0f"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u89c2\u5bdf\u8005\u6a21\u5f0f\u662f\u6781\u5176\u91cd\u8981\u7684\u4e00\u4e2a\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u4e5f\u662f\u6211\u51e0\u5e74\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u4f7f\u7528\u6700\u591a\u7684\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u672c\u6587\u9996\u5148\u6982\u8ff0\u89c2\u5bdf\u8005\u6a21\u5f0f\u7684\u57fa\u672c\u6982\u5ff5\u548cDemo\u5b9e\u73b0\uff0c\u63a5\u7740\u662f\u89c2\u5bdf\u8005\u6a21\u5f0f\u5728C++\u4e2d\u7684\u5e94\u7528\uff0c\u6700\u540e\u662f\u5bf9\u89c2\u5bdf\u8005\u6a21\u5f0f\u7684\u5e94\u7528\u573a\u666f\u548c\u4f18\u7f3a\u70b9\u8fdb\u884c\u603b\u7ed3<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u7528\u6765\u89e3\u51b3\u4e24\u4e2a\u4e0d\u76f8\u5173\u5bf9\u8c61\u4e4b\u95f4\u7684\u4e00\u5bf9\u4e00\u6216\u8005\u4e00\u5bf9\u591a\u7684\u901a\u4fe1\u6a21\u578b\u3002<\/p>\n

\u4ec0\u4e48\u662f\u89c2\u5bdf\u8005\u8bbe\u8ba1\u6a21\u5f0f<\/strong><\/div>\n

\u89c2\u5bdf\u8005\u6a21\u5f0f\u662f\u4e00\u79cd\u5bf9\u8c61\u884c\u4e3a\u6a21\u5f0f\u3002\u5b83\u5b9a\u4e49\u5bf9\u8c61\u95f4\u7684\u4e00\u79cd\u4e00\u5bf9\u591a\u7684\u4f9d\u8d56\u5173\u7cfb\uff0c \u5f53\u4e00\u4e2a\u5bf9\u8c61\u7684\u72b6\u6001\u53d1\u751f\u6539\u53d8\u65f6\uff0c\u6240\u6709\u4f9d\u8d56\u4e8e\u5b83\u7684\u5bf9\u8c61\u90fd\u5f97\u5230\u901a\u77e5\u5e76\u88ab\u81ea\u52a8\u66f4\u65b0\u3002\u5728\u89c2\u5bdf\u8005\u6a21\u5f0f\u4e2d\uff0c\u4e3b\u4f53\u662f\u901a\u77e5\u7684\u53d1\u5e03\u8005\uff0c\u5b83\u53d1\u51fa\u901a\u77e5\u65f6\u5e76\u4e0d\u9700\u8981\u77e5\u9053\u8c01\u662f\u5b83\u7684\u89c2\u5bdf\u8005\uff0c\u53ef\u4ee5\u6709\u4efb\u610f\u6570\u76ee\u7684\u89c2\u5bdf\u8005\u8ba2\u9605\u5e76\u63a5\u53d7\u901a\u77e5\u3002\u89c2\u5bdf\u8005\u6a21\u5f0f\u4e0d\u4ec5\u88ab\u5e7f\u6cdb\u5e94\u7528\u4e8e\u8f6f\u4ef6\u754c\u9762\u5143\u7d20\u4e4b\u95f4\u7684\u4ea4\u4e92\uff0c\u5728\u4e1a\u52a1\u5bf9\u8c61\u4e4b\u95f4\u7684\u4ea4\u4e92\u3001\u6743\u9650\u7ba1\u7406\u7b49\u65b9\u9762\u4e5f\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u3002<\/p>\n

\u89e3\u51b3\u7684\u95ee\u9898<\/strong><\/div>\n

\u5b9a\u4e49\u4e86\u5bf9\u8c61\u95f4\u7684\u4e00\u79cd\u4e00\u5bf9\u591a\u7684\u7ec4\u5408\u5173\u7cfb\uff0c\u4ee5\u4fbf\u4e00\u4e2a\u5bf9\u8c61\u7684\u72b6\u6001\u53d1\u751f\u65f6\uff0c\u6240\u6709\u4f9d\u8d56\u4e8e\u5b83\u7684\u5bf9\u8c61\u90fd\u5f97\u5230\u901a\u77e5\u5e76\u81ea\u52a8\u5237\u65b0\u3002<\/p>\n

\u89c2\u5bdf\u8005\u548c\u88ab\u89c2\u5bdf\u8005\u4e4b\u95f4\u5b58\u5728\u201c\u89c2\u5bdf\u201d\u7684\u903b\u8f91\u5173\u7cfb\uff0c\u5f53\u88ab\u89c2\u5bdf\u8005\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u89c2\u5bdf\u8005\u5c31\u4f1a\u89c2\u5bdf\u5230\u8fd9\u6837\u7684\u53d8\u5316\uff0c\u5e76\u4f5c\u51fa\u76f8\u5e94\u7684\u54cd\u5e94\u3002<\/p>\n

\u7f16\u7a0b\u601d\u8def<\/strong><\/div>\n

\u8bbe\u5b9a\u4e24\u8005\u7c7b\uff0c\u4e00\u4e2a\u4e3a\u89c2\u5bdf\u8005\u7c7b\uff0c\u4e00\u4e2a\u4e3a\u88ab\u89c2\u5bdf\u8005\u7c7b<\/p>\n

\u89c2\u5bdf\u8005\u7c7b\u4e2d\uff0c\u5b9a\u4e49\u4e00\u4e2a\u5bf9\u67d0\u4e2a\u4e8b\u4ef6\u611f\u5174\u8da3\u7684\u5904\u7406\u51fd\u6570\uff0c\u4e00\u822c\u4e5f\u53eb\u505a\u69fd\u51fd\u6570<\/p>\n

\u88ab\u89c2\u5bdf\u8005\u7c7b\u4e2d\uff0c\u5b9a\u4e49\u4e00\u4e2a\u6570\u636e\u7ed3\u6784\uff0c\u7528\u6765\u4fdd\u5b58\u89c2\u5bdf\u8005\u5bf9\u67d0\u4e00\u4e2a\u4e8b\u4ef6id\uff08\u4fe1\u53f7\uff09\u611f\u5174\u8da3\uff0c\u4f7f\u7528\u6570\u636e\u7ed3\u6784\u5efa\u7acb\u4fe1\u53f7\u4e0e\u5bf9\u8c61\u4e4b\u95f4\u7684\u6620\u5c04\u5173\u7cfb<\/p>\n

\u88ab\u89c2\u5bdf\u8005\u7c7b\u4e2d\uff0c\u5b9a\u4e49\u4e24\u4e2a\u65b9\u6cd5\u51fd\u6570\uff1a<\/p>\n

\u4e00\u4e2a\u65b9\u6cd5\u4e3a\uff1a\u6dfb\u52a0\u89c2\u5bdf\u8005\u4e0e\u5176\u611f\u5174\u8da3\u7684\u4e8b\u4ef6id\uff08\u4fe1\u53f7\uff09\u52a0\u5165\u5230\u5bb9\u5668\u4e2d<\/p>\n

\u53e6\u4e00\u4e2a\u65b9\u6cd5\u4e3a\uff1a\u4fe1\u53f7\u51fd\u6570\uff1a\u901a\u77e5\u4e8b\u4ef6\u51fd\u6570\u6267\u884c\u903b\u8f91\uff1a\u9996\u5148\u904d\u5386\u5bb9\u5668\u4e2d\uff0c\u6709\u6ca1\u6709\u611f\u5174\u8da3\u7684\u4e8b\u4ef6ID\uff0c\u5982\u679c\u6709\uff0c\u5219\u4ee3\u8868\u4e00\u7cfb\u5217\u7684\u89c2\u5bdf\u8005\uff0c\u5bf9\u8fd9\u4e2a\u4e8b\u4ef6\u611f\u5174\u8da3\uff0c\u90a3\u4e48\u518d\u6b21\u904d\u5386\u89c2\u5bdf\u8005\u5217\u8868\uff0c\u8ba9\u6bcf\u4e00\u4e2a\u89c2\u5bdf\u8005\u6267\u884c\u76f8\u5e94\u7684\u69fd\u51fd\u6570<\/p>\n

#include \r\n#include \r\n#include \r\nusing namespace std;\r\nclass RecvBase\r\n{\r\npublic:\r\n    RecvBase()\r\n    {\r\n        cout << \"RecvBase structure\" <<  endl;\r\n    }\r\n    virtual void slotFunctions(int msgid)=0;\r\n    virtual ~RecvBase()\r\n    {\r\n        cout << \"RecvBase destruct\" << endl;\r\n    }\r\n};\r\nclass Recv:public RecvBase\r\n{\r\npublic:\r\n    Recv()\r\n    {\r\n        cout << \"Recv structure\" << \"---\" << this << \"---\" << endl;\r\n    }\r\n    void slotFunctions(int msgid)override\r\n    {\r\n        switch(msgid)\r\n        {\r\n        case 1:\r\n            cout << this << \"\u63a5\u6536\u52301\u4fe1\u53f7\uff0c\u6267\u884c1\u4fe1\u53f7\u5bf9\u5e94\u7684\u69fd\u51fd\u6570\" << endl;\r\n            break;\r\n        case 2:\r\n            cout << this << \"\u63a5\u6536\u52302\u4fe1\u53f7\uff0c\u6267\u884c2\u4fe1\u53f7\u5bf9\u5e94\u7684\u69fd\u51fd\u6570\" << endl;\r\n            break;\r\n        case 3:\r\n            cout << this << \"\u63a5\u6536\u52303\u4fe1\u53f7\uff0c\u6267\u884c3\u4fe1\u53f7\u5bf9\u5e94\u7684\u69fd\u51fd\u6570\" << endl;\r\n            break;\r\n        case 4:\r\n            cout << this << \"\u63a5\u6536\u52304\u4fe1\u53f7\uff0c\u6267\u884c4\u4fe1\u53f7\u5bf9\u5e94\u7684\u69fd\u51fd\u6570\" << endl;\r\n            break;\r\n        }\r\n    }\r\n    ~Recv()override\r\n    {\r\n        cout << \"Recv destruct\" << endl;\r\n    }\r\n};\r\nclass Sender\r\n{\r\npublic:\r\n    Sender()\r\n    {\r\n        cout << \"sender structure\" << endl;\r\n    }\r\n    map> RecvMap;\r\n    void observerToRecvMap(int msgid,RecvBase* recv)\r\n    {\r\n        this->RecvMap[msgid].push_back(recv);\r\n    }\r\n    void senderSignals(int msgid)\r\n    {\r\n        auto it = RecvMap.find(msgid);\r\n        if(it!=RecvMap.end())\r\n        {\r\n            for(RecvBase* p:it->second)\r\n            {\r\n                p->slotFunctions(msgid);\r\n            }\r\n        }else{\r\n            cout << \"\u63a5\u6536\u5230\u672a\u77e5\u4fe1\u53f7\uff0c\u6ca1\u6709\u5bf9\u5e94\u7684\u53ef\u6267\u884c\u69fd\u51fd\u6570\" << endl;\r\n        }\r\n    }\r\n    ~Sender()\r\n    {\r\n        cout << \"sender destruct\" << endl;\r\n    }\r\n};\r\nint main()\r\n{\r\n    Sender sender;\r\n    RecvBase* r1=new Recv();\r\n    RecvBase* r2=new Recv();\r\n    RecvBase* r3=new Recv();\r\n    RecvBase* r4=new Recv();\r\n    sender.observerToRecvMap(1,r1);\r\n    sender.observerToRecvMap(1,r2);\r\n    sender.observerToRecvMap(2,r2);\r\n    sender.observerToRecvMap(3,r3);\r\n    sender.observerToRecvMap(4,r4);\r\n    while(true)\r\n    {\r\n        int msgid;\r\n        cin >> msgid;\r\n        if(-1==msgid)break;\r\n        sender.senderSignals(msgid);\r\n    }\r\n    delete r1;\r\n    delete r2;\r\n    delete r3;\r\n    delete r4;\r\n    return 0;\r\n}<\/list><\/map><\/iostream><\/pre>\n
\u77e5\u8bc6\u5c0f\u767e\u79d1<\/strong><\/div>\n

this\u6307\u9488\uff1a<\/p>\n

  • \u662f\u672c\u5bf9\u8c61\u7684\u8d77\u59cb\u5730\u5740\uff0c\u7528const\u4fee\u9970\u7684\u5e38\u5730\u5740\uff08\u539f\u578b\uff1astu *const this\uff09\uff08\u672c\u6587\u4ee3\u7801\u4e2dthis\u7684\u4f5c\u7528\uff09<\/li>\n
  • \u662f\u9690\u85cf\u5728\u6210\u5458\u51fd\u6570\uff08\u975e\u9759\u6001\u51fd\u6570\uff09\u7684\u6700\u5de6\u4fa7\u7684\u5e38\u53d8\u91cf\u5f62\u53c2<\/li>\n

    \u4f5c\u7528\uff1a<\/p>\n

  • \u533a\u522b\u6210\u5458\u5c5e\u6027\u548c\u6210\u5458\u51fd\u6570\u7684\u5f62\u53c2<\/li>\n
  • \u89e3\u5f15\u7528\uff0c\u8fd4\u56de\u672c\u5bf9\u8c61\u7684\u5730\u5740<\/li>\n

    \u5230\u6b64\u8fd9\u7bc7\u5173\u4e8eC++\u793a\u4f8b\u8bb2\u89e3\u89c2\u5bdf\u8005\u8bbe\u8ba1\u6a21\u5f0f\u7684\u6587\u7ae0\u5c31\u4ecb\u7ecd\u5230\u8fd9\u4e86<\/p>\n","protected":false},"excerpt":{"rendered":"

    \u7528\u6765\u89e3\u51b3\u4e24\u4e2a\u4e0d\u76f8\u5173\u5bf9\u8c61\u4e4b\u95f4\u7684\u4e00\u5bf9\u4e00\u6216\u8005\u4e00\u5bf9\u591a\u7684\u901a\u4fe1\u6a21\u578b\u3002 \u89c2\u5bdf\u8005\u6a21\u5f0f\u662f\u4e00\u79cd\u5bf9\u8c61\u884c\u4e3a\u6a21\u5f0f\u3002\u5b83\u5b9a\u4e49\u5bf9\u8c61\u95f4\u7684\u4e00\u79cd\u4e00\u5bf9 […]<\/p>\n","protected":false},"author":362,"featured_media":254555,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-260368","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/260368","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/362"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=260368"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/260368\/revisions"}],"predecessor-version":[{"id":260371,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/260368\/revisions\/260371"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/254555"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=260368"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=260368"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=260368"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}