{"id":2022,"date":"2019-05-30T09:00:37","date_gmt":"2019-05-30T06:00:37","guid":{"rendered":"http:\/\/demensdeum.com\/blog\/?p=2022"},"modified":"2024-12-16T22:32:37","modified_gmt":"2024-12-16T19:32:37","slug":"chain-of-responsibility-pattern","status":"publish","type":"post","link":"https:\/\/demensdeum.com\/blog\/zh\/2019\/05\/30\/chain-of-responsibility-pattern\/","title":{"rendered":"\u8d23\u4efb\u94fe\u6a21\u5f0f"},"content":{"rendered":"<p>\u8d23\u4efb\u94fe\u662f\u6307\u884c\u4e3a\u8bbe\u8ba1\u6a21\u5f0f\u3002<\/p>\n<p><a href=\"https:\/\/www.flickr.com\/photos\/122565603@N02\/13689239584\/\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2025\" src=\"https:\/\/demensdeum.com\/blog\/wp-content\/uploads\/2019\/05\/13689239584_12c1c20ebc_z1.jpg\" alt=\"\" width=\"427\" height=\"640\" \/><\/a><br \/>\n<a class=\"owner-name truncate no-outline\" title=\"\u524d\u5f80 Ganna Dolbieva \u7684\u7167\u7247\u6d41\" href=\"https:\/\/www.flickr.com\/photos\/122565603@N02\/\" target=\"_blank\" rel=\" noopener\" data-track=\"attributionNameClick\">Ganna Dolbieva<\/a><\/p>\n<p>\u7535\u5f71\u516c\u53f8 Jah-Pictures \u5236\u4f5c\u4e86\u4e00\u90e8\u5173\u4e8e\u5229\u6bd4\u91cc\u4e9a\u5171\u4ea7\u4e3b\u4e49\u62c9\u65af\u5854\u6cd5\u91cc\u6559\u5f92\u7684\u7eaa\u5f55\u7247\uff0c\u540d\u4e3a\u300a\u9a6c\u5229\u7684\u7ea2\u8272\u9ece\u660e\u300b\u3002\u8fd9\u90e8\u7535\u5f71\u5f88\u957f\uff088\u5c0f\u65f6\uff09\uff0c\u5f88\u6709\u8da3\uff0c\u4f46\u5728\u4e0a\u6620\u4e4b\u524d\uff0c\u4e8b\u5b9e\u8bc1\u660e\uff0c\u5728\u4e00\u4e9b\u56fd\u5bb6\uff0c\u7535\u5f71\u4e2d\u7684\u955c\u5934\u548c\u77ed\u8bed\u53ef\u80fd\u88ab\u8ba4\u4e3a\u662f\u5f02\u7aef\u90aa\u8bf4\uff0c\u4e0d\u4f1a\u83b7\u5f97\u53d1\u884c\u8bb8\u53ef\u3002\u7535\u5f71\u5236\u7247\u4eba\u51b3\u5b9a\u624b\u52a8\u548c\u81ea\u52a8\u5730\u4ece\u7535\u5f71\u4e2d\u5220\u9664\u5305\u542b\u53ef\u7591\u77ed\u8bed\u7684\u65f6\u523b\u3002\u9700\u8981\u8fdb\u884c\u53cc\u91cd\u68c0\u67e5\uff0c\u4ee5\u514d\u5728\u4eba\u5de5\u68c0\u67e5\u548c\u5b89\u88c5\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u9519\u8bef\u65f6\uff0c\u7ecf\u9500\u5546\u4ee3\u8868\u4e0d\u4f1a\u5728\u67d0\u4e9b\u56fd\u5bb6\u88ab\u7b80\u5355\u67aa\u51b3\u3002<br \/>\u56fd\u5bb6\u5206\u4e3a\u56db\u7ec4\uff1a\u6ca1\u6709\u5ba1\u67e5\u5236\u5ea6\u7684\u56fd\u5bb6\uff0c\u6709\u4e2d\u7b49\u3001\u4e2d\u7b49\u548c\u975e\u5e38\u4e25\u683c\u7684\u5ba1\u67e5\u5236\u5ea6\u3002\u51b3\u5b9a\u4f7f\u7528\u795e\u7ecf\u7f51\u7edc\u5bf9\u89c2\u770b\u7684\u7535\u5f71\u7247\u6bb5\u4e2d\u7684\u5f02\u7aef\u7ea7\u522b\u8fdb\u884c\u5206\u7c7b\u3002\u5bf9\u4e8e\u8be5\u9879\u76ee\uff0c\u9700\u8981\u8d2d\u4e70\u975e\u5e38\u6602\u8d35\u7684\u6700\u5148\u8fdb\u7684\u795e\u7ecf\u5143\uff0c\u5e76\u9488\u5bf9\u4e0d\u540c\u7ea7\u522b\u7684\u5ba1\u67e5\u8fdb\u884c\u8bad\u7ec3\uff0c\u8fd9\u662f\u5f00\u53d1\u4eba\u5458\u7684\u4efb\u52a1 -\u5c06\u5f71\u7247\u5206\u6210\u788e\u7247\uff0c\u5e76\u901a\u8fc7\u4e00\u7cfb\u5217\u795e\u7ecf\u7f51\u7edc\u4f20\u8f93\u5b83\u4eec\uff0c\u4ece\u81ea\u7531\u5230\u4e25\u683c\uff0c\u76f4\u5230\u5176\u4e2d\u4e00\u4e2a\u68c0\u6d4b\u5230\u5f02\u7aef\uff0c\u7136\u540e\u5c06\u7247\u6bb5\u8f6c\u79fb\u5230\u4eba\u5de5\u5ba1\u67e5\u4ee5\u8fdb\u4e00\u6b65\u7f16\u8f91\u3002\u4e0d\u53ef\u80fd\u901a\u8fc7\u6240\u6709\u795e\u7ecf\u5143\uff0c\u56e0\u4e3a\u4ed6\u4eec\u7684\u5de5\u4f5c\u9700\u8981\u592a\u591a\u7684\u8ba1\u7b97\u80fd\u529b\uff08\u6bd5\u7adf\u6211\u4eec\u8fd8\u8981\u652f\u4ed8\u7535\u8d39\uff09\uff0c\u5728\u7b2c\u4e00\u4e2a\u5de5\u4f5c\u65f6\u505c\u6b62\u5c31\u8db3\u591f\u4e86\u3002<br \/>\u6734\u7d20\u7684\u4f2a\u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-unknown\" data-lang=\"unknown\"><code>import StateOfArtCensorshipHLNNClassifiers\n\nprotocol MovieCensorshipClassifier {\n    func shouldBeCensored(movieChunk: MovieChunk) -&gt; Bool\n}\n\nclass CensorshipClassifier: MovieCensorshipClassifier {\n\n    let hnnclassifier: StateOfArtCensorshipHLNNClassifier\n\n    init(_ hnnclassifier: StateOfArtCensorshipHLNNClassifier) {\n        self.hnnclassifier = hnnclassifier\n    }\n    \n    func shouldBeCensored(_ movieChunk: MovieChunk) -&gt; Bool {\n        return hnnclassifier.shouldBeCensored(movieChunk)\n    }\n}\n\nlet lightCensorshipClassifier = CensorshipClassifier(StateOfArtCensorshipHLNNClassifier(\"light\"))\nlet normalCensorshipClassifier = CensorshipClassifier(StateOfArtCensorshipHLNNClassifier(\"normal\"))\nlet hardCensorshipClassifier = CensorshipClassifier(StateOfArtCensorshipHLNNClassifier(\"hard\"))\n\nlet classifiers = [lightCensorshipClassifier, normalCensorshipClassifier, hardCensorshipClassifier]\n\nlet movie = Movie(\"Red Jah rising\")\nfor chunk in movie.chunks {\n    for classifier in classifiers {\n        if classifier.shouldBeCensored(chunk) == true {\n            print(\"Should censor movie chunk: \\(chunk), reported by \\(classifier)\")\n        }\n   }\n}\n<\/code><\/pre>\n<\/div>\n<p>\u4e00\u822c\u6765\u8bf4\uff0c\u4f7f\u7528\u5206\u7c7b\u5668\u6570\u7ec4\u7684\u89e3\u51b3\u65b9\u6848\u5e76\u6ca1\u6709\u90a3\u4e48\u7cdf\u7cd5\uff0c\u4f46\u662f\uff01\u8ba9\u6211\u4eec\u60f3\u8c61\u4e00\u4e0b\uff0c\u6211\u4eec\u65e0\u6cd5\u521b\u5efa\u4e00\u4e2a\u6570\u7ec4\uff0c\u6211\u4eec\u6709\u673a\u4f1a\u53ea\u521b\u5efa\u4e00\u4e2a\u5206\u7c7b\u5668\u5b9e\u4f53\uff0c\u5b83\u5df2\u7ecf\u786e\u5b9a\u4e86\u7535\u5f71\u7247\u6bb5\u7684\u5ba1\u67e5\u7c7b\u578b\u3002\u5728\u5f00\u53d1\u6269\u5c55\u5e94\u7528\u7a0b\u5e8f\u529f\u80fd\u7684\u5e93\uff08\u63d2\u4ef6\uff09\u65f6\uff0c\u6b64\u7c7b\u9650\u5236\u662f\u53ef\u80fd\u7684\u3002<br \/>\u8ba9\u6211\u4eec\u4f7f\u7528\u88c5\u9970\u5668\u6a21\u5f0f&#8211;\u8ba9\u6211\u4eec\u5c06\u94fe\u4e2d\u4e0b\u4e00\u4e2a\u5206\u7c7b\u5668\u7684\u5f15\u7528\u6dfb\u52a0\u5230\u5206\u7c7b\u5668\u7c7b\u4e2d\uff0c\u5e76\u5728\u7b2c\u4e00\u4e2a\u6210\u529f\u5206\u7c7b\u65f6\u505c\u6b62\u9a8c\u8bc1\u8fc7\u7a0b\u3002<br \/>\u56e0\u6b64\uff0c\u6211\u4eec\u5b9e\u73b0\u4e86\u8d23\u4efb\u94fe\u6a21\u5f0f\uff1a<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-unknown\" data-lang=\"unknown\"><code>import StateOfArtCensorshipHLNNClassifiers\n\nprotocol MovieCensorshipClassifier {\n    func shouldBeCensored(movieChunk: MovieChunk) -&gt; Bool\n}\n\nclass CensorshipClassifier: MovieCensorshipClassifier {\n\n    let nextClassifier: CensorshipClassifier?\n    let hnnclassifier: StateOfArtCensorshipHLNNClassifier\n\n    init(_ hnnclassifier: StateOfArtCensorshipHLNNClassifier, nextClassifier: CensorshipClassifiers?) {\n            self.nextClassifier = nextClassifier\n            self.hnnclassifier = hnnclassifier\n    }\n    \n    func shouldBeCensored(_ movieChunk: MovieChunk) -&gt; Bool {\n        let result = hnnclassifier.shouldBeCensored(movieChunk)\n        \n        print(\"Should censor movie chunk: \\(movieChunk), reported by \\(self)\")\n        \n        if result == true {\n                return true\n        }\n        else {\n                return nextClassifier?.shouldBeCensored(movieChunk) ?? false\n        }\n    }\n}\n\nlet censorshipClassifier = CensorshipClassifier(StateOfArtCensorshipHLNNClassifier(\"light\"), nextClassifier: CensorshipClassifier(StateOfArtCensorshipHLNNClassifier(\"normal\", nextClassifier: CensorshipClassifier(StateOfArtCensorshipHLNNClassifier(\"hard\")))))\n\nlet movie = Movie(\"Red Jah rising\")\nfor chunk in movie.chunks {\n    censorshipClassifier.shouldBeCensored(chunk)\n}\n<\/code><\/pre>\n<\/div>\n<h3>\u53c2\u8003\u6587\u732e<\/h3>\n<p><a href=\"https:\/\/refactoring.guru\/ru\/design-patterns\/chain-of-responsibility\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/refactoring.guru\/ru\/\u8bbe\u8ba1\u6a21\u5f0f\/\u8d23\u4efb\u94fe<\/a><\/p>\n<h3>\u6e90\u4ee3\u7801<\/h3>\n<p><a href=\"https:\/\/gitlab.com\/demensdeum\/patterns\/\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/gitlab.com\/demensdeum\/patterns\/<\/a>< \/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8d23\u4efb\u94fe\u662f\u6307\u884c\u4e3a\u8bbe\u8ba1\u6a21\u5f0f\u3002 Ganna Dolbieva \u7535\u5f71\u516c\u53f8 Jah-Pictures \u5236\u4f5c\u4e86\u4e00\u90e8\u5173\u4e8e\u5229\u6bd4\u91cc\u4e9a\u5171\u4ea7\u4e3b\u4e49\u62c9\u65af\u5854\u6cd5\u91cc\u6559\u5f92\u7684\u7eaa\u5f55\u7247\uff0c\u540d\u4e3a\u300a\u9a6c\u5229\u7684\u7ea2\u8272\u9ece\u660e\u300b\u3002\u8fd9\u90e8\u7535\u5f71\u5f88\u957f\uff088\u5c0f\u65f6\uff09\uff0c\u5f88\u6709\u8da3\uff0c\u4f46\u5728\u4e0a\u6620\u4e4b\u524d\uff0c\u4e8b\u5b9e\u8bc1\u660e\uff0c\u5728\u4e00\u4e9b\u56fd\u5bb6\uff0c\u7535\u5f71\u4e2d\u7684\u955c\u5934\u548c\u77ed\u8bed\u53ef\u80fd\u88ab\u8ba4\u4e3a\u662f\u5f02\u7aef\u90aa\u8bf4\uff0c\u4e0d\u4f1a\u83b7\u5f97\u53d1\u884c\u8bb8\u53ef\u3002\u7535\u5f71\u5236\u7247\u4eba\u51b3\u5b9a\u624b\u52a8\u548c\u81ea\u52a8\u5730\u4ece\u7535\u5f71\u4e2d\u5220\u9664\u5305\u542b\u53ef\u7591\u77ed\u8bed\u7684\u65f6\u523b\u3002\u9700\u8981\u8fdb\u884c\u53cc\u91cd\u68c0\u67e5\uff0c\u4ee5\u514d\u5728\u4eba\u5de5\u68c0\u67e5\u548c\u5b89\u88c5\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u9519\u8bef\u65f6\uff0c\u7ecf\u9500\u5546\u4ee3\u8868\u4e0d\u4f1a\u5728\u67d0\u4e9b\u56fd\u5bb6\u88ab\u7b80\u5355\u67aa\u51b3\u3002\u56fd\u5bb6\u5206\u4e3a\u56db\u7ec4\uff1a\u6ca1\u6709\u5ba1\u67e5\u5236\u5ea6\u7684\u56fd\u5bb6\uff0c\u6709\u4e2d\u7b49\u3001\u4e2d\u7b49\u548c\u975e\u5e38\u4e25\u683c\u7684\u5ba1\u67e5\u5236\u5ea6\u3002\u51b3\u5b9a\u4f7f\u7528\u795e\u7ecf\u7f51\u7edc\u5bf9\u89c2\u770b\u7684\u7535\u5f71\u7247\u6bb5\u4e2d\u7684\u5f02\u7aef\u7ea7\u522b\u8fdb\u884c\u5206\u7c7b\u3002\u5bf9\u4e8e\u8be5\u9879\u76ee\uff0c\u9700\u8981\u8d2d\u4e70\u975e\u5e38\u6602\u8d35\u7684\u6700\u5148\u8fdb\u7684\u795e\u7ecf\u5143\uff0c\u5e76\u9488\u5bf9\u4e0d\u540c\u7ea7\u522b\u7684\u5ba1\u67e5\u8fdb\u884c\u8bad\u7ec3\uff0c\u8fd9\u662f\u5f00\u53d1\u4eba\u5458\u7684\u4efb\u52a1 -\u5c06\u5f71\u7247\u5206\u6210\u788e\u7247\uff0c\u5e76\u901a\u8fc7\u4e00\u7cfb\u5217\u795e\u7ecf\u7f51\u7edc\u4f20\u8f93\u5b83\u4eec\uff0c\u4ece\u81ea\u7531\u5230\u4e25\u683c\uff0c\u76f4\u5230\u5176\u4e2d\u4e00\u4e2a\u68c0\u6d4b\u5230\u5f02\u7aef\uff0c\u7136\u540e\u5c06\u7247\u6bb5\u8f6c\u79fb\u5230\u4eba\u5de5\u5ba1\u67e5\u4ee5\u8fdb\u4e00\u6b65\u7f16\u8f91\u3002\u4e0d\u53ef\u80fd\u901a\u8fc7\u6240\u6709\u795e\u7ecf\u5143\uff0c\u56e0\u4e3a\u4ed6\u4eec\u7684\u5de5\u4f5c\u9700\u8981\u592a\u591a\u7684\u8ba1\u7b97\u80fd\u529b\uff08\u6bd5\u7adf\u6211\u4eec\u8fd8\u8981\u652f\u4ed8\u7535\u8d39\uff09\uff0c\u5728\u7b2c\u4e00\u4e2a\u5de5\u4f5c\u65f6\u505c\u6b62\u5c31\u8db3\u591f\u4e86\u3002\u6734\u7d20\u7684\u4f2a\u4ee3\u7801\u5b9e\u73b0\uff1a import StateOfArtCensorshipHLNNClassifiers protocol MovieCensorshipClassifier { func shouldBeCensored(movieChunk: MovieChunk) -&gt; Bool } class CensorshipClassifier: MovieCensorshipClassifier { let hnnclassifier: StateOfArtCensorshipHLNNClassifier init(_ hnnclassifier: StateOfArtCensorshipHLNNClassifier) { self.hnnclassifier = hnnclassifier } func shouldBeCensored(_ movieChunk: MovieChunk) -&gt; Bool { return hnnclassifier.shouldBeCensored(movieChunk) } } let lightCensorshipClassifier = CensorshipClassifier(StateOfArtCensorshipHLNNClassifier(&#8220;light&#8221;)) let normalCensorshipClassifier = CensorshipClassifier(StateOfArtCensorshipHLNNClassifier(&#8220;normal&#8221;)) let hardCensorshipClassifier =<a class=\"more-link\" href=\"https:\/\/demensdeum.com\/blog\/zh\/2019\/05\/30\/chain-of-responsibility-pattern\/\">Continue reading <span class=\"screen-reader-text\">&#8220;\u8d23\u4efb\u94fe\u6a21\u5f0f&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[61,52],"tags":[105,95],"class_list":["post-2022","post","type-post","status-publish","format-standard","hentry","category-techie","category-tutorials","tag-chain-of-responsibility","tag-patterns","entry"],"translation":{"provider":"WPGlobus","version":"3.0.2","language":"zh","enabled_languages":["en","ru","zh","de","fr","ja","pt"],"languages":{"en":{"title":true,"content":true,"excerpt":false},"ru":{"title":true,"content":true,"excerpt":false},"zh":{"title":true,"content":true,"excerpt":false},"de":{"title":true,"content":true,"excerpt":false},"fr":{"title":true,"content":true,"excerpt":false},"ja":{"title":true,"content":true,"excerpt":false},"pt":{"title":true,"content":true,"excerpt":false}}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/demensdeum.com\/blog\/zh\/wp-json\/wp\/v2\/posts\/2022","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/demensdeum.com\/blog\/zh\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/demensdeum.com\/blog\/zh\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/zh\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/zh\/wp-json\/wp\/v2\/comments?post=2022"}],"version-history":[{"count":18,"href":"https:\/\/demensdeum.com\/blog\/zh\/wp-json\/wp\/v2\/posts\/2022\/revisions"}],"predecessor-version":[{"id":3956,"href":"https:\/\/demensdeum.com\/blog\/zh\/wp-json\/wp\/v2\/posts\/2022\/revisions\/3956"}],"wp:attachment":[{"href":"https:\/\/demensdeum.com\/blog\/zh\/wp-json\/wp\/v2\/media?parent=2022"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/zh\/wp-json\/wp\/v2\/categories?post=2022"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/zh\/wp-json\/wp\/v2\/tags?post=2022"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}