业务场景:运营后台会审核用户提交的信息,审核人员在后台看数据的时候会根据提交信息的状态展示不同的页面。
状态如下:1: 未提交 2: 已提交(待审核) 3: 审核不通过 4: 审核通过
不同的逻辑需要渲染的数据不一样,处理逻辑也不一样,此处利用策略实现符合开闭原则的代码,后续再添加不同状态可灵活扩展
设计:
- 审核状态接口工厂类,内涵所有实现的容器,提供对外实例的输出
- 审核状态接口,定义功能以及一个标识策略的唯一key
- 审核状态枚举,用于限制代码越界行为
- 各自实现,后续新增时刻动态扩展
代码:
工厂类
package com.vip.qc.strategy; import org.junit.Assert; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; import java.util.HashMap; import java.util.Map; /** * @author QuCheng on 2020/6/17. */ @Component public class AuthStatusFactory implements ApplicationContextAware { private MapauthStatusMap = new HashMap<>(); @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { Map beansOfType = applicationContext.getBeansOfType(IAuthStatus.class); for (Map.Entry entry : beansOfType.entrySet()) { IAuthStatus value = entry.getValue(); authStatusMap.put(value.getKey().getAuthStatus(), value); } } public void process(Integer key) { IAuthStatus authStatus = authStatusMap.get(StrategyKeyEnum.getKeyByStatus(key).getAuthStatus()); Assert.assertNotNull("请检查策略接口是否实现", authStatus); authStatus.view(); } }
接口
package com.vip.qc.strategy; /** * @author QuCheng on 2020/6/17. */ public interface IAuthStatus { /** * 具体业务逻辑 */ void view(); /** * 定位策略 key * * @return 策略枚举 */ StrategyKeyEnum getKey(); }
枚举
package com.vip.qc.strategy; /** * @author QuCheng on 2020/6/17. */ public enum StrategyKeyEnum { /** * 默认 */ DEFAULT(0), /** * 未提交 */ AUTH_UNCOMMIT(1), /** * 已提交-待审核 */ AUTH_COMMIT(2), /** * 未通过 */ AUTH_UNPASS(3), /** * 已通过 */ AUTH_PASS(4), ; private Integer authStatus; StrategyKeyEnum(Integer authStatus) { this.authStatus = authStatus; } public Integer getAuthStatus() { return authStatus; } public static StrategyKeyEnum getKeyByStatus(Integer status) { for (StrategyKeyEnum value : StrategyKeyEnum.values()) { if (value.getAuthStatus().equals(status)) { return value; } } return DEFAULT; } }
实现
package com.vip.qc.strategy.impl; import com.vip.qc.strategy.IAuthStatus; import com.vip.qc.strategy.StrategyKeyEnum; import org.springframework.stereotype.Component; /** * @author QuCheng on 2020/6/17. */ @Component public class AuthDefault implements IAuthStatus { @Override public void view() { System.out.println("默认处理"); } @Override public StrategyKeyEnum getKey() { return StrategyKeyEnum.DEFAULT; } } package com.vip.qc.strategy.impl; import com.vip.qc.strategy.IAuthStatus; import com.vip.qc.strategy.StrategyKeyEnum; import org.springframework.stereotype.Component; /** * @author QuCheng on 2020/6/17. */ @Component public class AuthUncommit implements IAuthStatus { @Override public void view() { System.out.println("审核未提交"); } @Override public StrategyKeyEnum getKey() { return StrategyKeyEnum.AUTH_UNCOMMIT; } } package com.vip.qc.strategy.impl; import com.vip.qc.strategy.IAuthStatus; import com.vip.qc.strategy.StrategyKeyEnum; import org.springframework.stereotype.Component; /** * @author QuCheng on 2020/6/17. */ @Component public class AuthCommited implements IAuthStatus { @Override public void view() { System.out.println("审核已提交"); } @Override public StrategyKeyEnum getKey() { return StrategyKeyEnum.AUTH_COMMIT; } } package com.vip.qc.strategy.impl; import com.vip.qc.strategy.IAuthStatus; import com.vip.qc.strategy.StrategyKeyEnum; import org.springframework.stereotype.Component; /** * @author QuCheng on 2020/6/17. */ @Component public class AuthUnpass implements IAuthStatus { @Override public void view() { System.out.println("审核未通过"); } @Override public StrategyKeyEnum getKey() { return StrategyKeyEnum.AUTH_UNPASS; } } package com.vip.qc.strategy.impl; import com.vip.qc.strategy.IAuthStatus; import com.vip.qc.strategy.StrategyKeyEnum; import org.springframework.stereotype.Component; /** * @author QuCheng on 2020/6/17. */ @Component public class AuthPass implements IAuthStatus { @Override public void view() { System.out.println("审核通过"); } @Override public StrategyKeyEnum getKey() { return StrategyKeyEnum.AUTH_PASS; } }
测试 - 生产中直接注入AuthStatusFactory即可
@Test public void test() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.vip.qc.strategy"); AuthStatusFactory bean = context.getBean(AuthStatusFactory.class); for (int i = 1; i < 6; i++) { bean.process(i); } }