{"id":95,"date":"2023-02-21T09:02:08","date_gmt":"2023-02-21T09:02:08","guid":{"rendered":"https:\/\/blessingcr.com\/?p=95"},"modified":"2023-02-21T09:02:10","modified_gmt":"2023-02-21T09:02:10","slug":"%e7%ad%96%e7%95%a5%e6%a8%a1%e5%bc%8f-%e6%9b%bf%e4%bb%a3if-else","status":"publish","type":"post","link":"https:\/\/blessingcr.com\/?p=95","title":{"rendered":"\u7b56\u7565\u6a21\u5f0f--\u66ff\u4ee3if-else"},"content":{"rendered":"\n<p>\u7528\u4e8e\u53d6\u4ee3\u4ee5\u4e0b\u4ee3\u7801<\/p>\n\n\n\n<p>switch(resourceType){<\/p>\n\n\n\n<p>&nbsp;case \"\u7ea2\u5305\":&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;String grantType=new Context(new RedPaper()).ContextInterface();<\/p>\n\n\n\n<p>&nbsp;&nbsp;break;<\/p>\n\n\n\n<p>&nbsp;case \"\u8d2d\u7269\u5238\":&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;String grantType=new Context(new Shopping()).ContextInterface();<\/p>\n\n\n\n<p>&nbsp;&nbsp;break;<\/p>\n\n\n\n<p>&nbsp;......<\/p>\n\n\n\n<p>&nbsp;default : logger.info(\"\u67e5\u627e\u4e0d\u5230\u8be5\u4f18\u60e0\u5238\u7c7b\u578bresourceType\u4ee5\u53ca\u5bf9\u5e94\u7684\u6d3e\u53d1\u65b9\u5f0f\");<\/p>\n\n\n\n<p>&nbsp;&nbsp;break;<\/p>\n\n\n\n<p>\u5b9e\u73b0\u65b9\u5f0f\uff1a<\/p>\n\n\n\n<p>@Service<\/p>\n\n\n\n<p>public class GrantTypeService {<\/p>\n\n\n\n<p>&nbsp; &nbsp;public String redPaper(String resourceId){<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp;\/\/\u7ea2\u5305\u7684\u53d1\u653e\u65b9\u5f0f<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp;return \"\u6bcf\u5468\u672b9\u70b9\u53d1\u653e\";<\/p>\n\n\n\n<p>&nbsp; &nbsp;}<\/p>\n\n\n\n<p>&nbsp; &nbsp;public String shopping(String resourceId){<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp;\/\/\u8d2d\u7269\u5238\u7684\u53d1\u653e\u65b9\u5f0f<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp;return \"\u6bcf\u5468\u4e099\u70b9\u53d1\u653e\";<\/p>\n\n\n\n<p>&nbsp; &nbsp;}<\/p>\n\n\n\n<p>&nbsp; &nbsp;public String QQVip(String resourceId){<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp;\/\/qq\u4f1a\u5458\u7684\u53d1\u653e\u65b9\u5f0f<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp;return \"\u6bcf\u5468\u4e000\u70b9\u5f00\u59cb\u79d2\u6740\";<\/p>\n\n\n\n<p>&nbsp; &nbsp;}<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>@Service<\/p>\n\n\n\n<p>public class QueryGrantTypeService {<\/p>\n\n\n\n<p>&nbsp; &nbsp;@Autowired<\/p>\n\n\n\n<p>&nbsp; &nbsp;private GrantTypeService grantTypeSerive;<\/p>\n\n\n\n<p>&nbsp; &nbsp;private Map&lt;String, Function&lt;String,String&gt;&gt; grantTypeMap=new ConcurrentHashMap&lt;&gt;();<\/p>\n\n\n\n<p>&nbsp; &nbsp;\/**<\/p>\n\n\n\n<p>&nbsp; &nbsp; *&nbsp; \u521d\u59cb\u5316\u4e1a\u52a1\u5206\u6d3e\u903b\u8f91,\u4ee3\u66ff\u4e86if-else\u90e8\u5206<\/p>\n\n\n\n<p>&nbsp; &nbsp; *&nbsp; key: \u4f18\u60e0\u5238\u7c7b\u578b<\/p>\n\n\n\n<p>&nbsp; &nbsp; *&nbsp; value: lambda\u8868\u8fbe\u5f0f,\u6700\u7ec8\u4f1a\u83b7\u5f97\u8be5\u4f18\u60e0\u5238\u7684\u53d1\u653e\u65b9\u5f0f<\/p>\n\n\n\n<p>&nbsp; &nbsp; *\/<\/p>\n\n\n\n<p>&nbsp; &nbsp;@PostConstruct<\/p>\n\n\n\n<p>&nbsp; &nbsp;public void dispatcherInit(){<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; grantTypeMap.put(\"\u7ea2\u5305\",resourceId-&gt;grantTypeSerive.redPaper(resourceId));<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; grantTypeMap.put(\"\u8d2d\u7269\u5238\",resourceId-&gt;grantTypeSerive.shopping(resourceId));<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; grantTypeMap.put(\"qq\u4f1a\u5458\",resourceId-&gt;grantTypeSerive.QQVip(resourceId));<\/p>\n\n\n\n<p>&nbsp; &nbsp;}<\/p>\n\n\n\n<p>&nbsp; &nbsp;public String getResult(String resourceType){<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp;\/\/Controller\u6839\u636e \u4f18\u60e0\u5238\u7c7b\u578bresourceType\u3001\u7f16\u7801resourceId \u53bb\u67e5\u8be2 \u53d1\u653e\u65b9\u5f0fgrantType<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp;Function&lt;String,String&gt; result=grantTypeMap.get(resourceType);<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp;if(result!=null){<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/\u4f20\u5165resourceId \u6267\u884c\u8fd9\u6bb5\u8868\u8fbe\u5f0f\u83b7\u5f97String\u578b\u7684grantType<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return result.apply(resourceType);<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp;}<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp;return \"\u67e5\u8be2\u4e0d\u5230\u8be5\u4f18\u60e0\u5238\u7684\u53d1\u653e\u65b9\u5f0f\";<\/p>\n\n\n\n<p>&nbsp; &nbsp;}<\/p>\n\n\n\n<p>}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7528\u4e8e\u53d6\u4ee3\u4ee5\u4e0b\u4ee3\u7801 switch(resourceType){ &nbsp;case &#8220;\u7ea2\u5305&#8221;:&nbsp; &#038; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[48,43,44],"class_list":["post-95","post","type-post","status-publish","format-standard","hentry","category-15","tag-b","tag-43","tag-44"],"_links":{"self":[{"href":"https:\/\/blessingcr.com\/index.php?rest_route=\/wp\/v2\/posts\/95","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blessingcr.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blessingcr.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blessingcr.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blessingcr.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=95"}],"version-history":[{"count":1,"href":"https:\/\/blessingcr.com\/index.php?rest_route=\/wp\/v2\/posts\/95\/revisions"}],"predecessor-version":[{"id":96,"href":"https:\/\/blessingcr.com\/index.php?rest_route=\/wp\/v2\/posts\/95\/revisions\/96"}],"wp:attachment":[{"href":"https:\/\/blessingcr.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=95"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blessingcr.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=95"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blessingcr.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=95"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}