{"id":1001,"date":"2021-09-28T08:56:23","date_gmt":"2021-09-28T00:56:23","guid":{"rendered":"https:\/\/fireinsect.top\/?p=1001"},"modified":"2021-10-02T20:07:34","modified_gmt":"2021-10-02T12:07:34","slug":"%e8%ae%be%e8%ae%a1%e6%a8%a1%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/fireinsect.top\/index.php\/2021\/09\/28\/%e8%ae%be%e8%ae%a1%e6%a8%a1%e5%bc%8f\/","title":{"rendered":"\u8bbe\u8ba1\u6a21\u5f0f"},"content":{"rendered":"\n<p>\u8bbe\u8ba1\u6a21\u5f0f\u662f\u524d\u7a0b\u5e8f\u5458\u603b\u7ed3\u51fa\u6765\u7684\u6700\u4f73\u5b9e\u8df5\uff0c\u7ed3\u679c\u65f6\u95f4\u6c89\u6dc0\uff0c\u5f62\u6210\u7684\u4e00\u5957\u89e3\u51b3\u95ee\u9898\u7684\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>\u901a\u5e38\uff0c\u4e00\u4e2a\u8bbe\u8ba1\u6a21\u5f0f\u4e13\u6ce8\u4e8e\u89e3\u51b3\u4e00\u7c7b\u95ee\u9898\uff0c\u4e0d\u4f1a\u6709\u4e07\u80fd\u7684\u8bbe\u8ba1\u6a21\u5f0f<\/p>\n\n\n\n<!--more-->\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-large-font-size\">\u5355\u4f8b\u6a21\u5f0f<\/p>\n\n\n\n<p>\u5f53\u6570\u636e\u5bf9\u8c61\u5b58\u5728 \u552f\u4e00 \u73b0\u8c61<\/p>\n\n\n\n<p>\u5982\u4e00\u4e2a\u73ed\u7ea7\u53ea\u6709\u4e00\u4e2a\u73ed\u4e3b\u4efb\uff0c\u5219\u4f7f\u7528\u5355\u4f8b\u6a21\u5f0f\uff0c\u5373\u4fdd\u8bc1\u4e00\u4e2a\u7c7b\u4ec5\u6709\u4e00\u4e2a\u5b9e\u4f8b<\/p>\n\n\n\n<p>\u5355\u4f8b\u6a21\u5f0f\u7684\u6838\u5fc3\u529e\u6cd5\u662f\u628a\u6784\u9020\u51fd\u6570\u8bbe\u7f6e\u4e3a\u79c1\u6709\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\"><strong>public<\/strong> <strong>class<\/strong> <strong>ClassMaster<\/strong> {\n  <strong>private<\/strong> String id;\n  <em>\/\/ \u73ed\u4e3b\u4efb\u540d\u79f0<\/em>\n  <strong>private<\/strong> String name;\n  <strong>private<\/strong> String gender;\n\n  <em>\/\/ \u552f\u4e00\u5b9e\u4f8b<\/em>\n  <strong>private<\/strong> <strong>static<\/strong> ClassMaster instance = <strong>new<\/strong> ClassMaster();\n\n  <strong>private<\/strong> <strong>ClassMaster<\/strong>() {\n  }\n}<\/code><\/pre>\n\n\n\n<p>\u518d\u5728\u81ea\u5df1\u7c7b\u7684\u5185\u90e8\u5b9e\u4f8b\u5316\u81ea\u5df1\uff0c\u4ee5\u6b64\u4e0d\u5141\u8bb8\u5176\u4ed6\u7c7b\u5b9e\u4f8b\u5316\u8fd9\u4e2a\u7c7b\uff0c\u53ea\u6709\u81ea\u5df1\u80fd\u5b9e\u4f8b\u5316\u4e00\u4e2a\u552f\u4e00\u7684\u81ea\u5df1\u3002<\/p>\n\n\n\n<p>\u6b64\u65f6\uff0c\u6211\u4eec\u9700\u8981\u6dfb\u52a0\u4e00\u4e2aget\u65b9\u6cd5\u6765\u83b7\u53d6\u8fd9\u4e2a\u5b9e\u4f8b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\"> <strong>public<\/strong> <strong>static<\/strong> ClassMaster <strong>getInstance<\/strong>() {\n    <strong>return<\/strong> instance;\n  }<\/code><\/pre>\n\n\n\n<div style=\"height:24px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-large-font-size\">\u7b80\u5355\u5de5\u5382\u6a21\u5f0f<\/p>\n\n\n\n<p>\u7b80\u5355\u5de5\u5382\u6a21\u5f0f\u89e3\u51b3\u7684\u95ee\u9898\u662f\uff1a\u4ee3\u7801\u91cd\u590d\u3001\u8026\u5408\u7d27\u5bc6<\/p>\n\n\n\n<p>\u5982\u4ee5\u6c34\u679c\u5546\u5e97\u4e3a\u4f8b\uff0c\u4e3a\u6ee1\u8db3\u4e0d\u540c\u53e3\u5473\u987e\u5ba2\uff0c\u5546\u5e97\u9700\u8981\u6839\u636e\u53e3\u5473\u5355\u72ec\u5c06\u5355\u4e2a\u6c34\u679c\u5b9e\u4f8b\u5316\u5982\uff08\u4f2a\u4ee3\u7801\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\"><strong>public<\/strong> <strong>class<\/strong> \u9910\u9986 {\n  <strong>public<\/strong> <strong>static<\/strong> <strong>void<\/strong> <strong>main<\/strong>(String[] args) {\n    <strong>if<\/strong> (\u5ba2\u4eba.get\u53e3\u5473() == \"\u751c\") {\n      \u897f\u74dc w = <strong>new<\/strong> \u897f\u74dc();\n    } <strong>else<\/strong> <strong>if<\/strong> (\u5ba2\u4eba.get\u53e3\u5473() == \"\u9178\") {\n      \u67e0\u6aac l = <strong>new<\/strong> \u67e0\u6aac();\n    } <strong>else<\/strong> <strong>if<\/strong> (\u5ba2\u4eba.get\u53e3\u5473() == \"\u81ed\") {\n      \u69b4\u83b2 d = <strong>new<\/strong> \u69b4\u83b2();\n    }\n  }\n}<\/code><\/pre>\n\n\n\n<p>\u800c\u8fd9\u6837\u7684\u60c5\u51b5\u53ef\u4ee5\u4f7f\u7528\u7b80\u5355\u5de5\u5382\u6a21\u5f0f\u4ee3\u66ff<\/p>\n\n\n\n<p>\u7b80\u5355\u5de5\u5382\u6a21\u5f0f\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/style.youkeda.com\/img\/ham\/course\/j5\/j5-4-3-2.svg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\"><strong>public<\/strong> <strong>class<\/strong> <strong>FruitFactory<\/strong> {\n    <strong>public<\/strong> <strong>static<\/strong> Fruit <strong>getFruit<\/strong>(Customer customer) {\n        Fruit fruit = <strong>null<\/strong>;\n        <strong>if<\/strong> (\"sweet\".equals(customer.getFlavor())) {\n            fruit = <strong>new<\/strong> Watermelon();\n        } <strong>else<\/strong> <strong>if<\/strong> (\"acid\".equals(customer.getFlavor())) {\n            fruit = <strong>new<\/strong> Lemon();\n        } <strong>else<\/strong> <strong>if<\/strong> (\"smelly\".equals(customer.getFlavor())) {\n            fruit = <strong>new<\/strong> Durian();\n        }\n\n        <strong>return<\/strong> fruit;\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u6837\u6765\u5b8c\u6210\u4ee3\u7801\uff0c\u53ef\u4ee5\u66f4\u6709\u6548\u5b8c\u6210\u89e3\u8026\u4ee5\u53ca\u89e3\u51b3\u4ee3\u7801\u91cd\u590d\u95ee\u9898<\/p>\n\n\n\n<p>\u5de5\u5382\u7c7b\u4e3a\u4e86\u63d0\u9ad8\u8fa8\u8bc6\u5ea6\uff0c\u4e00\u822c\u4ee5 XXXFactory\u4e3a\u540d<\/p>\n\n\n\n<div style=\"height:33px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-large-font-size\">\u62bd\u8c61\u5de5\u7a0b\u6a21\u5f0f<\/p>\n\n\n\n<p>\u7b80\u5355\u5de5\u5382\u9002\u5408\u521b\u5efa\u4e00\u79cd\u5bf9\u8c61\uff0c\u4f46\u5f53\u60c5\u51b5\u590d\u6742\uff0c\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u7cfb\u5217\u3001\u591a\u79cd\u4ea7\u54c1\u7684\u65f6\u5019\uff0c\u5c31\u9700\u8981\u62bd\u8c61\u5de5\u5382\u6a21\u5f0f\u3002<\/p>\n\n\n\n<p>\u5982\u6c34\u679c\u8d85\u5e02\u9664\u4e86\u5356\u6c34\u679c\uff0c\u8fd8\u9700\u8981\u63d0\u4f9b\u996e\u6599\uff0c\u9700\u8981\u5728\u201c\u6c34\u679c\u5de5\u5382\u201d\u4e4b\u5916\u521b\u5efa\u4e00\u4e2a\u201c\u996e\u6599\u5de5\u5382\u201d\uff0c\u6211\u4eec\u53ef\u4ee5\u591a\u521b\u5efa\u4e00\u4e2a\u5de5\u5382\u62bd\u8c61\u7c7b\u628a\u591a\u4e2a\u5de5\u5382\u8fdb\u4e00\u6b65\u62bd\u8c61\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/style.youkeda.com\/img\/ham\/course\/j5\/j5-4-4-1.svg\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u5176\u4e2d\u7531\u4e8e\u6c34\u679c\u5de5\u5382\u9700\u8981\u7ee7\u627f\u63a5\u53e3\uff0c\u9700\u8981\u6dfb\u52a0getDrink()\u65b9\u6cd5\uff0c\u6b64\u65f6\u8fd4\u56denull\u5c31\u884c<\/p>\n\n\n\n<p><strong>SnacksFactoryBuilder<\/strong> \u65b9\u6cd5\u662f\u751f\u4ea7\u5de5\u5382\u7684\u5de5\u5382\uff0c\u5982\u5de5\u5382\u662f\u751f\u4ea7\u4ea7\u54c1\u7684\u5de5\u5382\uff0c\u5176\u4f5c\u7528\u662f\u521b\u5efa\u5de5\u5382\u5b9e\u4f8b\u3002<\/p>\n\n\n\n<p>  <\/p>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-large-font-size\">\u89c2\u5bdf\u8005\u6a21\u5f0f<\/p>\n\n\n\n<p>\u89c2\u5bdf\u8005\u6a21\u5f0f\u7684\u6838\u5fc3\u662f\u8981\u77e5\u9053\u89c2\u5bdf\u4ec0\u4e48\uff0c\u4ec0\u4e48\u5bf9\u8c61\u53d1\u751f\u4e86\u53d8\u5316\u9700\u8981\u53d1\u51fa\u901a\u77e5\uff0c\u5982\u5929\u6c14\u7cfb\u7edf\u7684\u8ba2\u9605\/\u901a\u77e5\u529f\u80fd\u3002<\/p>\n\n\n\n<p>\u5982\u5929\u6c14\u7cfb\u7edf\uff0c\u6211\u4eec\u9700\u8981\u89c2\u5bdf\u7684\u662f\u5929\u6c14\u7684\u53d8\u5316\uff0c\u6211\u4eec\u62bd\u8c61\u51fa\u5929\u6c14\u7684\u5bf9\u8c61\uff0c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\"><strong>public<\/strong> <strong>class<\/strong> <strong>WeatherData<\/strong> <strong>extends<\/strong> <strong>Observable<\/strong><\/code><\/pre>\n\n\n\n<p>\u8ba9\u5b83\u7ee7\u627fJava\u63d0\u4f9b\u7684 <strong>Observable<\/strong> \u7c7b\uff0c\u8868\u793a\u5176\u662f\u6838\u5fc3\u7684\u3001\u9700\u8981\u88ab\u89c2\u5bdf\u7684\u7c7b\u3002\u88ab\u79f0\u4e3a<strong>\u88ab\u89c2\u5bdf\u8005<\/strong><\/p>\n\n\n\n<p>\u5982<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\"><strong>public<\/strong> <strong>class<\/strong> <strong>WeatherData<\/strong> <strong>extends<\/strong> <strong>Observable<\/strong> {\n    <em>\/\/ \u57ce\u5e02<\/em>\n    <strong>private<\/strong> String cityName;\n    <em>\/\/ \u65f6\u95f4<\/em>\n    <strong>private<\/strong> String time;\n    <em>\/\/ \u6e29\u5ea6<\/em>\n    <strong>private<\/strong> String temp;\n\n    <em>\/\/ \u57ce\u5e02\u56fa\u5b9a\u4e86\u5c31\u4e0d\u53d8\u4e86<\/em>\n    <strong>public<\/strong> <strong>WeatherData<\/strong>(String cityName) {\n        <strong>this<\/strong>.cityName = cityName;\n    }\n\n    <em>\/\/ \u6253\u5370\u5929\u6c14\u4fe1\u606f<\/em>\n    <strong>public<\/strong> String <strong>toString<\/strong>() {\n        <strong>return<\/strong> cityName + \"\uff0c\" + LocalDate.now().toString() + \" \" + time + \"\uff0c\u6c14\u6e29\uff1a\" + temp + \"\u6444\u6c0f\u5ea6\u3002\";\n    }\n\n    <strong>public<\/strong> String <strong>getCityName<\/strong>() {\n        <strong>return<\/strong> cityName;\n    }\n\n    <strong>public<\/strong> String <strong>getTime<\/strong>() {\n        <strong>return<\/strong> time;\n    }\n\n    <strong>public<\/strong> String <strong>getTemp<\/strong>() {\n        <strong>return<\/strong> temp;\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u7136\u540e\u6211\u4eec\u9700\u8981\u5728\u6570\u636e\u53d8\u5316\u540e\u53d1\u8d77\u901a\u77e5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\"><strong>public<\/strong> <strong>class<\/strong> <strong>WeatherData<\/strong> <strong>extends<\/strong> <strong>Observable<\/strong> {\n    <em>\/**\n     * \u4e00\u4e2a\u57ce\u5e02\u7684\u6c14\u6e29\u5728\u67d0\u4e2a\u65f6\u523b\u53d1\u751f\u4e86\u53d8\u5316\n     *\/<\/em>\n    <strong>public<\/strong> <strong>void<\/strong> <strong>changeTemp<\/strong>(String time, String temp) {\n        <strong>if<\/strong>(time == <strong>null<\/strong> || temp == <strong>null<\/strong>) {\n            <em>\/\/ \u8f93\u5165\u6570\u636e\u4e3a\u7a7a\u662f\u6709\u95ee\u9898\u7684\uff0c\u4e0d\u5904\u7406<\/em>\n            <strong>return<\/strong>;\n        }\n\n        <em>\/\/ \u4e0e\u539f\u6570\u636e\u4e0d\u540c\uff0c\u8bf4\u660e\u53d1\u751f\u4e86\u53d8\u5316<\/em>\n        <strong>if<\/strong>(!time.equals(<strong>this<\/strong>.time) || !temp.equals(<strong>this<\/strong>.temp)) {\n            <em>\/\/ \u6807\u8bb0\u53d8\u5316<\/em>\n            <strong>super<\/strong>.setChanged();\n            <strong>this<\/strong>.time = time;\n            <strong>this<\/strong>.temp = temp;\n            <em>\/\/ \u53d1\u51fa\u901a\u77e5\uff0c\u53c2\u6570\u662f\u989d\u5916\u7684\u4fe1\u606f<\/em>\n            <strong>super<\/strong>.notifyObservers(\"\u6e29\u5ea6\u53d8\u5316\u5df2\u901a\u77e5\");\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u4f7f\u7528\u4e86\u7236\u7c7b\u65b9\u6cd5 <strong>setChanged()<\/strong> \u548c <strong>notifyObservers()<\/strong> <\/p>\n\n\n\n<p> <strong>setChanged()<\/strong> \u662f\u6807\u8bb0\u88ab\u89c2\u5bdf\u8005\u5bf9\u8c61\u53d1\u751f\u4e86\u53d8\u5316<\/p>\n\n\n\n<p> <strong>notifyObservers()<\/strong> \u5c31\u662f\u53d1\u51fa\u901a\u77e5\uff0c\u5982\u679c\u9700\u8981\u53d1\u751f\u989d\u5916\uff08\u4e0d\u5728\u89c2\u5bdf\u8005\u5bf9\u8c61\u5185\uff09\u7684\u4fe1\u606f\uff0c\u5728\u53c2\u6570\u4e2d\u4f20\u5165\u4fe1\u606f\u5bf9\u8c61\u5373\u53ef\u3002<\/p>\n\n\n\n<p>\u5b58\u5728\u88ab\u89c2\u5bdf\u8005\uff0c\u81ea\u7136\u4e5f\u8981\u521b\u5efa\u89c2\u5bdf\u8005\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\"><strong>public<\/strong> <strong>class<\/strong> <strong>WeatherObserver<\/strong> <strong>implements<\/strong> <strong>Observer<\/strong> {\n    <strong>private<\/strong> String name;\n\n    <strong>@Override<\/strong>\n    <strong>public<\/strong> <strong>void<\/strong> <strong>update<\/strong>(Observable o, Object arg) {\n        System.out.print(<strong>this<\/strong>.name + \"\u89c2\u5bdf\u5230\u5929\u6c14\u53d8\u5316\u4e3a\uff1a\");\n        System.out.print(o.toString());\n        System.out.println(\" \" + arg);\n    }\n\n    <strong>public<\/strong> String <strong>getName<\/strong>() {\n        <strong>return<\/strong> name;\n    }\n\n    <strong>public<\/strong> <strong>void<\/strong> <strong>setName<\/strong>(String name) {\n        <strong>this<\/strong>.name = name;\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u89c2\u5bdf\u8005\u9700\u8981\u5b9e\u73b0 <strong>update()<\/strong> \u65b9\u6cd5\u5176\u4e2d\uff0c<\/p>\n\n\n\n<p>\u65b9\u6cd5\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u88ab\u89c2\u5bdf\u8005\u5bf9\u8c61(\u7ee7\u627fObservable) <\/p>\n\n\n\n<p>\u65b9\u6cd5\u7b2c\u4e8c\u4e2a\u53c2\u6570\u5c31\u662f\u989d\u5916\u4fe1\u606f\uff0c\u5177\u4f53\u5c31\u662f\u8c03\u7528 <code>super.notifyObservers()<\/code> \u65f6\u4f20\u5165\u7684\u53c2\u6570\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u4e0d\u60f3\u53d1\u751f\u989d\u5916\u4fe1\u606f\uff0c\u5c31\u662f <code>super.notifyObservers(null)<\/code> \u6b64\u65f6arg\u503c\u5c31\u662fnull\u3002<\/p>\n\n\n\n<p>\u5f53\u6211\u4eec\u5728 main\u4e2d\u8c03\u7528\u65f6\uff0c\u88ab\u89c2\u5bdf\u8005\u9700\u8981\u6dfb\u52a0\u89c2\u5bdf\u8005\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">        <em>\/\/ \u57ce\u5e02\u5929\u6c14\u6570\u636e<\/em>\n        WeatherData weatherData = <strong>new<\/strong> WeatherData(\"\u4f59\u676d\");\n        <em>\/\/ \u6dfb\u52a0\u89c2\u5bdf\u8005<\/em>\n        weatherData.addObserver(w1);\n        weatherData.addObserver(w2);<\/code><\/pre>\n\n\n\n<p> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8bbe\u8ba1\u6a21\u5f0f\u662f\u524d\u7a0b\u5e8f\u5458\u603b\u7ed3\u51fa\u6765\u7684\u6700\u4f73\u5b9e\u8df5\uff0c\u7ed3\u679c\u65f6\u95f4\u6c89\u6dc0\uff0c\u5f62\u6210\u7684\u4e00\u5957\u89e3\u51b3\u95ee\u9898\u7684\u65b9\u6cd5\u3002 \u901a\u5e38\uff0c\u4e00\u4e2a\u8bbe\u8ba1\u6a21\u5f0f\u4e13\u6ce8\u4e8e\u89e3\u51b3\u4e00 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[6],"tags":[],"class_list":["post-1001","post","type-post","status-publish","format-standard","hentry","category-6"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/1001"}],"collection":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/comments?post=1001"}],"version-history":[{"count":5,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/1001\/revisions"}],"predecessor-version":[{"id":1008,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/1001\/revisions\/1008"}],"wp:attachment":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/media?parent=1001"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/categories?post=1001"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/tags?post=1001"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}