{"id":10006947,"date":"2022-12-03T12:05:11","date_gmt":"2022-12-03T04:05:11","guid":{"rendered":"https:\/\/ityet.com\/?p=10006947"},"modified":"2022-12-03T12:05:11","modified_gmt":"2022-12-03T04:05:11","slug":"%e5%b7%a5%e4%bd%9c%e4%b8%ad%e5%b8%b8%e7%94%a8%e7%9a%84%e8%ae%be%e8%ae%a1%e6%a8%a1%e5%bc%8f-%e4%ba%ab%e5%85%83%e6%a8%a1%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006947","title":{"rendered":"\u5de5\u4f5c\u4e2d\u5e38\u7528\u7684\u8bbe\u8ba1\u6a21\u5f0f&#8211;\u4eab\u5143\u6a21\u5f0f"},"content":{"rendered":"<blockquote><p>\u4e00\u822c\u505a\u4e1a\u52a1\u5f00\u53d1\uff0c\u4e0d\u592a\u5bb9\u6613\u6709\u5927\u91cf\u4f7f\u7528\u8bbe\u8ba1\u6a21\u5f0f\u7684\u573a\u666f\u3002\u8fd9\u91cc\u603b\u7ed3\u4e00\u4e0b\u5728\u4e1a\u52a1\u5f00\u53d1\u4e2d\u4f7f\u7528\u8f83\u4e3a\u9891\u7e41\u7684\u8bbe\u8ba1\u6a21\u5f0f\u3002\u5f53\u7136\u8bed\u8a00\u4e3aJava\uff0c\u57fa\u4e8eSpring\u6846\u67b6\u3002<\/p><\/blockquote>\n<h3>1 \u4eab\u5143\u6a21\u5f0f(Flyweight Pattern)<\/h3>\n<p>\u5982\u679c\u7cfb\u7edf\u4e2d\u5b58\u5728\u5927\u91cf\u7684\u91cd\u590d\u5bf9\u8c61\uff0c\u6216\u8005\u9700\u8981\u4e0d\u65ad\u5730\u521b\u5efa\u548c\u9500\u6bc1\u5927\u91cf\u7684\u91cd\u590d\u5bf9\u8c61\u3002\u8fd9\u65f6\u5c31\u53ef\u4ee5\u4f7f\u7528\u8be5\u6a21\u5f0f\uff0c\u7f13\u5b58\u8fd9\u4e9b\u91cd\u590d\u7684\u5bf9\u8c61\uff0c\u8fbe\u5230\u5171\u4eab\u7684\u76ee\u7684\uff0c\u4ece\u800c\u5927\u5927\u51cf\u5c11\u5185\u5b58\u5360\u7528\u3002<\/p>\n<p>\u4eab\u5143\u6a21\u5f0f\u4e00\u822c\u67093\u4e2a\u89d2\u8272:<\/p>\n<ul>\n<li>Flyweight: \u4eab\u5143\u7c7b(\u4e3b\u8981\u5b9a\u4e49\u5185\u90e8\u72b6\u6001\u4e0e\u5916\u90e8\u72b6\u6001)<\/li>\n<li>FlyweightFactory: \u4eab\u5143\u5de5\u5382\u7c7b(\u4e3b\u8981\u7528\u4e8e\u521b\u5efa\u548c\u7ba1\u7406\u4eab\u5143\u7c7b)<\/li>\n<\/ul>\n<h4>1.1 \u5b9e\u9645\u4e1a\u52a1\u573a\u666f<\/h4>\n<p>\u8fd9\u662f\u7531\u5b9e\u9645\u751f\u4ea7\u4e8b\u6545\u5f15\u53d1\uff0c\u53cd\u5411\u63a8\u52a8\u4f7f\u7528\u7684\u8bbe\u8ba1\u6a21\u5f0f\u3002\u56e0\u4e3a\u505a\u8425\u9500\u76f8\u5173\uff0c\u5f88\u591a\u7528\u6237\u7559\u8d44\u9875(\u843d\u5730\u9875)\u90fd\u4f1a\u4f7f\u7528\u5230\u56fd\u5bb6\u5730\u533a\u53ca\u7535\u8bdd\u56fd\u5bb6\u7801\u3002\u4f5c\u4e3a\u57fa\u7840\u6570\u636e\uff0c\u6211\u4eec\u8fd9\u8fb9\u5e76\u6ca1\u6709\u505a\u5b58\u50a8\uff0c\u751a\u81f3\u5f53\u65f6\u8fde\u7f13\u5b58\u4e5f\u6ca1\u505a\u3002\u5b8c\u5168\u7531\u57fa\u7840\u670d\u52a1\u7ec4\u63d0\u4f9b\u76f8\u5173<code>Feign<\/code>\u63a5\u53e3\u8c03\u7528\uff0c\u8bf4\u767d\u4e86\u6211\u4eec\u5c31\u662f\u4e2a\u4e2d\u95f4\u5546\u3002\u552f\u4e00\u505a\u7684\u5c31\u662f\u8981\u6839\u636e\u7528\u6237IP\u6765\u8bc6\u522b\u5f53\u524d\u56fd\u5bb6\u5730\u533a\u3001\u4ee5\u53ca\u5b58\u50a8Top15\u70ed\u70b9\u56fd\u5bb6\u5730\u533a\u6570\u636e\u3002<\/p>\n<p>\u8fd9\u4e00\u4efd\u6570\u636e\uff0c\u6211\u8bb0\u5f97\u5f53\u65f6\u7edf\u8ba1\u7684\u6709<code>30KB<\/code>\u5de6\u53f3\u3002\u4e5f\u5c31\u662f\u8bf4\u6bcf\u4e2a\u8bf7\u6c42\uff0c\u90fd\u4f1a\u5728\u5806\u4e2d\u521b\u5efa\u8fd9\u4e48\u4e00\u4e2a\u5bf9\u8c61\uff0c\u7d27\u63a5\u7740\u5c31\u6210\u4e3a\u4e86\u5783\u573e\u5bf9\u8c61\u3002\u5f53\u65f6\u662f\u5728\u505a\u4e00\u4e2a\u95ee\u5377\u8c03\u67e5\uff0c\u4f1a\u7ed9\u6240\u6709APP\u7528\u6237\u63a8\u9001\u3002\u670d\u52a1\u5668\u6700\u5f00\u59cb\u53ea\u6709\u4e00\u53f0\u65e0\u54cd\u5e94\uff0c\u7d27\u63a5\u7740\u53c8\u7b2c\u4e8c\u53f0\uff0c\u8fd0\u7ef4\u8fb9\u91cd\u542f\u673a\u5668\u8fb9\u5728\u8fb9\u5728\u544a\u8b66\u7fa4\u901a\u77e5\u3002<\/p>\n<p>\u7ecf\u6392\u67e5\uff0c\u5e76\u7ed3\u5408\u6700\u8fd1\u4e0a\u7ebf\u5185\u5bb9\uff0c\u53ca\u63a5\u53e3\u8bf7\u6c42\u76d1\u63a7\u6570\u636e\u3002\u8ba4\u5b9a\u662f\u7531\u4e8e\u56fd\u5bb6\u5730\u533a\u63a5\u53e3\u8bbf\u95ee\u91cf\u7a81\u589e\u5f15\u8d77\u7684\u3002\u4f46\u5177\u4f53\u539f\u56e0\u53ca\u89e3\u51b3\u65b9\u6848\u8fd8\u9700\u8981\u770b\u5177\u4f53\u4ee3\u7801\u903b\u8f91\u624d\u80fd\u786e\u5b9a\u3002<\/p>\n<p>\u4ecb\u7ecd\u5b8c\u4e1a\u52a1\u80cc\u666f\uff0c\u54b1\u4eec\u6765\u8bf4\u4e0b\u5927\u6982\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<blockquote><p>\u5730\u533a\u5143\u6570\u636e<\/p><\/blockquote>\n<pre><code class=\"json\">{\n    &quot;code&quot;: &quot;US&quot;,\n    &quot;nameZh&quot;: &quot;\u7f8e\u56fd&quot;,\n    &quot;nameEn&quot;: &quot;United States&quot;,\n    &quot;tel&quot;: &quot;+1&quot;,\n    &quot;pyName&quot;: &quot;mg&quot;,\n    &quot;sortNo&quot;: 1,\n    &quot;areaId&quot;: null\n}<\/code><\/pre>\n<blockquote><p>\u6574\u4f53\u7ed3\u6784<\/p><\/blockquote>\n<pre><code class=\"json\">{\n    &quot;allCountry&quot;: [],\n    &quot;commonCountry&quot;: [],\n    &quot;currentCountry&quot;: {}\n}<\/code><\/pre>\n<p>\u5728\u8fd9\u4efd\u6570\u636e\u4e2d\uff0c\u5f88\u5bb9\u6613\u770b\u5230\u3002<code>allCountry<\/code>\u57fa\u672c\u4e0d\u4f1a\u6539\u53d8\uff0c\u53ef\u4ee5\u8ba4\u4e3a\u662f\u5185\u90e8\u72b6\u6001\uff0c\u5f53\u7136\u8fd9\u4e5f\u662f\u5360\u7528\u7a7a\u95f4\u6700\u5927\u7684\u5730\u65b9\u3002<code>commonCountry<\/code>\u5728\u4e00\u5b9a\u65f6\u95f4\u5185\u57fa\u672c\u4e0d\u4f1a\u6539\u53d8\uff0c\u4f46\u6211\u4eec\u5728\u8fd9\u628a\u4ed6\u4f5c\u4e3a\u4e00\u4e2a\u5916\u90e8\u72b6\u6001\u5904\u7406(\u5185\u90e8\u5b9e\u73b0\u53ef\u901a\u8fc7\u7f13\u5b58\u6216\u5176\u4ed6\u65b9\u5f0f\u5b9e\u73b0)\u3002<code>currentCountry<\/code>\u9700\u8981\u6839\u636e\u8bf7\u6c42IP\u6765\u52a8\u6001\u751f\u6210\uff0c\u8fd9\u4e2a\u4f5c\u4e3a\u5916\u90e8\u53d8\u91cf\u5904\u7406\u3002\u81f3\u6b64\uff0c\u6574\u4f53\u601d\u8def\u5df2\u6e05\u6670\u3002<\/p>\n<h4>1.2 \u4ee3\u7801\u5b9e\u73b0<\/h4>\n<blockquote><p>\u56fd\u5bb6\u4fe1\u606f POJO<\/p><\/blockquote>\n<pre><code class=\"java\">@Data\npublic class Country {\n    private String code;\n    private String nameZh;\n    private String nameEn;\n    private String tel;\n    private String pyName;\n    private String sortNo;\n    private Integer areaId;\n}<\/code><\/pre>\n<blockquote><p>\u4eab\u5143\u7c7b(\u5185\u90e8\u72b6\u6001\u3001\u5916\u90e8\u72b6\u6001)<\/p><\/blockquote>\n<pre><code class=\"java\">@Getter\npublic class CountryList {\n    \/\/ \u5185\u90e8\u72b6\u6001\n    private final List&lt;Country&gt; allCountry;\n    \/\/ \u5916\u90e8\u72b6\u6001\n    private List&lt;Country&gt; commonCountry;\n    private Country currentCountry;\n\n    \/\/ \u5185\u90e8\u72b6\u6001\uff0c\u521b\u5efa\u5bf9\u8c61\u65f6\u8bbe\u7f6e\n    public CountryList(List&lt;Country&gt; allCountry) {\n        this.allCountry = allCountry;\n    }\n    \n    public void setCommonCountry(List&lt;Country&gt; commonCountry) {\n        this.commonCountry = commonCountry;\n    }\n\n    public void setCurrentCountry(Country currentCountry) {\n        this.currentCountry = currentCountry;\n    }\n}<\/code><\/pre>\n<blockquote><p>\u4eab\u5143\u5de5\u5382\u7c7b<\/p><\/blockquote>\n<pre><code class=\"java\">@Slf4j\n@Component\n@RequiredArgsConstructor\npublic class CountryListFactory {\n    private static final String DEFAULT = &quot;DEFAULT&quot;;\n    private static final Map&lt;String, CountryList&gt; CL_MAP = Maps.newHashMap();\n\n    private final CountryClient countryClient;\n\n    @PostConstruct\n    public void init() {\n        final List&lt;Country&gt; countryList = countryClient.getCountryList();\n        CL_MAP.put(DEFAULT, new CountryList(countryList));\n    }\n\n    public static CountryList getDefaultCl() {\n        return CL_MAP.get(DEFAULT);\n    }\n}<\/code><\/pre>\n<p>\u56e0\u4e3a\u672c\u4f8b\u7684\u4eab\u5143\u7c7b\u4e2d\uff0c\u5185\u90e8\u72b6\u6001\u53ea\u6709\u4e00\u79cd\u3002\u6240\u4ee5\u589e\u52a0\u4e86<code>DEFAULT<\/code>key\u503c\uff0c\u5b9e\u9645\u4e5f\u5c31\u8fd9\u4e00\u79cd\u3002<br \/><strong>\u5728\u8fd9\u91cc\u6709\u6ca1\u6709\u53d1\u73b0\u4ec0\u4e48???\u5982\u679c\u6ca1\u6709\u8fd9\u4e2a<code>DEFAULT<\/code>\uff0c\u8fd9\u4e0d\u5c31\u662f\u4e2a\u5355\u4f8b\u6a21\u5f0f\u5417???<\/strong> <br \/>\u8fd9\u5757\u4e4b\u6240\u4ee5\u8fd9\u6837\u5199\uff0c\u4e3b\u8981\u4e3a\u4e86\u66f4\u597d\u7684\u7406\u89e3\u4eab\u5143\u6a21\u5f0f\u672c\u8eab\u3002<\/p>\n<p><code>CountryClient<\/code>\u4e3a\u6a21\u62df\u5916\u90e8Feign\u63a5\u53e3\u670d\u52a1\uff0c\u63d0\u4f9b\u5168\u91cf\u56fd\u5bb6\u6570\u636e\u83b7\u53d6\u670d\u52a1\u3002\u5e76\u901a\u8fc7<code>@PostConstruct<\/code>\u5c06\u6570\u636e\u6ce8\u5165\u5230\u9759\u6001\u5b57\u5178\u4e2d\u3002\u901a\u8fc7\u8be5\u5de5\u5382\u7c7b\uff0c\u5c31\u53ef\u4ee5\u62ff\u5230<code>CountryList<\/code>\uff0c\u5728\u6839\u636e\u9700\u8981\u5c06<code>\u5916\u90e8\u72b6\u6001<\/code>\u6ce8\u5165\u8be5\u7c7b\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u4e86\u3002<\/p>\n<h4>1.4 \u5355\u6d4b<\/h4>\n<pre><code class=\"java\">@SpringBootTest\nclass CountryListTest {\n\n    @Test\n    void testFactory() {\n        final CountryList cl = CountryListFactory.getDefaultCl();\n        assertNotNull(cl);\n    }\n}<\/code><\/pre>\n<h3>2 \u601d\u8003<\/h3>\n<p>\u8ddf\u5355\u4f8b\u6a21\u5f0f\u6709\u5565\u533a\u522b\uff1f<\/p>\n<ul>\n<li>\u5355\u4f8b\u4e3b\u8981\u5728\u4e8e<code>\u5355<\/code>\uff0c\u5373\u5168\u5c40\u4ec5\u6709\u4e00\u4e2a\u5b9e\u4f8b\uff1b\u800c\u4eab\u5143\u4e3b\u8981\u5728\u4e8e<code>\u4eab<\/code>\uff0c\u5373\u5171\u4eab\u3002<\/li>\n<li>\u5728\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\uff0c\u53ef\u4ee5\u8ba4\u4e3a\u5355\u4f8b\u662f\u4eab\u5143\u7684\u4e00\u79cd\u7279\u6b8a\u60c5\u51b5\u3002<\/li>\n<li>\u4eab\u5143\u66f4\u4e3b\u8981\u7684\u662f\u901a\u8fc7\u5171\u4eab\uff0c\u8fbe\u5230\u8282\u7ea6\u5185\u5b58\u7684\u76ee\u7684\u3002<\/li>\n<\/ul>\n<p>\u8ddf\u7f13\u5b58\u6709\u4e9b\u7c7b\u4f3c\uff1f<\/p>\n<ul>\n<li>\u7f13\u5b58\uff0c\u4e3b\u8981\u76ee\u7684\u662f\u4e3a\u4e86\u52a0\u5feb\u8bbf\u95ee\u901f\u5ea6\u3002\u4eab\u5143\uff0c\u5728\u4e8e\u6570\u636e\u5171\u4eab\u8282\u7ea6\u7a7a\u95f4\u4e3a\u76ee\u7684\u3002<\/li>\n<li>\u7f13\u5b58\u662f\u4e00\u79cd\u601d\u60f3\uff0c\u53ef\u4ee5\u901a\u8fc7\u4eab\u5143\u6a21\u5f0f\u6765\u5b9e\u73b0\u7f13\u5b58\u3002<\/li>\n<\/ul>\n<p>\u5176\u4ed6\u7684\u4e00\u4e9b\u601d\u8003?<\/p>\n<ul>\n<li>\n<p>\u4eab\u5143\u7c7b\u4f3c\u5355\u4f8b\uff0c\u8fd9\u5c31\u53ef\u80fd\u4f1a\u6d89\u53ca\u5230\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\uff1f<\/p>\n<ul>\n<li>\u4e3b\u8981\u5728\u4e8e\u770b\u95ee\u9898\u7684\u89d2\u5ea6\uff0c\u5e76\u4e0d\u51b2\u7a81\u3002\u6d89\u53ca\u5230\u5e76\u53d1\uff0c\u8981\u8003\u8651\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u4e0d\u901a\u8fc7\u4eab\u5143\u6a21\u5f0f\uff0c\u4e5f\u80fd\u8fbe\u5230\u76f8\u540c\u7684\u6548\u679c\uff1f<\/p>\n<ul>\n<li>\u8bbe\u8ba1\u6a21\u5f0f\u662f\u5bf9\u7279\u5b9a(\u67d0\u4e9b\u573a\u666f)\u901a\u7528\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u53d8\u901a\u4e5f\u662f\u5f88\u91cd\u8981\u7684\u3002\u5148\u6709\u95ee\u9898\u624d\u6709\u89e3\u51b3\u65b9\u6848\uff0c\u800c\u4e0d\u662f\u5148\u6709\u8bbe\u8ba1\u6a21\u5f0f\u624d\u6709\u7279\u5b9a\u573a\u666f\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<hr>\n<p>\u5c01\u9762\u56fe\u6765\u6e90\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=yc5mCkk4aVpS5hQkku9wUA%3D%3D.BiTr1kR9wz8dmCa4pTINCjNHrR%2FxSAXo1I9U%2BK9H1N%2B3iCM%2B83JvYgz5DBSzQLKemtap8g87%2B44rFigm5K5izw%3D%3D\" rel=\"nofollow\">https:\/\/refactoring.guru\/desi&#8230;<\/a><\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042960543\">https:\/\/segmentfault.com\/a\/1190000042960543<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u822c\u505a\u4e1a\u52a1\u5f00\u53d1\uff0c\u4e0d\u592a\u5bb9\u6613\u6709\u5927\u91cf\u4f7f\u7528\u8bbe\u8ba1\u6a21\u5f0f\u7684\u573a\u666f\u3002\u8fd9\u91cc\u603b\u7ed3\u4e00\u4e0b\u5728\u4e1a\u52a1\u5f00\u53d1\u4e2d\u4f7f\u7528\u8f83\u4e3a\u9891\u7e41\u7684\u8bbe\u8ba1\u6a21\u5f0f\u3002\u5f53\u7136\u8bed\u8a00\u4e3aJ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10006947","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006947","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006947"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006947\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006947"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006947"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006947"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}