{"id":6661,"date":"2024-07-31T21:01:03","date_gmt":"2024-07-31T13:01:03","guid":{"rendered":""},"modified":"2024-07-31T21:01:03","modified_gmt":"2024-07-31T13:01:03","slug":"\u89c2\u5bdf\u8005\u6a21\u5f0f\u662f\u4ec0\u4e48\u610f\u601d_\u89c2\u5bdf\u8005\u6a21\u5f0f\u89e3\u51b3\u4ec0\u4e48\u95ee\u9898","status":"publish","type":"post","link":"https:\/\/mushiming.com\/6661.html","title":{"rendered":"\u89c2\u5bdf\u8005\u6a21\u5f0f\u662f\u4ec0\u4e48\u610f\u601d_\u89c2\u5bdf\u8005\u6a21\u5f0f\u89e3\u51b3\u4ec0\u4e48\u95ee\u9898"},"content":{"rendered":"<\/p>\n
\u89c2\u5bdf\u8005\u6a21\u5f0f: \u5b9a\u4e49\u4e86\u4e00\u79cd 1\u5bf9\u591a \u7684\u4f9d\u8d56\u5173\u7cfb, \u8ba9\u591a\u4e2a\u89c2\u5bdf\u8005\u5bf9\u8c61\u540c\u65f6\u76d1\u542c1\u4e2a\u4e3b\u9898\u5bf9\u8c61.<\/p>\n
\u8fd9\u4e2a\u4e3b\u9898\u5bf9\u8c61\u5728\u72b6\u6001\u53d1\u751f\u53d8\u5316\u65f6, \u4f1a\u901a\u77e5\u6240\u6709\u7684\u89c2\u5bdf\u8005\u5bf9\u8c61, \u4f7f\u5b83\u4eec\u80fd\u591f\u540c\u65f6\u66f4\u65b0\u81ea\u5df1.<\/p>\n<\/p>\n
\u7a0d\u5fae\u89e3\u91ca\u4e00\u4e0b \u8fd9\u4e2a1 \u5bf9\u591a \u7684\u4f9d\u8d56\u5173\u7cfb.<\/p>\n
1\u5bf9\u591a \u8fd9\u4e2a\u5173\u952e\u8bcd\u6211\u4eec\u5e38\u5e38\u5728DB \u8868\u8bbe\u8ba1\u91cc\u63d0\u5230, \u4f46\u662f\u8fd9\u91cc\u7684\u610f\u601d\u662f\u6709\u70b9\u533a\u522b\u7684.<\/p>\n
<\/p>\n
\u9996\u5148, 1 \u662f1\u4e2a\u5bf9\u8c61, \u800c\u4e0d\u662f1\u4e2a\u7c7b, \u800c\u591a\u4e5f\u662f\u6307\u591a\u4e2a\u5bf9\u8c61, \u800c\u4e0d\u662f\u591a\u4e2a\u7c7b.<\/p>\n
\u5176\u6b21, \u8fd9\u91cc\u7684\u591a\u4e2a\u5bf9\u8c61\u53ef\u4ee5\u662f\u591a\u4e2a\u4e0d\u540c\u7684\u7c7b\u7684\u5bf9\u8c61. \u751a\u81f3\u662f\u6beb\u65e0\u5173\u7cfb\u7684\u591a\u4e2a\u7c7b.<\/p>\n<\/p>\n
\u518d\u6b21, \u8fd9\u4e2a\u4f9d\u8d56\u5173\u7cfb, \u5230\u5e95\u662f1\u4e2a\u5bf9\u8c61\u4f9d\u8d56\u591a\u4e2a\u5bf9\u8c61, \u8fd8\u662f\u591a\u4e2a\u5bf9\u8c61\u4f9d\u8d561\u4e2a\u5bf9\u8c61\u5462.<\/p>\n<\/p>\n
\u5728\u8fd9\u91cc\u7684\u5b9a\u4e49\u6765\u8bb2\u7b54\u6848\u662f\u540e\u8005.<\/strong><\/span><\/p>\n<\/p>\n \u4f46\u662f, \u5b9e\u9645\u4e0a, 1\u4e2a\u89c2\u5bdf\u8005\u4e5f\u53ef\u4ee5\u89c2\u5bdf\u591a\u4e2a\u88ab\u89c2\u5bdf\u8005\u7684. (\u4f46\u8fd9\u5c31\u4e0d\u5c5e\u4e8e\u89c2\u5bdf\u8005\u6a21\u5f0f\u4e86)<\/strong><\/p>\n<\/p>\n \u6240\u4ee5, \u89c2\u5bdf\u8005\u6a21\u5f0f(Observer) \u4e5f\u53eb\u505a \u53d1\u5e03-\u8ba2\u9605\u6a21\u5f0f(publish\/Subscribe).<\/p>\n<\/p>\n \u76f8\u5f53\u4e0e, \u591a\u4e2a\u8bfb\u8005\u540c\u65f6\u6536\u542c1\u4e2a\u7535\u53f0.<\/p>\n<\/p>\n<\/p>\n \u9996\u5148\u7747\u4e0bObserver \u6a21\u5f0f\u7684\u6807\u51c6UML\u56fe.<\/p>\n<\/p>\n \u6211\u4eec\u5927\u6982\u5f97\u51fa\u4e0a\u56fe\u67094\u4e2a\u89d2\u8272.<\/p>\n<\/p>\n Observer\u662f1\u4e2a\u63a5\u53e3, \u6211\u4eec\u53ef\u4ee5\u7406\u89e3\u5b83\u662f1\u4e2a\u62bd\u8c61\u89c2\u5bdf\u8005\u7c7b.<\/p>\n \u5b83\u53ea\u67091\u4e2aupdate()\u65b9\u6cd5, \u4e5f\u5c31\u662f\u8bf4\u5b83\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e2a\u65b9\u6cd5\u6765\u6267\u884c\u67d0\u4e9b\u52a8\u4f5c.<\/p>\n<\/p>\n Subject\u662f1\u4e2a\u63a5\u53e3, \u6211\u4eec\u53ef\u4ee5\u7406\u89e3\u4e3a1\u4e2a\u62bd\u8c61\u88ab\u89c2\u5bdf\u8005\u7c7b.<\/p>\n<\/p>\n \u6211\u4eec\u53ef\u4ee5\u89c1\u5230\u5b83\u67095\u4e2a\u65b9\u6cd5.<\/p>\n<\/p>\n attach() \u548c detach()\u65b9\u6cd5\u7528\u6765\u589e\u5220\u89c2\u5bdf\u8005\u7684\u6570\u91cf, \u4e5f\u5c31\u662f\u6307\u5f53\u524d\u88ab\u89c2\u5bdf\u8005\u5bf9\u8c61\u5230\u5e95\u6709\u591a\u5c11\u4e2a\u89c2\u5bdf\u8005\u5728\u89c2\u5bdf\u7740\u5b83.<\/p>\n<\/p>\n setState(), \u548c getState() \u7528\u4e8e\u83b7\u53d6\u548c\u8bbe\u7f6e\u901a\u77e5\u8005\u5bf9\u8c61\u672c\u8eab\u7684\u72b6\u6001, \u8fd9\u4e2a\u72b6\u6001\u901a\u5e38\u662f\u4f20\u9001\u7ed9\u89c2\u5bdf\u8005\u4eec\u7684\u4fe1\u606f\u6216\u53c2\u6570.<\/p>\n \u4e5f\u5c31\u662f\u8bf4\u89c2\u5bdf\u8005\u6a21\u5f0f\u5230\u5e95\u5728\u89c2\u5bdf\u4ec0\u4e48. \u65e0\u975e\u5c31\u662f\u89c2\u5bdf\u88ab\u89c2\u5bdf\u8005\u7684\u8fd9\u4e2a\u72b6\u6001.<\/p>\n<\/p>\n Notify(), \u88ab\u89c2\u5bdf\u8005\u901a\u77e5\u6240\u6709\u89c2\u5bdf\u8005, \u8ba9\u89c2\u5bdf\u8005\u6839\u636e\u81ea\u5df1\u7684\u5f53\u524d\u72b6\u6001(getState())\u6267\u884c\u81ea\u5df1\u7684update()\u65b9\u6cd5<\/p>\n<\/p>\n<\/p>\n \u8fd9\u4e2a(\u4e9b)\u5c31\u662f\u5177\u4f53\u7684\u88ab\u89c2\u5bdf\u8005\u7c7b, \u53ea\u8981\u5b9e\u73b0\u4e86Subject\u63a5\u53e3, \u5c31\u53ef\u4ee5\u6dfb\u52a01\u4e9b\u5bf9\u8c61\u4f5c\u4e3a\u81ea\u5df1\u7684\u89c2\u5bdf\u8005(\u6216\u8005\u53eb\u7c89\u4e1d\u5566)<\/p>\n \u5199\u5230\u8fd9\u91cc, \u5927\u5bb6\u90fd\u4f1a\u4e86\u89e3\u5230, \u8fd9\u4e2a\u7c7b\u91cc\u9762\u80af\u5b9a\u67091\u4e2a\u5bb9\u5668, \u7528\u4e8e\u5b58\u653e\u89c2\u5bdf\u8005\u7684\u5bf9\u8c61.<\/p>\n<\/p>\n \u8fd9\u4e2a\u5bb9\u5668\u53ef\u4ee5\u6839\u636e\u9700\u8981\u7531\u5177\u4f53\u7684\u88ab\u89c2\u5bdf\u8005\u9009\u62e9, \u901a\u5e38\u662f\u65e0\u5e8f\u4e0d\u80fd\u91cd\u590d\u7684Set\u5bb9\u5668(\u4f8b\u5982 HashSet)<\/p>\n<\/p>\n \u800cNotify()\u65b9\u6cd5\u65e0\u975e\u5c31\u662f\u904d\u5386\u81ea\u5df1\u7684\u89c2\u5bdf\u8005\u5bb9\u5668, \u9010\u4e2a\u6267\u884c\u89c2\u5bdf\u8005\u7684update()\u65b9\u6cd5.<\/p>\n<\/p>\n<\/p>\n \u8fd9\u4e9b\u7c7b\u53ef\u4ee5\u662f\u6beb\u65e0\u5173\u8054\u7684\u7c7b, \u4f46\u662f\u5b83\u4eec\u90fd\u5fc5\u987b\u5b9e\u73b0Observer\u63a5\u53e3<\/p>\n<\/p>\n \u4e00\u65e6\u8fd9\u4e9b\u5bf9\u8c61\u88ab\u901a\u77e5\u8005, \u52a0\u5165\u81ea\u5df1\u7684\u5bb9\u5668, \u5c31\u76f8\u5f53\u4e8e\u89c2\u5bdf\u8005\u6b63\u5728\u89c2\u5bdf\u67d0\u4e2a\u88ab\u89c2\u5bdf\u8005.<\/p>\n<\/p>\n \u6ce8\u610f, \u89c2\u5bdf\u8005\u53ef\u4ee5\u88ab\u591a\u4e2a\u88ab\u89c2\u5bdf\u8005\u52a0\u5165\u81ea\u5df1\u7684\u5bb9\u5668, \u4e5f\u5c31\u662f\u76f8\u5f53\u4e8e\u89c2\u5bdf\u4e86\u591a\u4e2a\u88ab\u89c2\u5bdf\u8005\u4e86.(\u4f46\u8fd9\u5c31break\u4e86\u89c2\u5bdf\u8005\u6a21\u5f0f)<\/p>\n<\/p>\n<\/p>\n \u4e0b\u9762\u6211\u4eec\u75281\u4e2a\u5177\u4f53\u4f8b\u5b50\u6765\u7b80\u5355\u5b9e\u73b0\u8fd9\u4e2a\u6a21\u5f0f.<\/p>\n<\/p>\n \u6211\u4eec\u5047\u5b9a1\u4e2a\u4e8b\u4ef6\u67093\u4e2a\u89d2\u8272.<\/p>\n<\/p>\n 1. \u6307\u6325\u8005.(Commander)<\/p>\n \u6307\u6325\u70ae\u624b\u6253\u70ae, \u4ed6\u53ef\u4ee5\u8ba9\u8ba9\u82e5\u5e72\u4e2a<\/strong><\/span>\u70ae\u624b\u548c\u70ae\u7070\u7eb3\u5165\u81ea\u5df1\u7684\u547d\u4ee4\u8303\u56f4.<\/p>\n<\/p>\n 2. \u70ae\u624b, (CannonShooter)<\/p>\n \u4e00\u65e6\u6307\u6325\u8005\u901a\u77e5\u76ee\u6807, \u82e5\u5e72\u4e2a<\/strong><\/span>\u70ae\u624b\u5c31\u5f80\u54ea\u4e2a\u76ee\u6807\u8f70\u51fb.<\/p>\n<\/p>\n 3. \u70ae\u7070 (CannonFodder)<\/p>\n \u4e00\u65e6\u6307\u6325\u8005\u901a\u77e5, \u70ae\u7070\u5c31\u8db4\u4e0b..<\/p>\n<\/p>\n \u4e5f\u5c31\u662f\u8bf4, \u70ae\u624b\u5fc5\u987b\u77e5\u9053\u6307\u6325\u8fd9\u7684\u72b6\u6001(\u76ee\u6807\u4fe1\u53f7), \u5230\u5e95\u6253\u8c01.<\/p>\n \u800c\u70ae\u7070\u662f\u65e0\u5e8f\u5173\u5fc3\u5230\u5e95\u6253\u54ea\u91cc\u7684, \u4e00\u65e6\u63a5\u5230\u901a\u77e5, \u8db4\u4e0b\u5c31\u662f\u4e86.<\/p>\n<\/p>\n 5\u4e2a\u65b9\u6cd5\u7684\u610f\u4e49\u4e0a\u9762\u5df2\u7ecf\u89e3\u91ca\u8fc7\u4e86.<\/p>\n<\/p>\n \u901a\u77e5\u65b9\u6cd5\u4e4b\u6240\u4ee5\u4e0d\u5199\u6210notify(), \u662f\u56e0\u4e3anotify()\u672c\u8eab\u662fObject\u7c7b\u76841\u4e2afinel\u65b9\u6cd5<\/p>\n<\/p>\n<\/p>\n \u53ea\u67091\u4e2a\u62bd\u8c61\u65b9\u6cd5update()<\/p>\n<\/p>\n<\/p>\n \u5b83\u91cd\u5199\u4e86\u63a5\u53e3\u6240\u6709\u65b9\u6cd5.<\/p>\n<\/p>\n \u6240\u8c13\u7684notify()\u65b9\u6cd5, \u65e0\u975e\u5c31\u662f\u904d\u5386\u81ea\u5df1\u5bb9\u5668\u7684\u6240\u6709\u89c2\u5bdf\u8005, \u8be5\u5e72\u561b\u7684\u5e72\u561b(\u904d\u5386\u8c03\u7528\u5b83\u4eec\u7684update())\u65b9\u6cd5<\/p>\n<\/p>\n \u53ef\u4ee5\u89c1\u5230, \u70ae\u624b\u5fc5\u987b\u77e5\u9053\u6307\u6325\u8005\u7684\u72b6\u6001\u4fe1\u606f, \u6240\u4ee5\u5b83\u91cc\u9762\u5fc5\u987b\u6709\u4e2a\u5f53\u524d\u6307\u6325\u8005\u7684\u5bf9\u8c61\u6210\u5458.<\/p>\n<\/p>\n<\/p>\n \u70ae\u7070\u65e0\u9700\u5173\u5fc3\u6307\u6325\u8005\u7684\u72b6\u6001, \u91cc\u9762\u53ea\u9700\u8981\u91cd\u5199\u81ea\u5df1\u7684update()\u65b9\u6cd5\u5c31ok.<\/p>\n<\/p>\n \u4e0a\u9762\u7684\u4ee3\u7801\u4e0d\u96be\u770b\u61c2.<\/p>\n<\/p>\n \u65e0\u975e\u5c31\u662f\u5b9e\u4f8b\u53161\u4e2a\u6307\u6325\u8005, 1\u4e2a\u70ae\u624b, 3\u4e2a\u70ae\u7070<\/p>\n \u9996\u5148, \u6307\u6325\u8005\u901a\u77e5\u5411107\u76ee\u6807\u6253\u70ae, \u70ae\u624b\u5c04\u4e86, 3\u4e2a\u70ae\u7070\u8db4\u4e0b\u4e86.<\/p>\n<\/p>\n \u540e\u6765\u6307\u6325\u8005\u60f3\u5411\u6253\u51fb108\u76ee\u6807, \u4f46\u662f\u89c9\u5f97\u7b2c3\u53f7\u70ae\u7070\u4e0d\u5728\u653b\u51fb\u8303\u56f4, \u6240\u4ee5\u4ece\u81ea\u5df1\u7684\u89c2\u5bdf\u8005\u5bb9\u5668\u91cc\u79fb\u96643\u53f7\u70ae\u7070.<\/p>\n \u8fd9\u65f6, \u70ae\u624b\u5411108\u53f7\u76ee\u6807\u6253\u51fb, \u53ea\u67091\u53f72\u53f7\u70ae\u7070\u542c\u6307\u6325\u722c\u4e0b.<\/p>\n<\/p>\n \u76f8\u5f53\u7075\u6d3b.<\/p>\n<\/p>\n<\/p>\n \u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d, \u89c2\u5bdf\u8005\u548c\u88ab\u89c2\u5bdf\u8005\u7684\u8026\u5408\u6027\u4e0d\u5927.<\/p>\n 1\u4e2asubject\u53ef\u4ee5\u6709\u4efb\u610f\u6570\u76ee\u7684\u89c2\u5bdf\u8005Observer, \u7a0b\u5e8f\u733f\u4ee4subject\u53d1\u51fa\u901a\u77e5\u65f6\u6839\u672c\u65e0\u9700\u77e5\u9053\u89c2\u5bdf\u8005\u662f\u8c01, \u6709\u591a\u5c11\u89c2\u5bdf\u8005\u5b58\u5728.<\/p>\n \u800c\u5355\u4e2a\u89c2\u5bdf\u8005\u672c\u8eab\u4e5f\u65e0\u9700\u77e5\u9053\u5230\u5e95\u6709\u51e0\u4e2a\u5176\u4ed6\u89c2\u5bdf\u8005\u540c\u65f6\u5b58\u5728.<\/p>\n<\/p>\n<\/p>\n \u5f88\u660e\u663e\u561b, \u5c31\u662f\u5f531\u4e2a\u5bf9\u8c61\u53d1\u751f\u6539\u53d8\u7684\u540c\u65f6\u9700\u8981\u540c\u65f6\u6539\u53d8\u5176\u4ed6\u591a\u4e2a\u5bf9\u8c61\u65f6.<\/p>\n<\/p>\n \u800c\u4e14, \u89c2\u5bdf\u8005\u6a21\u5f0f\u4ee4\u5230\u8026\u5408\u7684\u53cc\u65b9, \u4f9d\u8d56\u4e0e\u63a5\u53e3(\u62bd\u8c61), \u800c\u4e0d\u662f\u4f9d\u8d56\u4e8e\u5177\u4f53(\u975e\u62bd\u8c61\u7c7b), \u7b26\u5408\u5c01\u95ed-\u5f00\u653e\u6a21\u5f0f.<\/p>\n<\/p>\n<\/p>\n \u53e61\u4e2a\u4f8b\u5b50\uff1a<\/p>\n <\/p>\n <\/p><\/p>\n","protected":false},"excerpt":{"rendered":"\u89c2\u5bdf\u8005\u6a21\u5f0f\u662f\u4ec0\u4e48\u610f\u601d_\u89c2\u5bdf\u8005\u6a21\u5f0f\u89e3\u51b3\u4ec0\u4e48\u95ee\u9898\u4e00,\u89c2\u5bdf\u8005\u6a21\u5f0f(Observer)\u7684\u5b9a\u4e49\u89c2\u5bdf\u8005\u6a21\u5f0f:\u5b9a\u4e49\u4e86\u4e00\u79cd1\u5bf9\u591a\u7684\u4f9d\u8d56\u5173\u7cfb,\u8ba9\u591a\u4e2a\u89c2...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/6661"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=6661"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/6661\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=6661"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=6661"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=6661"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}\u4e8c, \u89c2\u5bdf\u8005\u6a21\u5f0f(Observer) \u7684\u5404\u4e2a\u89d2\u8272.<\/span><\/h3>\n<\/p>\n
2.1 Observer (\u89c2\u5bdf\u8005)<\/span><\/h4>\n
2.2 Subject (\u901a\u77e5\u8005\/\u88ab\u89c2\u5bdf\u8005)<\/span><\/h4>\n
2.3 ConcreteSubject (\u5177\u4f53\u901a\u77e5\u8005\/\u88ab\u89c2\u5bdf\u8005)<\/span><\/h4>\n<\/p>\n
2.4 ConcreteObserver (\u5177\u4f53\u89c2\u5bdf\u8005\u7c7b)<\/span><\/h4>\n
\u4e09, 1\u4e2a\u5177\u4f53\u4f8b\u5b50\u548c\u4ee3\u7801.<\/span><\/h3>\n<\/p>\n
3.1 UML\u56fe<\/span><\/h4>\n<\/p>\n<\/p>\n
3.2 Subject\u63a5\u53e3 \u4ee3\u7801<\/span><\/h4>\n<\/p>\n
public interface Subject { public void attach(Observer obs); public void detach(Observer obs); public void sNotify(); \/\/notify is a finel method of Object class public int getState(); public void setState(int state); }<\/code><\/pre>\n
3.2 Observer\u63a5\u53e3 \u4ee3\u7801<\/span><\/h4>\n
public interface Observer { public void update(); } <\/code><\/pre>\n
3.3 Commander \u7c7b \u4ee3\u7801<\/span><\/h4>\n
import java.util.HashSet; import java.util.Iterator; public class Commander implements Subject{ private int targetPlaceID; private HashSet<Observer> gunnerSet = new HashSet<Observer>(); @Override public void attach(Observer obs){ this.gunnerSet.add(obs); } @Override public void detach(Observer obs) { this.gunnerSet.remove(obs); } @Override public void sNotify() { if (this.gunnerSet.isEmpty()){ return; } Iterator itr = this.gunnerSet.iterator(); while (itr.hasNext()){ Observer obs = (Observer)itr.next(); obs.update(); } } @Override public int getState() { \/\/ TODO Auto-generated method stub return this.targetPlaceID; } @Override public void setState(int state) { \/\/ TODO Auto-generated method stub this.targetPlaceID = state; } }<\/code><\/pre>\n
3.4 CannonShooter \u7c7b \u4ee3\u7801<\/span><\/h4>\n
public class CannonShooter implements Observer{ private Subject cmder; public CannonShooter(Subject cmder){ this.cmder = cmder; } public Subject getCmder() { return cmder; } public void setCmder(Subject cmder) { this.cmder = cmder; } public void fireCannon(int targetPlace){ System.out.println(this.getClass().getSimpleName() + \": fired on target(id:\" + targetPlace + \") by Cannon\"); } @Override public void update() { \/\/ TODO Auto-generated method stub fireCannon(cmder.getState()); } } <\/code><\/pre>\n
3.5 CannonFodder \u7c7b \u4ee3\u7801<\/span><\/h4>\n
public class CannonFodder implements Observer{ private int id; public CannonFodder(int id){ this.id = id; } public void getDown(){ System.out.println(this.getClass().getSimpleName() +\" id:\" + this.id + \" getDowned\"); } @Override public void update() { \/\/ TODO Auto-generated method stub this.getDown(); } }<\/code><\/pre>\n
3.6 CannonFodder \u7c7b \u4ee3\u7801<\/span><\/h4>\n<\/p>\n<\/p>\n
public class ClientObserver { public static void f(){ Commander cmder = new Commander(); CannonShooter cster = new CannonShooter(cmder); CannonFodder cfder1 = new CannonFodder(1); CannonFodder cfder2 = new CannonFodder(2); CannonFodder cfder3 = new CannonFodder(3); cmder.setState(107); cmder.attach(cster); cmder.attach(cfder1); cmder.attach(cfder2); cmder.attach(cfder3); cmder.sNotify(); cmder.setState(108); cmder.detach(cfder3); cmder.sNotify(); } } <\/code><\/pre>\n<\/p>\n<\/p>\n<\/p>\n
\u56db, \u89c2\u5bdf\u8005\u6a21\u5f0f\u7684\u7279\u70b9\u548c\u5e94\u7528\u8303\u56f4.<\/span><\/h3>\n<\/p>\n<\/p>\n
4.1 Observer\u6a21\u5f0f\u7684\u7279\u70b9<\/span><\/h4>\n<\/p>\n
4.2 \u4ec0\u4e48\u65f6\u5019\u5e94\u8be5\u4f7f\u7528Observer\u6a21\u5f0f<\/span><\/h4>\n<\/p>\n
package com.home.javacommon.designpattern.observer; import lombok.extern.slf4j.Slf4j; import java.util.ArrayList; import java.util.List; interface Observer{ void sNotify(); } interface Publisher<T extends Observer>{ void setObserver(T t); void update(); } class Target{ private String target; public String getTarget(){ return this.target; } public Target(String target){ this.target = target; } } @Slf4j class Commander implements Observer{ private Target target; public void setTarget(Target target){ log.info(\"Commander set target is {}: \", target.getTarget()); this.target = target; } public Target getTarget(){ return this.target; } private List<Publisher> publishersList = new ArrayList<>(); public void addPublisher(Publisher publisher){ this.publishersList.add(publisher); } @Override public void sNotify() { log.info(\"Commander ordered to fire the target: {}\", this.target.getTarget()); for (Publisher publisher : this.publishersList) { publisher.setObserver(this); publisher.update(); } } } @Slf4j class CannonShooter implements Publisher<Commander>{ private String name; public CannonShooter(String name){ this.name = name; } private Commander commander; @Override public void setObserver(Commander commander) { this.commander = commander; } @Override public void update() { log.info(\"Canoon Shooter: {} fired the target {}\", this.name, this.commander.getTarget().getTarget()); } } public class ObserverClient { public static void main(String[] args){ Commander mike = new Commander(); mike.setTarget(new Target(\"The Tree\")); CannonShooter jack = new CannonShooter(\"jack\"); CannonShooter bill = new CannonShooter(\"bill\"); mike.addPublisher(jack); mike.addPublisher(bill); mike.sNotify(); } } <\/code><\/pre>\n<\/p>\n