{"id":43184,"date":"2024-12-01T13:59:04","date_gmt":"2024-12-01T05:59:04","guid":{"rendered":"https:\/\/fwq.ai\/blog\/43184\/"},"modified":"2024-12-01T13:59:04","modified_gmt":"2024-12-01T05:59:04","slug":"%e5%ae%9e%e9%99%85%e7%b1%bb%e5%9e%8b%e5%9c%a8%e8%b0%83%e7%94%a8%e6%97%b6%e4%b8%a2%e5%a4%b1","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/43184\/","title":{"rendered":"\u5b9e\u9645\u7c7b\u578b\u5728\u8c03\u7528\u65f6\u4e22\u5931"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>\u5b9e\u9645\u7c7b\u578b\u5728\u8c03\u7528\u65f6\u4e22\u5931<\/span><\/p>\n<h1>\u5b9e\u9645\u7c7b\u578b\u5728\u8c03\u7528\u65f6\u4e22\u5931<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-30 14:27:35<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>IT\u884c\u4e1a\u76f8\u5bf9\u4e8e\u4e00\u822c\u4f20\u7edf\u884c\u4e1a\uff0c\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u66f4\u5feb\uff0c\u4e00\u65e6\u505c\u6b62\u4e86\u5b66\u4e60\uff0c\u5f88\u5feb\u5c31\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u6240\u4ee5\u6211\u4eec\u9700\u8981\u8e0f\u8e0f\u5b9e\u5b9e\u7684\u4e0d\u65ad\u5b66\u4e60\uff0c\u7cbe\u8fdb\u81ea\u5df1\u7684\u6280\u672f\uff0c\u5c24\u5176\u662f\u521d\u5b66\u8005\u3002\u4eca\u5929\u7c73\u4e91\u7ed9\u5927\u5bb6\u6574\u7406\u4e86\u300a\u5b9e\u9645\u7c7b\u578b\u5728\u8c03\u7528\u65f6\u4e22\u5931\u300b\uff0c\u804a\u804a\uff0c\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u53e6\u4e00\u4e2a\u5173\u4e8e go \u4e2d\u591a\u6001\u6027\u7684\u95ee\u9898\uff0c\u53c2\u8003\uff1ago \u4e2d\u7684\u5d4c\u5165\u800c\u4e0d\u662f\u7ee7\u627f\uff0chttps:\/\/medium.com\/@adrianwit\/abstract-class-reinvented-with-go-4a7326525034<\/p>\n<p>\u52a8\u673a\uff1a\u6709\u4e00\u4e2a\u63a5\u53e3\uff08\u5e26\u6709\u4e00\u4e9b\u5904\u7406\u201c\u5916\u90e8\u4e16\u754c\u201d\u7684\u65b9\u6cd5\uff09\u548c\u8be5\u63a5\u53e3\u7684\u4e00\u5806\u5b9e\u73b0\u7ed3\u6784\u3002<\/p>\n<p>\u5176\u4e2d\u4e00\u4e9b\u65b9\u6cd5\u6709\u4e00\u4e2a\u201c\u6807\u51c6\u201d\u5b9e\u73b0\uff0c\u5176\u4e2d\u516c\u5171\u903b\u8f91\u5e94\u8be5\u653e\u5728\u4e00\u4e2a\u5730\u65b9\uff0c\u5e76\u59d4\u6258\u7ed9\u7ed3\u6784\u5b9e\u73b0\u63a5\u53e3\u4e2d\u7684\uff08\u65b0\uff09\u65b9\u6cd5\uff08\u201c\u5b50\u7c7b\u201d\u4e0d\u662f\u4e00\u4e2a\u8bcd\uff09 .<\/p>\n<p>\u6211\u5df2\u9605\u8bfb\u4e0a\u9762\u7684\u4e2d\u7b49\u94fe\u63a5\u5e76\u7f16\u5199\u4e86\u4e00\u4e9b\u6d4b\u8bd5\u4ee3\u7801\u3002\u5509\uff0c\u5b83\u6ca1\u6709\u6309\u7167\u6211\u671f\u671b\u7684\u65b9\u5f0f\u5de5\u4f5c\uff0c\u5f53\u63a5\u53e3\u4e0a\u7684\u8c03\u7528\u662f\u95f4\u63a5\u7684\u65f6\uff0c\u7ed3\u6784\u7684\u5b9e\u9645\u7c7b\u578b\u4f1a\u4e22\u5931\u3002<\/p>\n<p>\u5728 c++ \u4e2d\uff0c\u8fd9\u79f0\u4e3a\u201c\u57fa\u7c7b\u5207\u7247\u201d\uff0c\u5728\u6309\u503c\u4f20\u9012\u591a\u6001\u7c7b\u65f6\u53d1\u751f\u3002\u5728\u6211\u7684 go \u6d4b\u8bd5\u4ee3\u7801\u4e2d\uff0c\u6211\u5c0f\u5fc3\u5730\u901a\u8fc7\u5f15\u7528\u4f20\u9012\uff0c\u7136\u540e go \u5c31\u4e0d\u662f c++\uff08\u6216 java\uff09\u3002<\/p>\n<p>\u4ee3\u7801\uff1ahttps:\/\/play.golang.org\/p\/lxamw8v_kiw<\/p>\n<p>\u5185\u8054\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"log\"\n    \"reflect\"\n    \"strings\"\n)\n\n\/\/ Command - interface\n\ntype Command interface {\n    Execute()\n\n    getCommandString() string\n    onData(data string)\n}\n\n\/\/ Command - implementation\n\ntype Command_Impl struct {\n    commandString string\n    conn Connection\n}\n\nfunc newCommand_Impl(conn Connection, data string, args ...string) Command_Impl {\n    var buf strings.Builder\n    buf.WriteString(data)\n    for _, key := range args {\n        buf.WriteString(\" \")\n        buf.WriteString(key)\n    }\n\n    return Command_Impl {\n        conn: conn,\n        commandString: buf.String(),\n    }\n}\n\nfunc (self *Command_Impl) Execute() {\n    log.Printf(\"Command Impl Execute: %s\", reflect.TypeOf(self))\n    self.conn.execute(self)\n}\n\nfunc (self *Command_Impl) getCommandString() string {\n    return self.commandString\n}\n\nfunc (self *Command_Impl) onData(data string) {\n    log.Printf(\"Command Impl onData: %s\", data)\n}\n\n\/\/ Command - subclass\n\ntype Command_Login struct {\n    Command_Impl\n\n    onDataCalled bool\n}\n\nfunc newCommand_Login(conn Connection) *Command_Login {\n    return &amp;Command_Login{\n        Command_Impl: newCommand_Impl(conn, \"LOGIN\", \"[email&nbsp;protected]\", \"pa$$w0rd\"),\n    }\n}\n\nfunc (self *Command_Login) onData(data string) {\n    log.Printf(\"Command Login onData: %s\", data)\n\n    self.onDataCalled = true\n}\n\n\/\/ Connection - interface\n\ntype Connection interface {\n    execute(command Command)\n}\n\n\/\/ Connection - impelementation\n\ntype Connection_Impl struct {\n}\n\nfunc newConnection_Impl() *Connection_Impl {\n    return &amp;Connection_Impl{}\n}\n\nfunc (self *Connection_Impl) execute(command Command) {\n    log.Printf(\"Connection execute: %s, %s\", command.getCommandString(), reflect.TypeOf(command))\n    command.onData(\"some data\")\n}\n\nfunc main() {\n    conn := newConnection_Impl()\n    command := newCommand_Login(conn)\n\n    \/\/ I expect command.Execute to preserve actual type of command all the way through\n    \/\/ command.conn.execute(self) and then the callback onData from connection to command\n    \/\/ to use the onData in Command_Login\n    \/\/\n    \/\/ This does not happen however, the test fails\n    command.Execute()\n\n    \/\/ This does preserve actual type of command, but isn't how I'd like to connect\n    \/\/ commands and connections...\n    \/\/\n    \/\/conn.execute(command)\n\n    if command.onDataCalled {\n        log.Printf(\"*** GOOD: Command_Login onData ***was*** called\")\n    } else {\n        log.Printf(\"*** ERROR: Command_Login onData ***not*** called\")\n    }\n}<\/pre>\n<p>\u6709\u4e00\u4e2a command \u63a5\u53e3\uff0c\u5b83\u5b9a\u4e49\u4e86\u4e00\u4e9b\u65b9\u6cd5\u3002<\/p>\n<p>\u6709\u4e00\u4e2a command_impl \u7ed3\u6784\uff0c\u6211\u60f3\u5728\u5176\u4e2d\u5b9e\u73b0\u4e00\u4e9b\u901a\u7528\u4ee3\u7801\uff0c\u8fd9\u4e9b\u4ee3\u7801\u5c06\u8fdb\u4e00\u6b65\u59d4\u6258\u7ed9\u5b9e\u73b0\u76f8\u540c\u63a5\u53e3\u7684\u66f4\u591a\u7ed3\u6784\u4e2d\u7684\u66f4\u7ec6\u7c92\u5ea6\u7684\u65b9\u6cd5\uff08\u201c\u5b50\u7c7b\u4e0d\u662f\u4e00\u4e2a\u5355\u8bcd\u201d\uff09\uff0c\u7c7b\u4f3c\u4e8e\uff1a p&gt; <\/p>\n<p>https:\/\/stackoverflow.com\/a\/1727737\/2342806<\/p>\n<p>\u95ee\u9898\uff1a<\/p>\n<p>\u8c03\u7528 <code>command.execute()<\/code> \u53c8\u8c03\u7528 <code>conn.execute(self)<\/code> \u6700\u7ec8\u201c\u5207\u7247\u201d<code>command_login<\/code> \u5bf9\u8c61\uff0c\u5e76\u5728 <code>connection.execute<\/code> \u5185\u5c06\u5176\u8f6c\u6362\u4e3a <code>command_impl<\/code>\u3002\u56e0\u6b64\uff0c\u4e3a <code>command_login<\/code> \u5b9a\u4e49\u7684 <code>ondata<\/code> \u63a5\u53e3\u65b9\u6cd5\u4e0d\u4f1a\u88ab\u8c03\u7528\u3002<\/p>\n<p>\u5982\u679c\u6211\u8c03\u7528 <code>conn.execute(command)<\/code> \u90a3\u4e48\u6b63\u786e\u7684 <code>ondata<\/code> \u786e\u5b9e\u4f1a\u88ab\u8c03\u7528\uff0c\u4f46\u8fd9\u4e0d\u662f\u6211\u60f3\u8981\u8fde\u63a5\u6211\u7684\u5bf9\u8c61\u7684\u65b9\u5f0f\uff08\u4f8b\u5982 <code>command<\/code> \u5df2\u7ecf\u6709\u4e00\u4e2a <code>connection<\/code> \uff0c\u4f46\u57fa\u672c\u4e0a\u662f\u6211\u4e0a\u9762\u5199\u7684\u5173\u4e8e\u91cd\u7528\u7684\u5185\u5bb9\u5b9e\u65bd\uff09\u3002<\/p>\n<p>\u7528 go \u672f\u8bed\u6765\u8bf4\uff0c\u6211\u8bd5\u56fe\u60f3\u51fa\u4e00\u79cd\u901a\u8fc7\u5d4c\u5165\u6765\u59d4\u6258<em>\u5b9e\u73b0<\/em>\u7684\u65b9\u6cd5\uff0c\u5e76\u6709\u4e00\u79cd\u65b9\u6cd5\u8ba9\u59d4\u6258<em>\u56de\u8c03\u5230\u5c01\u95ed\u7c7b\u578b<\/em> em&gt; \uff08\u5fae\u8c03\u59d4\u6258\u7684\u903b\u8f91\uff09\u3002<\/p>\n<p>\u5509\uff0cgo \u4f3c\u4e4e\u4e0d\u652f\u6301\u5b83\uff08\u81f3\u5c11\u6211\u627e\u4e0d\u5230\u65b9\u6cd5\uff09\u2014\u2014\u4e00\u65e6\u4f60\u59d4\u6258\u7ed9\u5d4c\u5165\u7ed3\u6784\uff0c\u4f60\u7684\u8c03\u7528\u5c31\u5b8c\u5168\u4fdd\u7559\u5728\u5d4c\u5165\u7ed3\u6784\u4e2d\uff0c\u5b83\u201c\u4e0d\u77e5\u9053\u201d\u5b83\u662f\u4e00\u4e2a\u8f83\u5927\u5bf9\u8c61\u7684\u4e00\u90e8\u5206\uff0c\u8be5\u5bf9\u8c61\u53ef\u80fd\u60f3\u8981\u8986\u76d6\u67d0\u4e9b\u5d4c\u5165\u7ed3\u6784\u7684\u65b9\u6cd5\u3002<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u901a\u8fc7\u5728\u60a8\u9700\u8981\u7684\u6700\u6d45\u6df1\u5ea6\u5b9e\u73b0 execute \u63a5\u53e3\u6765\u59d4\u6258\u5b9e\u73b0\u600e\u4e48\u6837\uff1f <\/p>\n<pre>func (self *Command_Login) Execute() {\n    self.Command_Impl.Execute()\n    log.Printf(\"Command Login Execute: %s\", reflect.TypeOf(self))\n    self.onDataCalled = true\n}<\/pre>\n<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u5b9e\u9645\u7c7b\u578b\u5728\u8c03\u7528\u65f6\u4e22\u5931\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &gt; &gt; \u5b9e\u9645\u7c7b\u578b\u5728\u8c03\u7528\u65f6\u4e22\u5931 \u5b9e\u9645\u7c7b\u578b\u5728\u8c03\u7528\u65f6\u4e22\u5931 \u6765\u6e90\uff1astackoverflow 2024-04-30 14:27:35 0\u6d4f\u89c8 \u6536\u85cf IT\u884c\u4e1a\u76f8\u5bf9\u4e8e\u4e00\u822c\u4f20\u7edf\u884c\u4e1a\uff0c\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u66f4\u5feb\uff0c\u4e00\u65e6\u505c\u6b62\u4e86\u5b66\u4e60\uff0c\u5f88\u5feb\u5c31\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u6240\u4ee5\u6211\u4eec\u9700\u8981\u8e0f\u8e0f\u5b9e\u5b9e\u7684\u4e0d\u65ad\u5b66\u4e60\uff0c\u7cbe\u8fdb\u81ea\u5df1\u7684\u6280\u672f\uff0c\u5c24\u5176\u662f\u521d\u5b66\u8005\u3002\u4eca\u5929\u7c73\u4e91\u7ed9\u5927\u5bb6\u6574\u7406\u4e86\u300a\u5b9e\u9645\u7c7b\u578b\u5728\u8c03\u7528\u65f6\u4e22\u5931\u300b\uff0c\u804a\u804a\uff0c\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01 \u95ee\u9898\u5185\u5bb9 \u53e6\u4e00\u4e2a\u5173\u4e8e go \u4e2d\u591a\u6001\u6027\u7684\u95ee\u9898\uff0c\u53c2\u8003\uff1ago \u4e2d\u7684\u5d4c\u5165\u800c\u4e0d\u662f\u7ee7\u627f\uff0chttps:\/\/medium.com\/@adrianwit\/abstract-class-reinvented-with-go-4a7326525034 \u52a8\u673a\uff1a\u6709\u4e00\u4e2a\u63a5\u53e3\uff08\u5e26\u6709\u4e00\u4e9b\u5904\u7406\u201c\u5916\u90e8\u4e16\u754c\u201d\u7684\u65b9\u6cd5\uff09\u548c\u8be5\u63a5\u53e3\u7684\u4e00\u5806\u5b9e\u73b0\u7ed3\u6784\u3002 \u5176\u4e2d\u4e00\u4e9b\u65b9\u6cd5\u6709\u4e00\u4e2a\u201c\u6807\u51c6\u201d\u5b9e\u73b0\uff0c\u5176\u4e2d\u516c\u5171\u903b\u8f91\u5e94\u8be5\u653e\u5728\u4e00\u4e2a\u5730\u65b9\uff0c\u5e76\u59d4\u6258\u7ed9\u7ed3\u6784\u5b9e\u73b0\u63a5\u53e3\u4e2d\u7684\uff08\u65b0\uff09\u65b9\u6cd5\uff08\u201c\u5b50\u7c7b\u201d\u4e0d\u662f\u4e00\u4e2a\u8bcd\uff09 . \u6211\u5df2\u9605\u8bfb\u4e0a\u9762\u7684\u4e2d\u7b49\u94fe\u63a5\u5e76\u7f16\u5199\u4e86\u4e00\u4e9b\u6d4b\u8bd5\u4ee3\u7801\u3002\u5509\uff0c\u5b83\u6ca1\u6709\u6309\u7167\u6211\u671f\u671b\u7684\u65b9\u5f0f\u5de5\u4f5c\uff0c\u5f53\u63a5\u53e3\u4e0a\u7684\u8c03\u7528\u662f\u95f4\u63a5\u7684\u65f6\uff0c\u7ed3\u6784\u7684\u5b9e\u9645\u7c7b\u578b\u4f1a\u4e22\u5931\u3002 \u5728 c++ \u4e2d\uff0c\u8fd9\u79f0\u4e3a\u201c\u57fa\u7c7b\u5207\u7247\u201d\uff0c\u5728\u6309\u503c\u4f20\u9012\u591a\u6001\u7c7b\u65f6\u53d1\u751f\u3002\u5728\u6211\u7684 go \u6d4b\u8bd5\u4ee3\u7801\u4e2d\uff0c\u6211\u5c0f\u5fc3\u5730\u901a\u8fc7\u5f15\u7528\u4f20\u9012\uff0c\u7136\u540e go \u5c31\u4e0d\u662f c++\uff08\u6216 java\uff09\u3002 \u4ee3\u7801\uff1ahttps:\/\/play.golang.org\/p\/lxamw8v_kiw \u5185\u8054\uff1a package main import ( &#8220;log&#8221; &#8220;reflect&#8221; &#8220;strings&#8221; ) \/\/ Command &#8211; interface type Command interface { Execute() getCommandString() string onData(data string) } [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-43184","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/43184","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=43184"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/43184\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=43184"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=43184"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=43184"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}