为了解决项目中同一个业务场景,不同场景的实现方式,或者不同环境下多种实现逻辑的问题,避免过多 elsde if 导致后期维护灾难,引用了设计模式来处理这个问题。
思考
- 如何让我们的代码更好维护,并且保证良好的可读性
- 公共逻辑如何抽离
- 流程多的话,后期如何维护,如何在原有的基础上新增逻辑,比如提交数据:数据校验、保存数据、发送消息、推送三方数据,复杂的流程如何保证单一职责
这里我们以简单的一个提交操作为例

代码如下

首先定义上下文,也就是提交的信息实体
基础上下文
1 2
| public class FormBaseContext { }
|
主要的作用是作为一个标识,可能不同的环境内容不同,但是只要以此为父类,我们的方法都可以执行
定义接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| public interface BIzFormBaseService <T extends FormBaseContext>{
void submit(T context);
void save(T context);
void getDetails(String orderId); }
|
对业务行为进行定义,这里只有几个简单的,便于理解,下面对提交这个行为,不同环境,不同实现的处理
具体环境,行为定义接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| public interface BizSaveInformationService {
Integer getOrder();
String getEnvironment();
void handle(FormBaseContext context); }
|
这个接口是对提交这个行为的进行的细分后的定义,提交包含了多个行为,根据行为的优先级排序然后顺序执行。
定义抽象类
核心抽象类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| public abstract class ABsBizFormBaseService implements BIzFormBaseService<FormBaseContext> { public abstract List<BizSaveInformationService> getTiSaveInformationService();
@Override public void submit(FormBaseContext context) { for (BizSaveInformationService bizSaveInformationService : getTiSaveInformationService()) { bizSaveInformationService.handle(context); } }
@Override public void save(FormBaseContext context) { System.out.println("执行 save 方法-----------");
}
@Override public void getDetails(String orderId) { System.out.println("获取 orderId 详情信息"); }
public List<BizSaveInformationService> init(FormBaseFactory factory, String env){ factory.registerTiFormWritesService(env, this); Map<Integer, BizSaveInformationService> tiSaveInformationServiceMap = getTiSaveInformationService().stream() .filter(service -> Arrays.asList(Constant.DEFAULT_ENV, env).contains(service.getEnvironment())) .collect(Collectors.toMap(BizSaveInformationService::getOrder, Function.identity(), ((tiSaveInformationService, tiSaveInformationService2) -> env.equals(tiSaveInformationService.getEnvironment()) ? tiSaveInformationService : tiSaveInformationService2))); return new ArrayList<>(tiSaveInformationServiceMap.values()).stream() .sorted(Comparator.comparing(BizSaveInformationService::getOrder)).collect(Collectors.toList()); } }
|
抽象类是整个设计的核心,集体方法的执行逻辑在这里定义,init()方法,用来筛选出某个环境的需要执行的方法,并根据优先级进行排序,也就是责任链的实现。
环境定义
环境 A
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| public class BIzAFormBaseServiceImpl extends ABsBizFormBaseService implements BIzFormBaseService <FormBaseContext> { private final FormBaseFactory formBaseFactory; private static final String environment = "A"; private List<BizSaveInformationService> bizSaveInformationServiceList; @Override public List<BizSaveInformationService> getTiSaveInformationService() { return bizSaveInformationServiceList; }
public void init(){ formBaseFactory.registerTiFormWritesService(environment, this); Map<Integer, BizSaveInformationService> tiSaveInformationServiceMap = bizSaveInformationServiceList.stream() .filter(service -> Arrays.asList(Constant.DEFAULT_ENV, environment).contains(service.getEnvironment())) .collect(Collectors.toMap(BizSaveInformationService::getOrder, Function.identity(), ((tiSaveInformationService, tiSaveInformationService2) -> environment.equals(tiSaveInformationService.getEnvironment()) ? tiSaveInformationService : tiSaveInformationService2))); bizSaveInformationServiceList = new ArrayList<>(tiSaveInformationServiceMap.values()).stream() .sorted(Comparator.comparing(BizSaveInformationService::getOrder)).collect(Collectors.toList());
}
public BIzAFormBaseServiceImpl(FormBaseFactory factory, List<BizSaveInformationService> bizSaveInformationServiceList){ this.formBaseFactory = factory; this.bizSaveInformationServiceList = bizSaveInformationServiceList; } }
|
环境 B
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| public class BIzBFormBaseServiceImpl extends ABsBizFormBaseService implements BIzFormBaseService <FormBaseContext> { private FormBaseFactory formBaseFactory; private static final String environment = "B"; private List<BizSaveInformationService> bizSaveInformationServiceList; @Override public List<BizSaveInformationService> getTiSaveInformationService() { return bizSaveInformationServiceList; }
public void init(){ formBaseFactory.registerTiFormWritesService(environment, this); Map<Integer, BizSaveInformationService> tiSaveInformationServiceMap = bizSaveInformationServiceList.stream() .filter(service -> Arrays.asList(Constant.DEFAULT_ENV, environment).contains(service.getEnvironment())) .collect(Collectors.toMap(BizSaveInformationService::getOrder, Function.identity(), ((tiSaveInformationService, tiSaveInformationService2) -> environment.equals(tiSaveInformationService.getEnvironment()) ? tiSaveInformationService : tiSaveInformationService2))); bizSaveInformationServiceList = new ArrayList<>(tiSaveInformationServiceMap.values()).stream() .sorted(Comparator.comparing(BizSaveInformationService::getOrder)).collect(Collectors.toList()); }
public BIzBFormBaseServiceImpl(FormBaseFactory formBaseFactory, List<BizSaveInformationService> bizSaveInformationServiceList) { this.formBaseFactory = formBaseFactory; this.bizSaveInformationServiceList = bizSaveInformationServiceList; } }
|
环境 C
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public class BIzCFormBaseServiceImpl extends ABsBizFormBaseService implements BIzFormBaseService <FormBaseContext> { private static final String environment = "C"; private List<BizSaveInformationService> bizSaveInformationServiceList; @Override public List<BizSaveInformationService> getTiSaveInformationService() { return bizSaveInformationServiceList; }
public BIzCFormBaseServiceImpl(FormBaseFactory formBaseFactory, List<BizSaveInformationService> bizSaveInformationServiceList) { this.bizSaveInformationServiceList = bizSaveInformationServiceList; this.bizSaveInformationServiceList = init(formBaseFactory, environment); } }
|
对于公共的逻辑可以像环境 c一样抽成一个公共的实现,放在初始化的时候执行,这里实现的两种方式只是为了提供思路
对于提交动作的执行方法
环境 A保存逻辑
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public class BizASaveBaseServiceImpl implements BizSaveInformationService { @Override public Integer getOrder() { return BizSaveFormOrderEnum.A_SAVE_BASE_FORM.getOrder(); }
@Override public String getEnvironment() { return "A"; }
@Override public void handle(FormBaseContext context) { System.out.println("A环境保存基本信息逻辑"); } }
|
环境 C 保存逻辑
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public class BizCSaveFaultInfoServiceImpl implements BizSaveInformationService { @Override public Integer getOrder() { return BizSaveFormOrderEnum.DEFAULT_C_SAVE_FAULT_INFO_FORM.getOrder(); }
@Override public String getEnvironment() { return "C"; }
@Override public void handle(FormBaseContext context) { System.out.println("C环境保存故障信息--------"); } }
|
默认环境保存逻辑
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public class BizSaveBaseInfoServiceImpl implements BizSaveInformationService { @Override public Integer getOrder() { return BizSaveFormOrderEnum.DEFAULT_SAVE_BASE_FORM.getOrder(); }
@Override public String getEnvironment() { return Constant.DEFAULT_ENV; }
@Override public void handle(FormBaseContext context) { System.out.println("保存基本信息逻辑=========="); } }
|
保存后发送事件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public class BizSaveFormSendEventServiceImpl implements BizSaveInformationService { @Override public Integer getOrder() { return BizSaveFormOrderEnum.DEFAULT_SAVE_SEND_EVENT_FORM.getOrder(); }
@Override public String getEnvironment() { return Constant.DEFAULT_ENV; }
@Override public void handle(FormBaseContext context) { System.out.println("发送事件=========="); } }
|
保存前检查信息逻辑
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public class BizSaveInformationCheckServiceImpl implements BizSaveInformationService { @Override public Integer getOrder() { return BizSaveFormOrderEnum.DEFAULT_SAVE_FORM_CHECK.getOrder(); }
@Override public String getEnvironment() { return Constant.DEFAULT_ENV; }
@Override public void handle(FormBaseContext context) { System.out.println("check==========="); } }
|
以上是不同环境的责任区分,只是作为样例,这里优先级的定义写在了枚举中,这里的实现方式不固定,可以根据具体的业务在选择实现的方式。
责任链优先级
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| public enum BizSaveFormOrderEnum { DEFAULT_SAVE_FORM_CHECK(10, "保存信息校验"), DEFAULT_SAVE_BASE_FORM(20, "保存基本信息"), A_SAVE_BASE_FORM(20,"A环境保存故障基本信息"), DEFAULT_C_SAVE_FAULT_INFO_FORM(25,"保存故障基本信息-C环境"), DEFAULT_SAVE_SEND_EVENT_FORM(30,"保存基本信息发送事件") ; private final Integer order; private final String desc;
BizSaveFormOrderEnum(Integer order, String desc) { this.order = order; this.desc = desc; }
public Integer getOrder() { return order; }
public String getDesc() { return desc; }
}
|
具体调用实现
简单实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| public class ApplicationMain { public static void main(String[] args) {
List<BizSaveInformationService> bizSaveInformationServiceList = new ArrayList<>(); bizSaveInformationServiceList.add(new BizASaveBaseServiceImpl()); bizSaveInformationServiceList.add(new BizCSaveFaultInfoServiceImpl()); bizSaveInformationServiceList.add(new BizSaveBaseInfoServiceImpl()); bizSaveInformationServiceList.add(new BizSaveFormSendEventServiceImpl()); bizSaveInformationServiceList.add(new BizSaveInformationCheckServiceImpl());
FormBaseFactory factory = new FormBaseFactory(); BIzAFormBaseServiceImpl bIzAFormBaseService = new BIzAFormBaseServiceImpl(factory, bizSaveInformationServiceList); bIzAFormBaseService.init();
BIzBFormBaseServiceImpl bIzBFormBaseService = new BIzBFormBaseServiceImpl(factory, bizSaveInformationServiceList); bIzBFormBaseService.init(); BIzCFormBaseServiceImpl bIzCFormBaseService = new BIzCFormBaseServiceImpl(factory, bizSaveInformationServiceList);
BIzFormBaseService a = factory.getTiFormBaseService("C"); TiFormBaseContext context = new TiFormBaseContext(); a.submit(context); } }
|
因为这边用的是 JAVA 最基础的代码,所以对象的创建和初始化,需要手动执行,如果因为 Spring 相关框架使用起来会更加的精简。