{"id":10006901,"date":"2022-11-23T13:08:40","date_gmt":"2022-11-23T05:08:40","guid":{"rendered":"https:\/\/ityet.com\/?p=10006901"},"modified":"2022-11-23T13:08:40","modified_gmt":"2022-11-23T05:08:40","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-%e7%ad%96%e7%95%a5%e6%a8%a1%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006901","title":{"rendered":"\u5de5\u4f5c\u4e2d\u5e38\u7528\u7684\u8bbe\u8ba1\u6a21\u5f0f&#8211;\u7b56\u7565\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 \u7b56\u7565\u6a21\u5f0f(Strategy Pattern)<\/h3>\n<p>\u4e00\u4e2a\u7c7b\u7684\u884c\u4e3a\u6216\u65b9\u6cd5\uff0c\u5728\u8fd0\u884c\u65f6\u53ef\u4ee5\u6839\u636e\u6761\u4ef6\u7684\u4e0d\u540c\uff0c\u6709\u4e0d\u540c\u7684\u7b56\u7565(\u884c\u4e3a\u3001\u65b9\u6cd5)\u53bb\u6267\u884c\u3002\u4e3e\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a\u53bb\u4e0a\u73ed\uff0c\u53ef\u4ee5\u9a91\u5171\u4eab\u5355\u8f66\u3001\u53ef\u4ee5\u9009\u62e9\u516c\u4ea4\u8f66\u3001\u4e5f\u53ef\u4ee5\u4e58\u5750\u5730\u94c1\u3002\u8fd9\u91cc\u7684<code>\u4e58\u5750\u4ec0\u4e48\u4ea4\u901a\u5de5\u5177<\/code>\u5c31\u662f\u9488\u5bf9\u53bb\u4e0a\u73ed\u8fd9\u4e2a\u884c\u4e3a\u7684<code>\u7b56\u7565(\u89e3\u51b3\u65b9\u6848)<\/code>\u3002<\/p>\n<p>\u7b56\u7565\u6a21\u5f0f\u4e00\u822c\u67093\u4e2a\u89d2\u8272:<\/p>\n<ul>\n<li>Context: \u7b56\u7565\u7684\u4e0a\u4e0b\u6587\u6267\u884c\u73af\u5883<\/li>\n<li>Strategy: \u7b56\u7565\u7684\u62bd\u8c61<\/li>\n<li>ConcreteStrategy: \u7b56\u7565\u7684\u5177\u4f53\u5b9e\u73b0<\/li>\n<\/ul>\n<p>\u8fd9\u4e2a\u51fa\u73b0\u7684\u573a\u666f\u5176\u5b9e\u8fd8\u5f88\u591a\u3002\u5982\u4e4b\u524d\u505a\u5546\u57ce\u65f6\u9047\u5230\u7684\u767b\u5f55(\u624b\u673a\u53f7\u3001\u5fae\u4fe1\u3001QQ\u7b49)\uff0c\u53ca\u4f18\u60e0\u5238(\u6ee1\u51cf\u5238\u3001\u4ee3\u91d1\u5238\u3001\u6298\u6263\u5238\u7b49)\u3002\u8fd9\u91cc\u4e3b\u8981\u8bb2\u4e00\u4e0b\u6700\u8fd1\u9047\u5230\u7684\u4e24\u79cd\u3002\u4e00\u79cd\u662f\u9884\u5148\u77e5\u9053\u8981\u8d70\u54ea\u4e2a\u7b56\u7565\uff0c\u4e00\u79cd\u662f\u9700\u8981\u52a8\u6001\u8ba1\u7b97\u624d\u80fd\u786e\u5b9a\u8d70\u54ea\u79cd\u7b56\u7565\u3002<\/p>\n<h4>1.1 \u9759\u6001(\u53c2\u6570)\u7b56\u7565<\/h4>\n<p>\u5728\u505a\u589e\u957f\u7cfb\u7edf\u65f6\uff0c\u7528\u6237\u7559\u8d44\u8fdb\u7ebf\u9700\u8981\u6839\u636e\u4e0d\u540c\u6765\u6e90\u8d70\u4e0d\u540c\u7684\u5904\u7406\u903b\u8f91\u3002\u800c\u8fd9\u79cd\u6765\u6e90\uff0c\u5728\u6570\u636e\u51fa\u73b0\u65f6\u5c31\u80fd\u786e\u5b9a\u3002<\/p>\n<blockquote><p>SyncContext<\/p><\/blockquote>\n<pre><code class=\"java\">\/**\n * \u540c\u6b65\u4e0a\u4e0b\u6587\n *\n *\/\n@Data\n@Builder\npublic class SyncContext {\n    \/\/ \u4efb\u52a1ID\n    private Long taskId;\n    \/\/ \u4efb\u52a1\u7c7b\u578b 1: \u81ea\u7136\u6ce8\u518c; 2: \u56e2\u8d2d\u7528\u6237; 3: \u843d\u5730\u9875\u7559\u8d44\n    private Integer taskType;\n    \/\/ \u6240\u6709\u7559\u8d44\u76f8\u5173\u4fe1\u606f(\u5ffd\u7565\u7ec6\u8282)\n    private Object reqVO;\n\n    \/\/ \u5b58\u50a8\u6267\u884c\u7b56\u7565\u540d\u79f0(\u4f2a\u88c5\u6267\u884c\u7ed3\u679c)\n    private String respVO;\n}<\/code><\/pre>\n<blockquote><p>SyncStrategy<\/p><\/blockquote>\n<pre><code class=\"java\">\/**\n * \u540c\u6b65\u7b56\u7565\n *\n *\/\npublic interface SyncStrategy {\n\n    \/**\n     * \u5177\u4f53\u7b56\u7565\n     * @param ctx Context\n     *\/\n    void process(SyncContext ctx);\n}<\/code><\/pre>\n<blockquote><p>OtSyncStrategy<\/p><\/blockquote>\n<pre><code class=\"java\">\/**\n * \u81ea\u7136\u6ce8\u518c\n *\n *\/\n@Slf4j\n@Service\npublic class OtSyncStrategy implements SyncStrategy, BeanNameAware {\n    private String beanName;\n\n    @Override\n    public void process(SyncContext ctx) {\n        log.info(&quot;[\u81ea\u7136\u6ce8\u518c] {}&quot;, ctx);\n        ctx.setRespVO(beanName);\n    }\n\n    @Override\n    public void setBeanName(String s) {\n        beanName = s;\n    }\n}<\/code><\/pre>\n<blockquote><p>AbSyncStrategy<\/p><\/blockquote>\n<pre><code class=\"java\">\/**\n * \u56e2\u8d2d\u7528\u6237\n *\n *\/\n@Slf4j\n@Service\npublic class AbSyncStrategy implements SyncStrategy, BeanNameAware {\n    private String beanName;\n\n    @Override\n    public void process(SyncContext ctx) {\n        log.info(&quot;[\u56e2\u8d2d\u7528\u6237] {}&quot;, ctx);\n        ctx.setRespVO(beanName);\n    }\n\n    @Override\n    public void setBeanName(String s) {\n        beanName = s;\n    }\n}<\/code><\/pre>\n<blockquote><p>DefaultSyncStrategy<\/p><\/blockquote>\n<pre><code class=\"java\">\/**\n * \u843d\u5730\u9875\u6ce8\u518c(Default)\n *\n *\/\n@Slf4j\n@Service\npublic class DefaultSyncStrategy implements SyncStrategy, BeanNameAware {\n    private String beanName;\n\n    @Override\n    public void process(SyncContext ctx) {\n        log.info(&quot;[\u843d\u5730\u9875\u6ce8\u518c] {}&quot;, ctx);\n        ctx.setRespVO(beanName);\n    }\n\n    @Override\n    public void setBeanName(String s) {\n        beanName = s;\n    }\n}<\/code><\/pre>\n<p>\u81f3\u6b64\uff0c\u7b56\u7565\u6a21\u5f0f\u7684\u4e09\u4e2a\u89d2\u8272\u5df2\u51d1\u9f50\u3002\u4f46\u4f3c\u4e4e\u8fd8\u6709\u4e00\u4e9b\u95ee\u9898\uff0c<code>SyncContext<\/code>\u4e2d\u6709<code>taskType<\/code>\uff0c\u4f46\u662f\u8be5\u600e\u4e48\u4e0e\u5177\u4f53\u7684\u7b56\u7565\u5339\u914d\u5462\uff1f\u6211\u4eec\u53ef\u4ee5\u501f\u52a9<code>Spring<\/code>\u6846\u67b6\u7684\u4f9d\u8d56\u6ce8\u5165\u7ba1\u7406\u7b56\u7565\u3002<\/p>\n<blockquote><p>SyncStrategy<\/p><\/blockquote>\n<pre><code class=\"java\">\/**\n * \u540c\u6b65\u7b56\u7565\n *\n *\/\npublic interface SyncStrategy {\n    String OT_STRATEGY = &quot;otStrategy&quot;;\n    String AB_STRATEGY = &quot;abStrategy&quot;;\n    String DEFAULT_STRATEGY = &quot;defaultStrategy&quot;;\n\n    \/**\n     * \u5177\u4f53\u7b56\u7565\n     * @param ctx Context\n     *\/\n    void process(SyncContext ctx);\n}<\/code><\/pre>\n<p>\u540c\u65f6\u4fee\u6539\u4e00\u4e0b\u5177\u4f53\u7b56\u7565\uff0c\u6307\u5b9a<code>@Service<\/code>\u522b\u540d\u3002\u5c063\u4e2a\u5177\u4f53\u7b56\u7565\u7c7b\u4fee\u6539\u5b8c\u5373\u53ef\u3002<\/p>\n<blockquote><p>OtSyncStrategy<\/p><\/blockquote>\n<pre><code class=\"java\">\/**\n * \u81ea\u7136\u6ce8\u518c\n *\n *\/\n@Slf4j\n@Service(SyncStrategy.OT_STRATEGY)\npublic class OtSyncStrategy implements SyncStrategy, BeanNameAware {\n    private String beanName;\n\n    @Override\n    public void process(SyncContext ctx) {\n        log.info(&quot;[\u81ea\u7136\u6ce8\u518c] {}&quot;, ctx);\n        ctx.setRespVO(beanName);\n    }\n\n    @Override\n    public void setBeanName(String s) {\n        beanName = s;\n    }\n}<\/code><\/pre>\n<p>\u6b64\u65f6\u6211\u4eec\u4f3c\u4e4e\u8fd8\u9700\u8981\u4e00\u4e2a\u6574\u5408\u8c03\u7528\u7684\u7c7b\uff0c\u5426\u5219\u7684\u8bdd\u5c31\u8981\u628a\u6240\u6709\u7b56\u7565\u66b4\u9732\u51fa\u53bb\u3002\u4e00\u4e2a\u7b80\u5355\u5de5\u5382\u5373\u53ef\u641e\u5b9a\u3002<\/p>\n<blockquote><p>SyncStrategyFactory<\/p><\/blockquote>\n<pre><code class=\"java\">\/**\n * \u540c\u6b65\u7b56\u7565\u5de5\u5382\u7c7b\u63a5\u53e3\n *\n *\/\npublic interface SyncStrategyFactory {\n    Map&lt;Integer, String&gt; STRATEGY_MAP = Map.of(\n            1, SyncStrategy.OT_STRATEGY,\n            2, SyncStrategy.AB_STRATEGY,\n            3, SyncStrategy.DEFAULT_STRATEGY\n    );\n\n    \/**\n     * \u6839\u636e\u4efb\u52a1\u7c7b\u578b\u83b7\u53d6\u5177\u4f53\u7b56\u7565\n     *\n     * @param taskType \u4efb\u52a1\u7c7b\u578b\n     * @return \u5177\u4f53\u7b56\u7565\n     *\/\n    SyncStrategy getStrategy(Integer taskType);\n\n    \/**\n     * \u6267\u884c\u7b56\u7565  \/\/ XXX: \u5176\u5b9e\u8fd9\u5757\u653e\u8fd9\u91cc\u6709\u80cc\u5355\u4e00\u804c\u8d23\u7684\uff0c\u540c\u65f6\u4e5f\u4e0d\u7b26\u5408Factory\u672c\u610f\u3002\n     *\n     * @param ctx \u7b56\u7565\u4e0a\u4e0b\u6587\n     *\/\n    void exec(SyncContext ctx);\n}<\/code><\/pre>\n<blockquote><p>SyncStrategyFactoryImpl<\/p><\/blockquote>\n<pre><code class=\"java\">\/**\n * \u7b56\u7565\u5de5\u5382\u5177\u4f53\u5b9e\u73b0\n *\n *\/\n@Slf4j\n@Service\n@RequiredArgsConstructor\npublic class SyncStrategyFactoryImpl implements SyncStrategyFactory {\n\n    \/\/ \u8fd9\u5757\u53ef\u4ee5\u6309Spring Bean\u522b\u540d\u6ce8\u5165\n    private final Map&lt;String, SyncStrategy&gt; strategyMap;\n\n    @Override\n    public SyncStrategy getStrategy(Integer taskType) {\n        if (!STRATEGY_MAP.containsKey(taskType) || !strategyMap.containsKey(STRATEGY_MAP.get(taskType))) {\n            return null;\n        }\n        return strategyMap.get(STRATEGY_MAP.get(taskType));\n    }\n\n    @Override\n    public void exec(SyncContext ctx) {\n        Optional.of(getStrategy(ctx.getTaskType())).ifPresent(strategy -&gt; {\n            log.info(&quot;[\u7b56\u7565\u6267\u884c] \u67e5\u627e\u7b56\u7565 {}, ctx=&gt;{}&quot;, strategy.getClass().getSimpleName(), ctx);\n            strategy.process(ctx);\n            log.info(&quot;[\u7b56\u7565\u6267\u884c] \u6267\u884c\u5b8c\u6210 ctx=&gt;{}&quot;, ctx);\n        });\n    }\n}<\/code><\/pre>\n<p>\u81f3\u6b64\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u7684\u5728Spring\u73af\u5883\u4e2d\uff0c\u901a\u8fc7\u6ce8\u5165<code>SyncStrategyFactory<\/code>\u6765\u8c03\u7528\u3002<\/p>\n<p>\u6700\u540e\u8865\u4e0a\u5355\u6d4b<\/p>\n<pre><code class=\"java\">\/**\n * \u7b56\u7565\u5355\u6d4b\n *\n *\/\n@Slf4j\n@SpringBootTest\nclass SyncStrategyFactoryTest {\n\n    @Autowired\n    SyncStrategyFactory strategyFactory;\n\n    @Test\n    void testOtStrategy() {\n        final SyncContext ctx = SyncContext.builder().taskType(1).build();\n        strategyFactory.exec(ctx);\n        Assertions.assertEquals(&quot;otStrategy&quot;, ctx.getRespVO());\n    }\n\n    @Test\n    void testAbStrategy() {\n        final SyncContext ctx = SyncContext.builder().taskType(2).build();\n        strategyFactory.exec(ctx);\n        Assertions.assertEquals(&quot;abStrategy&quot;, ctx.getRespVO());\n    }\n\n    @Test\n    void testDefaultStrategy() {\n        final SyncContext ctx = SyncContext.builder().taskType(3).build();\n        strategyFactory.exec(ctx);\n        Assertions.assertEquals(&quot;defaultStrategy&quot;, ctx.getRespVO());\n    }\n\n    @Test\n    void testOtherStrategy() {\n        final SyncContext ctx = SyncContext.builder().taskType(-1).build();\n        strategyFactory.exec(ctx);\n        Assertions.assertNull(ctx.getRespVO());\n    }\n}<\/code><\/pre>\n<h4>1.2 \u52a8\u6001(\u53c2\u6570)\u7b56\u7565<\/h4>\n<p>\u5176\u5b9e\u5728\u4e0a\u9762\u7684\u7b56\u7565\u6a21\u5f0f\u4e2d\uff0c\u4e5f\u53ef\u4ee5\u5c06<code>taskType<\/code>\u653e\u5230\u5177\u4f53\u7b56\u7565\u4e2d\uff0c\u4f5c\u4e3a\u4e00\u4e2a\u5143\u6570\u636e\u5904\u7406\u3002\u5728\u9009\u62e9\u5177\u4f53\u7b56\u7565\u65f6\uff0c\u904d\u5386\u6240\u6709\u7b56\u7565\u5b9e\u73b0\u7c7b\uff0c\u5f53<code>taskType<\/code>\u4e0e\u5f53\u524d\u53c2\u6570\u5339\u914d\u65f6\u5219\u7ec8\u6b62\u904d\u5386\uff0c\u7531\u5f53\u524d\u7b56\u7565\u7c7b\u5904\u7406\u3002<\/p>\n<p>\u5728\u4e0a\u8ff0<code>\u843d\u5730\u9875\u6ce8\u518c<\/code>\u4e2d\uff0c\u5411CRM\u540c\u6b65\u6570\u636e\u65f6\uff0c\u9700\u8981\u6821\u9a8c\u7684\u6570\u636e\u6bd4\u8f83\u591a\u3002\u56e0\u4e3a\u4e0d\u540c\u5730\u533a\u843d\u5730\u9875\u53c2\u6570\u5404\u4e0d\u76f8\u540c\uff0c\u540c\u65f6\u6709\u4e9b\u5386\u53f2\u843d\u5730\u9875\u3002<\/p>\n<p>\u8fd9\u79cd\u5176\u5b9e\u53ef\u4ee5\u5728<code>\u7b56\u7565\u7c7b<\/code>\u4e2d\u6dfb\u52a0\u6821\u9a8c\u65b9\u6cd5\uff0c\u5982<code>boolean match(StrategyContext ctx)<\/code>\u3002\u5177\u4f53\u89c1\u4ee3\u7801<\/p>\n<blockquote><p>LayoutContext<\/p><\/blockquote>\n<pre><code class=\"java\">\/**\n * \u5e03\u5c40\u4e0a\u4e0b\u6587\n *\n *\/\n@Data\n@Builder\npublic class LayoutContext {\n    \/\/ \u843d\u5730\u9875\u7248\u672c(Landing Page Version)\n    private String lpv;\n\n    \/\/ \u56fd\u5bb6\u5730\u533a\n    private String country;\n    \/\/ \u6e20\u9053\u53f7\n    private String channel;\n\n    \/\/ \u6700\u7ec8\u5904\u7406\u7ed3\u679c \u62ff\u5230\u5e03\u5c40ID\n    private String layoutId;\n}<\/code><\/pre>\n<blockquote><p>LayoutStrategy<\/p><\/blockquote>\n<pre><code class=\"java\">\/**\n * \u5e03\u5c40\u5904\u7406\u7b56\u7565\n *\n *\/\npublic interface LayoutStrategy {\n\n    \/**\n     * \u6821\u9a8c\u662f\u5426\u5339\u914d\u8be5\u7b56\u7565\n     *\n     * @param ctx \u7b56\u7565\u4e0a\u4e0b\u6587\n     * @return bool\n     *\/\n    boolean match(LayoutContext ctx);\n\n    \/**\n     * \u5177\u4f53\u7b56\u7565\u5904\u7406\n     *\n     * @param ctx \u7b56\u7565\u4e0a\u4e0b\u6587\n     *\/\n    void process(LayoutContext ctx);\n}<\/code><\/pre>\n<blockquote><p>\u5177\u4f53\u5e03\u5c40\u5904\u7406\u7b56\u7565<\/p><\/blockquote>\n<pre><code class=\"java\">\/**\n * \u5e7c\u513f\u5e03\u5c40\n *\n *\/\n@Slf4j\n@Order(10)\n@Service\npublic class LayoutChildStrategy implements LayoutStrategy {\n    \/\/ \u5e7c\u513f\u7279\u6b8a\u6e20\u9053\u53f7(\u4f18\u5148\u7ea7\u6700\u9ad8)\n    private static final String CHILD_CHANNEL = &quot;FE-XX-XX-XX&quot;;\n\n    @Override\n    public boolean match(LayoutContext ctx) {\n        return Objects.nonNull(ctx) &amp;&amp; CHILD_CHANNEL.equals(ctx.getChannel());\n    }\n\n    @Override\n    public void process(LayoutContext ctx) {\n        log.info(&quot;[\u5e7c\u513f\u5e03\u5c40] \u5f00\u59cb\u5904\u7406&quot;);\n        ctx.setLayoutId(&quot;111&quot;);\n    }\n}<\/code><\/pre>\n<pre><code class=\"java\">\/**\n * \u6839\u636eLPV\u8fdb\u884c\u5224\u65ad\u7684\u7b56\u7565\n *\/\n@Slf4j\n@Order(20)\n@Service\npublic class LayoutLpvStrategy implements LayoutStrategy {\n    \/\/ \u9700\u8981\u8d70LPV\u5904\u7406\u903b\u8f91\u7684\u6e20\u9053\u53f7\n    private static final Set&lt;String&gt; LPV_CHANNELS = Set.of(\n            &quot;LP-XX-XX-01&quot;, &quot;LP-XX-XX-02&quot;, &quot;XZ-XX-XX-01&quot;, &quot;XZ-XX-XX-02&quot;\n    );\n\n    @Override\n    public boolean match(LayoutContext ctx) {\n        return Objects.nonNull(ctx) &amp;&amp; Objects.nonNull(ctx.getChannel()) &amp;&amp; LPV_CHANNELS.contains(ctx.getChannel());\n    }\n\n    @Override\n    public void process(LayoutContext ctx) {\n        log.info(&quot;[LPV\u5e03\u5c40] \u5f00\u59cb\u5904\u7406&quot;);\n        ctx.setLayoutId(&quot;222&quot;);\n    }\n}<\/code><\/pre>\n<pre><code class=\"java\">\/**\n * \u9ed8\u8ba4\u5904\u7406\u7b56\u7565\n *\/\n@Slf4j\n@Order(999)\n@Service\npublic class LayoutDefaultStrategy implements LayoutStrategy {\n\n    @Override\n    public boolean match(LayoutContext ctx) {\n        \/\/ \u515c\u5e95\u7b56\u7565\n        return true;\n    }\n\n    @Override\n    public void process(LayoutContext ctx) {\n        log.info(&quot;[\u9ed8\u8ba4\u5e03\u5c40] \u5f00\u59cb\u5904\u7406&quot;);\n        ctx.setLayoutId(&quot;999&quot;);\n    }\n}<\/code><\/pre>\n<p>\u6700\u540e\uff0c\u5de5\u5382\u7c7b:<\/p>\n<pre><code class=\"java\">\/**\n * \u5e03\u5c40\u5904\u7406\u5de5\u5382\n *\n *\/\npublic interface LayoutProcessFactory {\n\n    \/**\n     * \u83b7\u53d6\u5177\u4f53\u7b56\u7565\n     *\n     * @param ctx \u4e0a\u4e0b\u6587\n     * @return Strategy\n     *\/\n    Optional&lt;LayoutStrategy&gt; getStrategy(LayoutContext ctx);\n\n    \/**\n     * \u7b56\u7565\u8c03\u7528\n     *\n     * @param ctx \u4e0a\u4e0b\u6587\n     *\/\n    void exec(LayoutContext ctx);\n}<\/code><\/pre>\n<pre><code class=\"java\">\/**\n * \u5e03\u5c40\u5904\u7406\u5de5\u5382\u5b9e\u73b0\n *\/\n@Slf4j\n@Service\n@RequiredArgsConstructor\npublic class LayoutProcessFactoryImpl implements LayoutProcessFactory {\n\n    \/\/ Spring\u4f1a\u6839\u636e@Order\u6ce8\u89e3\u987a\u5e8f\u6ce8\u5165\n    private final List&lt;LayoutStrategy&gt; strategyList;\n\n    @Override\n    public Optional&lt;LayoutStrategy&gt; getStrategy(LayoutContext ctx) {\n        return strategyList.stream()\n                .filter(s -&gt; s.match(ctx)).findFirst();\n    }\n\n    @Override\n    public void exec(LayoutContext ctx) {\n        log.info(&quot;[\u5e03\u5c40\u5904\u7406] \u5c1d\u8bd5\u5904\u7406 ctx=&gt;{}&quot;, ctx);\n        getStrategy(ctx).ifPresent(s -&gt; {\n            s.process(ctx);\n            log.info(&quot;[\u5e03\u5c40\u5904\u7406] \u5904\u7406\u5b8c\u6210 ctx=&gt;{}&quot;, ctx);\n        });\n    }\n}<\/code><\/pre>\n<p>\u6700\u540e\u7684\u6700\u540e\uff0c\u5355\u6d4b\uff1a<\/p>\n<pre><code class=\"java\">@SpringBootTest\nclass LayoutProcessFactoryTest {\n\n    @Autowired\n    private LayoutProcessFactory processFactory;\n\n    @Test\n    void testChild() throws IllegalAccessException {\n        \/\/ \u901a\u8fc7\u53cd\u5c04\u83b7\u53d6Channel\n        final Field childChannel = ReflectionUtils.findField(LayoutChildStrategy.class, &quot;CHILD_CHANNEL&quot;);\n        assertNotNull(childChannel);\n        childChannel.setAccessible(true);  \/\/ XXX: setAccessible \u540e\u7eed\u53ef\u80fd\u4f1a\u7981\u6b62\u8fd9\u6837\u4f7f\u7528\n        String childChannelStr = (String) childChannel.get(LayoutChildStrategy.class);\n        \/\/ \u521d\u59cb\u5316Context\n        LayoutContext ctx = LayoutContext.builder().channel(childChannelStr).build();\n        \/\/\n        processFactory.exec(ctx);\n        assertEquals(&quot;111&quot;, ctx.getLayoutId());\n    }\n\n    @Test\n    void testLpv() {\n        LayoutContext ctx = LayoutContext.builder().channel(&quot;LP-XX-XX-02&quot;).build();\n        processFactory.exec(ctx);\n        assertEquals(&quot;222&quot;, ctx.getLayoutId());\n    }\n\n    @Test\n    void testDefault() {\n        final LayoutContext ctx = LayoutContext.builder().build();\n        processFactory.exec(ctx);\n        assertEquals(&quot;999&quot;, ctx.getLayoutId());\n    }\n}<\/code><\/pre>\n<h3>2 \u601d\u8003<\/h3>\n<p>\u7b56\u7565\u6a21\u5f0f\u80fd\u7ed9\u6211\u4eec\u5e26\u6765\u4ec0\u4e48\uff1f<\/p>\n<ol>\n<li>\u5bf9\u4e1a\u52a1\u903b\u8f91\u8fdb\u884c\u4e86\u4e00\u5b9a\u7a0b\u5ea6\u7684\u5c01\u88c5\uff0c\u5c06\u4e0d\u6613\u53d8\u548c\u6613\u53d8\u903b\u8f91\u8fdb\u884c\u4e86\u5206\u79bb\u3002\u4f7f\u5f97\u540e\u7eed\u7684\u4e1a\u52a1\u53d8\u66f4\uff0c\u4ec5\u4fee\u6539\u76f8\u5e94\u7684\u7b56\u7565\u6216\u8005\u65b0\u589e\u7b56\u7565\u5373\u53ef\u3002<\/li>\n<li>\u4f46\u518d\u6df1\u5c42\u601d\u8003\u4e00\u4e0b\u3002\u4e4b\u524d\u6613\u53d8\u548c\u4e0d\u6613\u53d8\u903b\u8f91\u4fee\u6539\u4ee3\u4ef7\u53ef\u80fd\u76f8\u5dee\u4e0d\u5927\uff0c\u800c\u4f7f\u7528\u8bbe\u8ba1\u6a21\u5f0f\u4e4b\u540e\uff0c\u4f7f\u5f97\u6613\u53d8\u4ee3\u7801\u4fee\u6539\u4ee3\u4ef7\u964d\u4f4e\uff0c\u4f46\u4e0d\u6613\u53d8\u4ee3\u7801\u4fee\u6539\u4ee3\u4ef7\u5219\u4e0a\u5347\u3002\u6240\u4ee5\u5728\u4f7f\u7528\u65f6\u8981\u4e09\u601d\u800c\u540e\u884c\u3002<\/li>\n<li>\u7b56\u7565\u6a21\u5f0f\u6d88\u9664\u4e86<code>if-else<\/code>\u5417\uff1f\u597d\u50cf\u6ca1\u6709\uff0c\u53ea\u662f\u628a\u8fd9\u4e2a\u9009\u62e9\u6743\u5411\u540e\u79fb(\u6216\u8005\u8bf4\u4ea4\u7ed9\u8c03\u7528\u8005)\u4e86\u3002<\/li>\n<li>\u7b56\u7565\u8ba9\u539f\u672c\u6df7\u6742\u5728\u4e00\u4e2a\u6587\u4ef6\u751a\u81f3\u662f\u4e00\u4e2a\u51fd\u6570\u91cc\u9762\u7684\u4ee3\u7801\uff0c\u6253\u6563\u5230\u6570\u4e2a\u6587\u4ef6\u4e2d\u3002\u5982\u679c\u6bcf\u5757\u903b\u8f91\u53ea\u662f\u7b80\u5355\u7684\u51e0\u884c\u4ee3\u7801\uff0c\u4f7f\u7528\u7b56\u7565\u53cd\u800c\u4f1a\u5f97\u4e0d\u507f\u5931\u3002\u8fd8\u4e0d\u5982<code>if-else<\/code>\u6216\u8005<code>switch<\/code>\u6d45\u663e\u6613\u61c2\u3001\u4e00\u76ee\u4e86\u7136\u3002<\/li>\n<\/ol>\n<p>\u7b56\u7565\u6a21\u5f0f\u8ddf\u5176\u4ed6\u6a21\u5f0f\u6709\u5565\u533a\u522b?<\/p>\n<ol>\n<li>\u8ddf<code>\u6a21\u677f\u6a21\u5f0f<\/code>\u6709\u70b9\u50cf\u3002\u4e0d\u8fc7\u6a21\u677f\u6a21\u5f0f\u4e3b\u8981\u662f\u5728\u7236\u7c7b(\u4e0a\u5c42)\u5bf9\u4e00\u4e9b\u52a8\u4f5c\u3001\u65b9\u6cd5\u505a\u7f16\u6392\u3002\u800c\u7531\u4e0d\u540c\u5b50\u7c7b\u53bb\u505a\u5177\u4f53\u52a8\u4f5c\u3001\u65b9\u6cd5\u7684\u5b9e\u73b0\u3002\u91cd\u70b9\u5728\u4e8e\u7f16\u6392\u3002<\/li>\n<li>\u8ddf<code>\u6865\u63a5\u6a21\u5f0f<\/code>\u6709\u70b9\u50cf\u3002\u4e0d\u8fc7\u6865\u63a5\u6709\u591a\u4e2a\u7ef4\u5ea6\u7684\u53d8\u5316\uff0c\u7b56\u7565\u53ef\u4ee5\u8ba4\u4e3a\u662f\u4e00\u7ef4\u7684\u6865\u63a5\u3002<\/li>\n<\/ol>\n<h3>3 \u540e\u7eed<\/h3>\n<p>\u672c\u6253\u7b97\u4e00\u7bc7\u6587\u7ae0\u5c06\u5e38\u7528\u7684\u8bbe\u8ba1\u6a21\u5f0f\u4e00\u5757\u8bb2\u8bb2\uff0c\u8d34\u4e0a\u4ee3\u7801\u4f3c\u4e4e\u6709\u70b9\u957f\uff0c\u8fd8\u662f\u5206\u5f00\u8bf4\u5427\u3002<\/p>\n<hr>\n<blockquote><p>\u5c01\u9762\u56fe\u6765\u6e90: <a href=\"https:\/\/link.segmentfault.com\/?enc=YmKQ%2BPYTqo25%2B5ToI1ZGPA%3D%3D.qqmTi6hD2z9sTp9kAxcCq7jAyP9vO8siMPdjimx7%2BoU8q3eqRuhkeHAVpD9SIwWtf55VfXyS%2FiIrhYlSFnzgFg%3D%3D\" rel=\"nofollow\">https:\/\/refactoring.guru\/desi&#8230;<\/a><\/p><\/blockquote>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042869754\">https:\/\/segmentfault.com\/a\/1190000042869754<\/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-10006901","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\/10006901","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=10006901"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006901\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}