{"id":3786,"date":"2024-05-21T20:47:19","date_gmt":"2024-05-21T12:47:19","guid":{"rendered":"https:\/\/xinchewhd.com.cn\/index.php\/%e8%ae%be%e8%ae%a1%e6%a8%a1%e5%bc%8f\/%e6%b6%88%e9%99%a4if-else%e7%a5%9e%e5%99%a8-%e7%ad%96%e7%95%a5%e6%a8%a1%e5%bc%8f%ef%bc%88%e7%bb%8f%e5%85%b8%e8%ae%be%e8%ae%a1%e6%a8%a1%e5%bc%8f%ef%bc%89\/"},"modified":"2024-05-24T15:07:35","modified_gmt":"2024-05-24T07:07:35","slug":"%e6%b6%88%e9%99%a4if-else%e7%a5%9e%e5%99%a8-%e7%ad%96%e7%95%a5%e6%a8%a1%e5%bc%8f%ef%bc%88%e7%bb%8f%e5%85%b8%e8%ae%be%e8%ae%a1%e6%a8%a1%e5%bc%8f%ef%bc%89","status":"publish","type":"post","link":"https:\/\/xinchewhd.com.cn\/index.php\/%e8%ae%be%e8%ae%a1%e6%a8%a1%e5%bc%8f\/%e6%b6%88%e9%99%a4if-else%e7%a5%9e%e5%99%a8-%e7%ad%96%e7%95%a5%e6%a8%a1%e5%bc%8f%ef%bc%88%e7%bb%8f%e5%85%b8%e8%ae%be%e8%ae%a1%e6%a8%a1%e5%bc%8f%ef%bc%89\/","title":{"rendered":"\u6d88\u9664if else\u795e\u5668-\u7b56\u7565\u6a21\u5f0f\uff08\u7ecf\u5178\u8bbe\u8ba1\u6a21\u5f0f\uff09"},"content":{"rendered":"<div class=\"wxsyncmain\">\n<p style=\"text-align: center; margin-bottom: 8px; margin-left: 8px; margin-right: 8px;\"><span style=\"color: #00d5ff;\"><strong><span style=\"color: #00d5ff; font-size: 18px;\">\u7b56\u7565\u6a21\u5f0f\u4ecb\u7ecd<\/span><\/strong><\/span><\/p>\n<p style=\"text-align: left; margin-bottom: 8px; margin-left: 8px; margin-right: 8px;\"><span style=\"font-size: 16px;\">\u7b56\u7565\u6a21\u5f0f\u5c5e\u4e8eJava23\u79cd\u8bbe\u8ba1\u6a21\u5f0f\u4e2d\u7684\u884c\u4e3a\u578b\u6a21\u5f0f\u3002\u5728\u5b9e\u9645\u9879\u76ee\u5f00\u53d1\u4f7f\u7528\u8f83\u591a\u3002\u4e5f\u662f\u9762\u8bd5\u7ecf\u5e38\u4f1a\u95ee\u5230\u7684\u77e5\u8bc6\u70b9\u3002<\/span><\/p>\n<p style=\"text-align: left; margin-bottom: 8px; margin-left: 8px; margin-right: 8px;\"><span style=\"font-size: 16px;\">\u7b56\u7565\u6a21\u5f0f(strategy pattern)\u7684\u539f\u59cb\u5b9a\u4e49\u662f\uff1a\u5b9a\u4e49\u4e00\u7cfb\u5217\u7b97\u6cd5\uff0c\u5c06\u6bcf\u4e00\u4e2a\u7b97\u6cd5\u5c01\u88c5\u8d77\u6765\uff0c\u5e76\u4f7f\u5b83\u4eec\u53ef\u4ee5\u76f8\u4e92\u66ff\u6362\u3002\u7b56\u7565\u6a21\u5f0f\u8ba9\u7b97\u6cd5\u53ef\u4ee5\u72ec\u7acb\u4e8e\u4f7f\u7528\u5b83\u7684\u5ba2\u6237\u7aef\u800c\u53d8\u5316\u3002<\/span><\/p>\n<p style=\"margin-bottom: 8px; margin-left: 8px; margin-right: 8px;\"><span style=\"font-size: 16px;\">\u7b56\u7565\u6a21\u5f0f\u7684\u4e3b\u8981\u89d2\u8272\u5982\u4e0b\uff1a<\/span><\/p>\n<p style=\"margin-bottom: 8px; margin-left: 8px; margin-right: 8px;\"><span style=\"font-size: 16px;\"><span style=\"color: #ff0000;\">\u62bd\u8c61\u7b56\u7565\uff08Strategy\uff09\u7c7b<\/span>\uff1a\u8fd9\u662f\u4e00\u4e2a\u62bd\u8c61\u89d2\u8272\uff0c\u901a\u5e38\u7531\u4e00\u4e2a\u63a5\u53e3\u6216\u62bd\u8c61\u7c7b\u5b9e\u73b0\u3002\u6b64\u89d2\u8272\u7ed9\u51fa\u6240\u6709\u7684\u5177\u4f53\u7b56\u7565\u7c7b\u6240\u9700\u7684\u63a5\u53e3\u3002<\/span><\/p>\n<p style=\"margin-bottom: 8px; margin-left: 8px; margin-right: 8px;\"><span style=\"font-size: 16px;\"><span style=\"color: #ff0000;\">\u5177\u4f53\u7b56\u7565\uff08Concrete Strategy\uff09\u7c7b<\/span>\uff1a\u5b9e\u73b0\u4e86\u62bd\u8c61\u7b56\u7565\u5b9a\u4e49\u7684\u63a5\u53e3\uff0c\u63d0\u4f9b\u5177\u4f53\u7684\u7b97\u6cd5\u5b9e\u73b0\u6216\u884c\u4e3a\u3002<\/span><\/p>\n<p style=\"margin-bottom: 8px; margin-left: 8px; margin-right: 8px;\"><span style=\"font-size: 16px;\"><span style=\"color: #ff0000;\">\u73af\u5883\u6216\u4e0a\u4e0b\u6587\uff08Context\uff09\u7c7b<\/span>\uff1a\u662f\u4f7f\u7528\u7b97\u6cd5\u7684\u89d2\u8272, \u6301\u6709\u4e00\u4e2a\u7b56\u7565\u7c7b\u7684\u5f15\u7528\uff0c\u6700\u7ec8\u7ed9\u5ba2\u6237\u7aef\u8c03\u7528\u3002<\/span><\/p>\n<p style=\"margin-bottom: 8px; margin-left: 8px; margin-right: 8px;\"><span style=\"font-size: 16px;\">\u8be5\u65b9\u6cd5\u59d4\u6258\u7b56\u7565\u53d8\u91cf\u8c03\u7528\u5177\u4f53\u7b56\u7565\u6240\u5b9e\u73b0\u7684\u7b56\u7565\u63a5\u53e3\u4e2d\u7684\u65b9\u6cd5\uff08\u5b9e\u73b0\u63a5\u53e3\u7684\u7c7b\u91cd\u5199\u7b56\u7565\u4e2d\u7684\u65b9\u6cd5\uff0c\u6765\u5b8c\u6210\u5177\u4f53\u529f\u80fd\uff09<\/span><\/p>\n<p style=\"text-align: center; margin-bottom: 8px; margin-left: 8px; margin-right: 8px;\"><span style=\"font-size: 16px;\"><strong style=\"font-size: 16px; letter-spacing: 0.578px; text-align: center; text-wrap: wrap; color: #00d5ff;\"><span style=\"font-size: 18px;\">\u7b56\u7565\u6a21\u5f0f\u5b9e\u73b0<\/span><\/strong><\/span><\/p>\n<p style=\"margin-bottom: 8px; margin-left: 8px; margin-right: 8px;\"><span style=\"font-size: 16px;\">\u5148\u770b\u4e00\u4e2a\u7b80\u5355\u7684\u5b9e\u73b0\uff0c\u5feb\u901f\u4e86\u89e3\u4ec0\u4e48\u662f\u7b56\u7565\u6a21\u5f0f\u3002<\/span><\/p>\n<p style=\"margin-bottom: 8px; margin-left: 8px; margin-right: 8px;\"><span style=\"font-size: 16px;\">1\u3001\u5b9a\u4e49\u4e00\u4e2a\u7b56\u7565\u63a5\u53e3<\/span><\/p>\n<section class=\"code-snippet__fix code-snippet__js\">\n<p style=\"margin-bottom: 8px; margin-left: 8px; margin-right: 8px;\"><code><span class=\"code-snippet_outer\">public interface Strategy {<\/span><\/code><code><span class=\"code-snippet_outer\">    \/**<\/span><\/code><code><span class=\"code-snippet_outer\">     * \u6267\u884c\u4efb\u52a1<\/span><\/code><code><span class=\"code-snippet_outer\">    *\/<\/span><\/code><code><span class=\"code-snippet_outer\">    void executeTask();<\/span><\/code><code><span class=\"code-snippet_outer\">}<\/span><\/code><code><\/code><\/p>\n<\/section>\n<p style=\"margin-bottom: 8px; margin-left: 8px; margin-right: 8px;\"><span style=\"font-size: 16px;\">2\u3001\u5177\u4f53\u7684\u7b56\u7565\u5b9e\u73b0\uff0c\u8fd9\u91cc\u5b9e\u73b0\u4e24\u4e2a\u3002StrategyA\u548cStrategyB<\/span><\/p>\n<section class=\"code-snippet__fix code-snippet__js\">\n<p style=\"margin-bottom: 8px; margin-left: 8px; margin-right: 8px;\"><code><span class=\"code-snippet_outer\">public class StrategyA implements Strategy {<\/span><\/code><code><span class=\"code-snippet_outer\">    @Override<\/span><\/code><code><span class=\"code-snippet_outer\">    public void executeTask() {<\/span><\/code><code><span class=\"code-snippet_outer\">        System.out.println(\"\u6267\u884c\u4efb\u52a1A\");<\/span><\/code><code><span class=\"code-snippet_outer\">    }<\/span><\/code><code><span class=\"code-snippet_outer\">}<\/span><\/code><\/p>\n<\/section>\n<section class=\"code-snippet__fix code-snippet__js\">\n<p style=\"margin-bottom: 8px; margin-left: 8px; margin-right: 8px;\"><code><span class=\"code-snippet_outer\">public class StrategyB implements Strategy {<\/span><\/code><code><span class=\"code-snippet_outer\">    @Override<\/span><\/code><code><span class=\"code-snippet_outer\">    public void executeTask() {<\/span><\/code><code><span class=\"code-snippet_outer\">        System.out.println(\"\u6267\u884c\u4efb\u52a1B\");<\/span><\/code><code><span class=\"code-snippet_outer\">    }<\/span><\/code><code><span class=\"code-snippet_outer\">}<\/span><\/code><\/p>\n<\/section>\n<p style=\"margin-bottom: 8px; margin-left: 8px; margin-right: 8px;\"><span style=\"font-size: 16px;\">3\u3001\u7b56\u7565\u4e0a\u4e0b\u6587<\/span><\/p>\n<section class=\"code-snippet__fix code-snippet__js\">\n<p style=\"margin-bottom: 8px; margin-left: 8px; margin-right: 8px;\"><code><span class=\"code-snippet_outer\">public class Context {<\/span><\/code><code><span class=\"code-snippet_outer\">    private Strategy strategy;<\/span><\/code><code><\/code><code><span class=\"code-snippet_outer\">    public Context(Strategy strategy) {<\/span><\/code><code><span class=\"code-snippet_outer\">        this.strategy = strategy;<\/span><\/code><code><span class=\"code-snippet_outer\">    }<\/span><\/code><code><span class=\"code-snippet_outer\">    public void executeTask(){<\/span><\/code><code><span class=\"code-snippet_outer\">        strategy.executeTask();<\/span><\/code><code><span class=\"code-snippet_outer\">    }<\/span><\/code><code><span class=\"code-snippet_outer\">}<\/span><\/code><\/p>\n<\/section>\n<p style=\"margin-bottom: 8px; margin-left: 8px; margin-right: 8px;\"><span style=\"font-size: 16px;\">4\u3001\u7b56\u7565\u8c03\u7528<\/span><\/p>\n<section class=\"code-snippet__fix code-snippet__js\">\n<p style=\"margin-bottom: 8px; margin-left: 8px; margin-right: 8px;\"><code><span class=\"code-snippet_outer\">    @Test<\/span><\/code><code><span class=\"code-snippet_outer\">    public void test001(){<\/span><\/code><code><span class=\"code-snippet_outer\">        Context context = new Context(new StrategyA());<\/span><\/code><code><span class=\"code-snippet_outer\">        context.executeTask();<\/span><\/code><code><span class=\"code-snippet_outer\">    }<\/span><\/code><\/p>\n<\/section>\n<p style=\"margin-bottom: 8px; margin-left: 8px; margin-right: 8px;\"><span style=\"font-size: 16px;\">\u76f8\u4fe1\u5230\u8fd9\u91cc\u5bf9\u7b56\u7565\u6a21\u5f0f\u90fd\u6709\u4e86\u4e00\u5b9a\u7684\u4e86\u89e3\uff0c\u63a5\u4e0b\u6765\u5177\u4f53\u770b\u4e0b\u5982\u679c\u4f7f\u7528\u7b56\u7565\u6a21\u5f0f\u6d88\u9664if else<\/span><\/p>\n<p style=\"text-align: center; margin-bottom: 8px; margin-left: 8px; margin-right: 8px;\"><span style=\"font-size: 16px;\"><strong style=\"font-size: 16px; letter-spacing: 0.578px; text-align: center; text-wrap: wrap; color: #00d5ff;\"><span style=\"font-size: 18px;\">\u7b56\u7565\u6a21\u5f0f\u6d88\u9664if else<\/span><\/strong><\/span><\/p>\n<p style=\"margin-bottom: 8px; margin-left: 8px; margin-right: 8px;\"><span style=\"font-size: 16px;\">1\u3001\u5b9a\u4e49\u4e00\u4e2a\u5b9e\u4f53\u7c7b<\/span><\/p>\n<section class=\"code-snippet__fix code-snippet__js\">\n<pre class=\"code-snippet__js\" data-lang=\"typescript\"><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__meta\">@Datapublic<\/span> <span class=\"code-snippet__keyword\">class<\/span> StrategyModelTest {      <span class=\"code-snippet__keyword\">private<\/span> <span class=\"code-snippet__built_in\">String<\/span> name;      <span class=\"code-snippet__keyword\">private<\/span> <span class=\"code-snippet__built_in\">String<\/span> <span class=\"code-snippet__keyword\">type<\/span>;}<\/span><\/code><\/pre>\n<\/section>\n<p style=\"margin-bottom: 8px; margin-left: 8px; margin-right: 8px;\"><span style=\"font-size: 16px;\">2\u3001\u6a21\u62df\u6570\u636e\uff0c\u7528\u4e8e\u4e4b\u540e\u7b56\u7565\u4f7f\u7528\u3002<\/span><\/p>\n<section class=\"code-snippet__fix code-snippet__js\">\n<pre class=\"code-snippet__js\" data-lang=\"cs\"><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__keyword\">public<\/span> <span class=\"code-snippet__keyword\">class<\/span> <span class=\"code-snippet__title\">StrategyList<\/span> {    <span class=\"code-snippet__comment\">\/**     * \u7528\u6765\u6a21\u62df\u6570\u636e\uff0c\u6bd4\u5982\u662f\u6570\u636e\u5e93\u4e2d\u67e5\u8be2\u51fa\u6765\u7684\u6570\u636e     * @return java.util.List&lt;StrategyModelTest&gt;    *\/<\/span>    <span class=\"code-snippet__function\"><span class=\"code-snippet__keyword\">public<\/span> <span class=\"code-snippet__keyword\">static<\/span> List&lt;StrategyModelTest&gt; <span class=\"code-snippet__title\">getList<\/span>()<\/span>{        List&lt;StrategyModelTest&gt; strategyModelTestList = <span class=\"code-snippet__keyword\">new<\/span> ArrayList&lt;&gt;();        StrategyModelTest strategyModelTestA = <span class=\"code-snippet__keyword\">new<\/span> StrategyModelTest();        strategyModelTestA.setName(<span class=\"code-snippet__string\">\"name\"<\/span>);        strategyModelTestA.setType(<span class=\"code-snippet__string\">\"001\"<\/span>);        strategyModelTestList.<span class=\"code-snippet__keyword\">add<\/span>(strategyModelTestA);        StrategyModelTest strategyModelTestB = <span class=\"code-snippet__keyword\">new<\/span> StrategyModelTest();        strategyModelTestB.setName(<span class=\"code-snippet__string\">\"name\"<\/span>);        strategyModelTestB.setType(<span class=\"code-snippet__string\">\"002\"<\/span>);        strategyModelTestList.<span class=\"code-snippet__keyword\">add<\/span>(strategyModelTestB);        StrategyModelTest strategyModelTestC = <span class=\"code-snippet__keyword\">new<\/span> StrategyModelTest();        strategyModelTestC.setName(<span class=\"code-snippet__string\">\"name\"<\/span>);        strategyModelTestC.setType(<span class=\"code-snippet__string\">\"003\"<\/span>);        strategyModelTestList.<span class=\"code-snippet__keyword\">add<\/span>(strategyModelTestC);<span class=\"code-snippet__keyword\">return<\/span>strategyModelTestList;    }}<\/span><\/code><\/pre>\n<\/section>\n<p style=\"margin-bottom: 8px; margin-left: 8px; margin-right: 8px;\"><span style=\"font-size: 16px;\">3\u3001\u7b56\u7565\u63a5\u53e3\u7c7b<\/span><\/p>\n<section class=\"code-snippet__fix code-snippet__js\">\n<pre class=\"code-snippet__js\" data-lang=\"java\"><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__keyword\">public<\/span> <span class=\"code-snippet__class\"><span class=\"code-snippet__keyword\">interface<\/span> <span class=\"code-snippet__title\">StrategyTest<\/span> <\/span>{    <span class=\"code-snippet__comment\">\/**     * \u6267\u884c\u4efb\u52a1     * <span class=\"code-snippet__doctag\">@author<\/span>: mkj     * <span class=\"code-snippet__doctag\">@param<\/span> strategyModelTest    *\/<\/span>      <span class=\"code-snippet__function\"><span class=\"code-snippet__keyword\">void<\/span> <span class=\"code-snippet__title\">executeTask<\/span><span class=\"code-snippet__params\">(StrategyModelTest strategyModelTest)<\/span><\/span>;}<\/span><\/code><\/pre>\n<\/section>\n<p style=\"margin-bottom: 8px; margin-left: 8px; margin-right: 8px;\"><span style=\"font-size: 16px;\">4\u3001\u7b56\u7565\u5b9e\u73b0\u7c7b\uff0c\u8fd9\u6b21\u5b9e\u73b03\u4e2a\u3002<\/span><\/p>\n<section class=\"code-snippet__fix code-snippet__js\">\n<p style=\"margin-bottom: 8px; margin-left: 8px; margin-right: 8px;\"><code><span class=\"code-snippet_outer\">public class StrategyTestA implements StrategyTest {<\/span><\/code><code><span class=\"code-snippet_outer\">    @Override<\/span><\/code><code><span class=\"code-snippet_outer\">    public void executeTask(StrategyModelTest strategyModelTest) {<\/span><\/code><code><span class=\"code-snippet_outer\">        System.out.println(\"\u6267\u884c\u4efb\u52a1B\"+strategyModelTest.getName());<\/span><\/code><code><span class=\"code-snippet_outer\">    }<\/span><\/code><code><span class=\"code-snippet_outer\">}<\/span><\/code><code><\/code><code><span class=\"code-snippet_outer\">public class StrategyTestB implements StrategyTest {<\/span><\/code><code><span class=\"code-snippet_outer\">    @Override<\/span><\/code><code><span class=\"code-snippet_outer\">    public void executeTask(StrategyModelTest strategyModelTest) {<\/span><\/code><code><span class=\"code-snippet_outer\">        System.out.println(\"\u6267\u884c\u4efb\u52a1A\"+strategyModelTest.getName());<\/span><\/code><code><span class=\"code-snippet_outer\">    }<\/span><\/code><code><span class=\"code-snippet_outer\">}<\/span><\/code><code><\/code><code><span class=\"code-snippet_outer\">public class StrategyTestC implements StrategyTest {<\/span><\/code><code><span class=\"code-snippet_outer\">    @Override<\/span><\/code><code><span class=\"code-snippet_outer\">    public void executeTask(StrategyModelTest strategyModelTest) {<\/span><\/code><code><span class=\"code-snippet_outer\">        System.out.println(\"\u6267\u884c\u4efb\u52a1C\"+strategyModelTest.getName());<\/span><\/code><code><span class=\"code-snippet_outer\">    }<\/span><\/code><code><span class=\"code-snippet_outer\">}<\/span><\/code><\/p>\n<\/section>\n<p style=\"margin-bottom: 8px; margin-left: 8px; margin-right: 8px;\"><span style=\"font-size: 16px;\">5\u3001\u7b56\u7565\u4e0a\u4e0b\u6587<\/span><\/p>\n<section class=\"code-snippet__fix code-snippet__js\">\n<pre class=\"code-snippet__js\" data-lang=\"cs\"><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__keyword\">public<\/span> <span class=\"code-snippet__keyword\">class<\/span> <span class=\"code-snippet__title\">ContextTest<\/span> {    <span class=\"code-snippet__keyword\">private<\/span> StrategyTest strategyTest;    <span class=\"code-snippet__function\"><span class=\"code-snippet__keyword\">public<\/span> <span class=\"code-snippet__title\">ContextTest<\/span>(<span class=\"code-snippet__params\">StrategyTest strategyTest<\/span>)<\/span> {          <span class=\"code-snippet__keyword\">this<\/span>.strategyTest = strategyTest;    }    <span class=\"code-snippet__function\"><span class=\"code-snippet__keyword\">public<\/span> <span class=\"code-snippet__keyword\">void<\/span> <span class=\"code-snippet__title\">executeTask<\/span>(<span class=\"code-snippet__params\">StrategyModelTest strategyModelTes<\/span>)<\/span>{          strategyTest.executeTask(strategyModelTes);    }}<\/span><\/code><\/pre>\n<\/section>\n<p style=\"margin-bottom: 8px; margin-left: 8px; margin-right: 8px;\"><span style=\"font-size: 16px;\">6\u3001\u5148\u770b\u4e00\u4e2a\u53cd\u4f8b\uff0c\u4f7f\u7528if else \u5b9e\u73b0\u3002<\/span><\/p>\n<p style=\"margin-bottom: 8px; margin-left: 8px; margin-right: 8px;\"><span style=\"font-size: 16px;\">\u53ef\u4ee5\u770b\u89c1\u4ee3\u7801\u91cc\u6709\u597d\u51e0\u4e2aif else ,\u8fd9\u8fd8\u53ea\u662f\u793a\u4f8b\uff0c\u5982\u679c\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u8fd9\u79cd\u903b\u8f91\u53ea\u4f1a\u66f4\u591a\u3002<\/span><\/p>\n<section class=\"code-snippet__fix code-snippet__js\">\n<pre class=\"code-snippet__js\" data-lang=\"cs\"><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__comment\">\/**     * \u4e0d\u4f7f\u7528\u7b56\u7565     * @author: mkj    *\/<\/span>    @<span class=\"code-snippet__function\">Test    <span class=\"code-snippet__keyword\">public<\/span> <span class=\"code-snippet__keyword\">void<\/span> <span class=\"code-snippet__title\">test<\/span>()<\/span>{        <span class=\"code-snippet__comment\">\/\/\u8fd9\u662f\u53ea\u662f\u6a21\u62df\u6570\u636e\uff0ctype\u4e5f\u53ef\u4ee5\u662f\u5f53\u53c2\u6570\u4f20\u9012\u3002List&lt;StrategyModelTest&gt; list = StrategyList.getList();          for (StrategyModelTest strategyModelTest : list){              if (\"001\".equals(strategyModelTest.getType())){                System.out.println(\"\u6267\u884c001\u7684\u4e1a\u52a1\u903b\u8f91\");              }else if (\"002\".equals(strategyModelTest.getType())){                System.out.println(\"\u6267\u884c002\u7684\u4e1a\u52a1\u903b\u8f91\");              }else {                System.out.println(\"\u6267\u884c003\u7684\u4e1a\u52a1\u903b\u8f91\");              }          }      }<\/span><\/span><\/code><\/pre>\n<\/section>\n<p style=\"margin-bottom: 8px; margin-left: 8px; margin-right: 8px;\"><span style=\"font-size: 16px;\">7\u3001\u4e0b\u9762\u770b\u6b63\u4f8b\u3002<\/span><\/p>\n<p style=\"margin-bottom: 8px; margin-left: 8px; margin-right: 8px;\"><span style=\"font-size: 16px;\">\u60f3\u8981\u6d88\u9664if else \uff0c\u8fd8\u9700\u8981\u5b9e\u73b0\u4e00\u4e2a\u7c7b\uff0c\u7528Map\u5c01\u88c5\u7b56\u7565\u7c7b\u3002\u5982\u4e0b\uff1a<\/span><\/p>\n<section class=\"code-snippet__fix code-snippet__js\">\n<pre class=\"code-snippet__js\" data-lang=\"cs\"><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__keyword\">public<\/span> <span class=\"code-snippet__keyword\">class<\/span> <span class=\"code-snippet__title\">StrategyTestFactory<\/span> {    <span class=\"code-snippet__function\"><span class=\"code-snippet__keyword\">public<\/span> <span class=\"code-snippet__title\">StrategyTestFactory<\/span>()<\/span> {    }    <span class=\"code-snippet__comment\">\/** \u4f7f\u7528map\u6765\u5b58\u653e\u7b56\u7565\u7c7b\uff0c\u7528\u6765\u6d88\u9664if else*\/<\/span>    <span class=\"code-snippet__keyword\">private<\/span> <span class=\"code-snippet__keyword\">static<\/span> Map&lt;String, StrategyTest&gt; strategyMap;    <span class=\"code-snippet__function\"><span class=\"code-snippet__keyword\">public<\/span> <span class=\"code-snippet__keyword\">static<\/span> <span class=\"code-snippet__keyword\">void<\/span> <span class=\"code-snippet__title\">init<\/span>()<\/span>{        strategyMap = <span class=\"code-snippet__keyword\">new<\/span> HashMap&lt;&gt;(<span class=\"code-snippet__number\">8<\/span>);        strategyMap.put(<span class=\"code-snippet__string\">\"001\"<\/span>,<span class=\"code-snippet__keyword\">new<\/span> StrategyTestA());        strategyMap.put(<span class=\"code-snippet__string\">\"002\"<\/span>,<span class=\"code-snippet__keyword\">new<\/span> StrategyTestB());        strategyMap.put(<span class=\"code-snippet__string\">\"003\"<\/span>,<span class=\"code-snippet__keyword\">new<\/span> StrategyTestC());    }    <span class=\"code-snippet__function\"><span class=\"code-snippet__keyword\">public<\/span> <span class=\"code-snippet__keyword\">static<\/span> StrategyTest <span class=\"code-snippet__title\">getStrategy<\/span>(<span class=\"code-snippet__params\">String type<\/span>)<\/span>{        <span class=\"code-snippet__keyword\">return<\/span> strategyMap.<span class=\"code-snippet__keyword\">get<\/span>(type);    }}<\/span><\/code><\/pre>\n<\/section>\n<p style=\"margin-bottom: 8px; margin-left: 8px; margin-right: 8px;\"><span style=\"font-size: 16px;\">\u7b56\u7565\u5b9e\u73b0\uff1a<\/span><\/p>\n<section class=\"code-snippet__fix code-snippet__js\">\n<p style=\"margin-bottom: 8px; margin-left: 8px; margin-right: 8px;\"><code><span class=\"code-snippet_outer\">  \/**<\/span><\/code><code><span class=\"code-snippet_outer\">     * \u4f7f\u7528\u7b56\u7565<\/span><\/code><code><span class=\"code-snippet_outer\">     * @author: mkj<\/span><\/code><code><span class=\"code-snippet_outer\">     *\/<\/span><\/code><code><span class=\"code-snippet_outer\">    @Test<\/span><\/code><code><span class=\"code-snippet_outer\">    public void strategyTest(){<\/span><\/code><code><span class=\"code-snippet_outer\">        List&lt;StrategyModelTest&gt; list = StrategyList.getList();<\/span><\/code><code><span class=\"code-snippet_outer\">        StrategyTestFactory.init();<\/span><\/code><code><span class=\"code-snippet_outer\">        for (StrategyModelTest strategyModelTest : list){<\/span><\/code><code><span class=\"code-snippet_outer\">            StrategyTestFactory.getStrategy(strategyModelTest.getType()).executeTask(strategyModelTest);<\/span><\/code><code><span class=\"code-snippet_outer\">        }<\/span><\/code><code><span class=\"code-snippet_outer\">}<\/span><\/code><\/p>\n<\/section>\n<p style=\"margin-bottom: 8px; margin-left: 8px; margin-right: 8px;\"><span style=\"font-size: 16px;\"> \u4ece\u793a\u4f8b\u4e2d\u53ef\u4ee5\u770b\u51fa\uff0c\u4ee3\u7801\u4e2d\u4e00\u4e2aif else \u90fd\u6ca1\u6709\uff0c\u800c\u4e14\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\uff0c\u4e5f\u66f4\u52a0\u5229\u4e8e\u6269\u5c55\uff0c\u5982\u679c\u518d\u6709\u4e00\u4e2a\u4e0d\u540c\u903b\u8f91\uff0c\u5b9e\u73b0\u4e00\u4e2a\u7b56\u7565\u7c7b\u5c31\u53ef\u4ee5\u4e86\uff0c\u5176\u4ed6\u4ee3\u7801\u57fa\u672c\u4e0d\u7528\u52a8\u3002<\/span><\/p>\n<p style=\"text-align: center; margin-bottom: 8px; margin-left: 8px; margin-right: 8px;\"><span style=\"font-size: 16px;\"><strong style=\"color: #00d5ff; letter-spacing: 0.578px; text-align: center; text-wrap: wrap;\"><span style=\"font-size: 18px;\">\u7b56\u7565\u6a21\u5f0f\u5e94\u7528\u573a\u666f<\/span><\/strong><\/span><\/p>\n<p style=\"margin-bottom: 8px; margin-left: 8px; margin-right: 8px;\"><span style=\"font-size: 16px;\">1\u3001\u6709\u591a\u4e2a\u76f8\u4f3c\u7684\u7c7b,\u53ea\u6709\u884c\u4e3a\u6709\u5dee\u5f02\u7684\u60c5\u51b5\u4e0b,\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u7b56\u7565\u6a21\u5f0f\u3002<\/span><\/p>\n<p style=\"margin-bottom: 8px; margin-left: 8px; margin-right: 8px;\"><span style=\"font-size: 16px;\">2\u3001\u9700\u8981\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u5207\u6362\u7b97\u6cd5\u7684\u60c5\u51b5\u4e0b,\u53ef\u4ee5\u4f7f\u7528\u7b56\u7565\u6a21\u5f0f\u3002<\/span><\/p>\n<p style=\"margin-bottom: 8px; margin-left: 8px; margin-right: 8px;\"><span style=\"font-size: 16px;\">3\u3001\u4e00\u4e2a\u7cfb\u7edf\u9700\u8981\u5728\u4e0d\u540c\u65f6\u95f4\u5e94\u7528\u4e0d\u540c\u7684\u4e1a\u52a1\u89c4\u5219\u60c5\u51b5\u4e0b,\u53ef\u4ee5\u4f7f\u7528\u7b56\u7565\u6a21\u5f0f\u3002<\/span><\/p>\n<p style=\"margin-bottom: 8px; margin-left: 8px; margin-right: 8px;\"><span style=\"font-size: 16px;\">4\u3001\u5728\u4e00\u4e2a\u7cfb\u7edf\u4e2d,\u6709\u8bb8\u591a\u7c7b\u9700\u8981\u6839\u636e\u67d0\u79cd\u89c4\u5219\u8fdb\u884c\u6392\u5e8f,\u67e5\u627e\u7b49\u64cd\u4f5c\u7684\u60c5\u51b5\u4e0b,\u53ef\u4ee5\u4f7f\u7528\u7b56\u7565\u6a21\u5f0f\u3002<\/span><\/p>\n<p style=\"outline: 0px; font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif; letter-spacing: 0.544px; text-wrap: wrap; background-color: #ffffff; text-align: center; margin-bottom: 8px; margin-left: 8px; margin-right: 8px;\"><span style=\"outline: 0px; color: #ffffff; background-color: #00fcff;\"><strong style=\"outline: 0px;\"><span style=\"outline: 0px; font-family: \u7b49\u7ebf; letter-spacing: 0.544px;\">\u4f5c\u8005\u4ecb\u7ecd<\/span><\/strong><\/span><\/p>\n<p style=\"outline: 0px; font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif; letter-spacing: 0.544px; text-wrap: wrap; background-color: #ffffff; text-indent: 2em; margin-bottom: 8px; margin-left: 8px; margin-right: 8px;\"><span style=\"outline: 0px; font-size: 16px;\">90\u5e74\uff0c\u5c5e\u9a6c\uff0c\u4e0a\u73ed\u65cf\uff0c\u6000\u63e3\u68a6\u60f3\uff0c\u575a\u6301\u7528\u5fc3\u5206\u4eab\uff0c\u4e00\u76f4\u60f3\u901a\u8fc7\u81ea\u5df1\u7684\u52aa\u529b\u505a\u51fa\u4e00\u4e9b\u6210\u7ee9\u3002\u4f69\u670d\u901a\u8fc7\u81ea\u5df1\u7684\u575a\u6301\u5b9e\u73b0\u68a6\u60f3\u7684\u4eba\u3002\u76f8\u4fe1\u6ef4\u6c34\u7a7f\u77f3\uff0c\u76f8\u4fe1\u70b9\u6ef4\u7684\u79ef\u7d2f\u7ec8\u6709\u4e00\u5929\u53ef\u4ee5\u6210\u4e3a\u81ea\u5df1\u70ab\u8000\u7684\u8d44\u672c\uff01<\/span><\/p>\n<section style=\"margin-bottom: 0px; outline: 0px; font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif; letter-spacing: 0.544px; text-wrap: wrap; background-color: #ffffff; text-indent: 2em;\"><\/section>\n<section class=\"mp_profile_iframe_wrp\"><\/section>\n<p style=\"display: none;\">\n<blockquote><p>\u672c\u7bc7\u6587\u7ae0\u6765\u6e90\u4e8e\u5fae\u4fe1\u516c\u4f17\u53f7: Java\u6280\u672f\u4ed3\u5e93<\/p><\/blockquote>\n<\/div>\n","raw":"","protected":false},"excerpt":{"rendered":"<p>\u7b56\u7565\u6a21\u5f0f\u4ecb\u7ecd \u7b56\u7565\u6a21\u5f0f\u5c5e\u4e8eJava23\u79cd\u8bbe\u8ba1\u6a21\u5f0f\u4e2d\u7684\u884c\u4e3a\u578b\u6a21\u5f0f\u3002\u5728\u5b9e\u9645\u9879\u76ee\u5f00\u53d1\u4f7f\u7528\u8f83\u591a\u3002\u4e5f\u662f\u9762\u8bd5\u7ecf\u5e38\u4f1a\u95ee\u5230\u7684\u77e5\u8bc6\u70b9\u3002 \u7b56\u7565\u6a21\u5f0f(s &#8230;<\/p>\n","protected":false},"author":1,"featured_media":3783,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[24],"tags":[],"class_list":["post-3786","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-24"],"post_thumbnail_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524142529992-150x150.jpeg","content_first_image":null,"post_medium_image_300":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524142529992-300x128.jpeg","post_thumbnail_image_624":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524142529992-1024x436.jpeg","post_frist_image":null,"post_medium_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524142529992-300x128.jpeg","post_large_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524142529992-1024x436.jpeg","post_full_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524142529992.jpeg","post_all_images":[],"videoAdId":"","listAd":"0","listAdId":"","listAdEvery":6,"total_comments":0,"category_name":"\u8bbe\u8ba1\u6a21\u5f0f","post_date":"2024-05-21","like_count":"0","praiseWord":"\u9f13\u52b1","copyright_state":"","excitationAd":"0","rewardedVideoAdId":"","detailAdId":"","detailAd":"0","enterpriseMinapp":"0","audios":[],"postImageUrl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2023\/05\/20230519082947553.jpg","avatarurls":[],"related_posts":null,"pageviews":354,"next_post_id":null,"next_post_title":null,"previous_post_id":3794,"previous_post_title":"23\u79cd\u7ecf\u5178\u8bbe\u8ba1\u6a21\u5f0f-\u5de5\u5382\u6a21\u5f0f\uff08\u8be6\u89e3\uff09","_links":{"self":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/3786","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/comments?post=3786"}],"version-history":[{"count":1,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/3786\/revisions"}],"predecessor-version":[{"id":4178,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/3786\/revisions\/4178"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/media\/3783"}],"wp:attachment":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/media?parent=3786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/categories?post=3786"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/tags?post=3786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}